【问题标题】:Elasticsearch query multiple subfieldsElasticsearch 查询多个子字段
【发布时间】:2020-02-26 04:20:11
【问题描述】:

我很好奇是否有人知道如何查询 Elasticsearch 索引中的多个子字段。例如记录:

{
  "foo": [
    {
      "bar": "Good example",
      "subfield":  32
    }
  ]
},
{
  "foo": [
    {
      "bar": "Good example",
      "subfield":  50
    }
  ]
},
{
  "foo": [
    {
      "bar": "Bad example",
      "subfield":  32
    }
  ]
}

我希望建立一个查询foo.bar = "Good example" AND foo.subfield = 32,只返回第一条记录。欢迎使用 Elasticsearch 的查询 DSL 或查询字符串。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你想要一个nested query

    {
      "query": {
        "nested": {
          "path": "foo",
          "query": {
            "bool": {
              "must": [
                { "term": { "foo.bar": "Good example" } },
                { "term": { "foo.subfield": 32 } }
              ]
            }
          }
        }
      }
    }
    

    请注意,根据文档,您必须更新映射以包含嵌套字段映射。

    【讨论】:

    • 在映射中已经有了嵌套字段类型,还没有尝试使用 'term'。但是使用 bool 并匹配它会返回所有 3 个对象。
    • 成功了!并意识到我的match 用法也有效,我只是查询错误。谢谢!
    猜你喜欢
    • 2022-06-29
    • 2016-08-18
    • 2022-12-04
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    相关资源
    最近更新 更多