【问题标题】:Is it possible to combine MultiGet with a Bool query in elasticsearch?是否可以将 MultiGet 与弹性搜索中的 Bool 查询结合使用?
【发布时间】:2021-01-06 12:32:02
【问题描述】:

MultiGet api 可用于根据给定的 id 获取多个文档。 (https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html)

是否可以为此操作添加更多过滤器?给定一个 id 列表,我只想找到 'color' = 'green' 的文档。

示例:

假设存在以下文档:

  • 文档 1:{"id": "1", "color": "green" }
  • 文档 2:{"id": "2", "color": "red" }
  • 文档 3:{"id": "3", "color": "green"}
  • 文档 4:{"id": "4", "color": "green"}

目标:

获取 ID 为 1、2 或 4,颜色为“Green”的文档。

预期结果:

[文档 1,文档 4]

到目前为止的查询...:

GET /_mget
{
  "docs": [
    {
      "_index": "my-index-000001",
      "_id": "1"
    },
    {
      "_index": "my-index-000001",
      "_id": "2"
    },
    {
      "_index": "my-index-000001",
      "_id": "4"
    }
  ]
}

或在 C# 中使用 NEST:

var ids = new List<string> {"1", "2", "4"};
var result = await _elasticClient
   .MultiGetAsync(s => s
                    .Index("my-index-000001")
                    .GetMany<SomeRecordObject>(ids)
                );

所以我实际上正在寻找一种将 Bool 查询添加到 MultiGet 查询的方法(我认为)。谁能指出我正确的方向?

【问题讨论】:

标签: elasticsearch nest


【解决方案1】:

multi get 仅用于通过 id 获取多个文档。如果您需要执行查询或过滤,则需要搜索查询。

请注意,当涉及到只是索引的文档时,多重获取和搜索具有不同的行为。使用 multi get,just 索引文档可以立即使用其 id 检索。但是,对于搜索,只有在刷新索引(refresh interval elapses (by default, 1 second)index is manually refreshed with the refresh API)后,文档才会出现在搜索结果中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    相关资源
    最近更新 更多