【问题标题】:Elasticsearch query to match different values for same nested fieldElasticsearch 查询以匹配相同嵌套字段的不同值
【发布时间】:2017-09-17 08:47:58
【问题描述】:

我正在尝试在嵌套文档中获取具有两个名称的文档,但是 must 子句用作“OR”而不是“AND”。 示例如下:

映射:

curl  -XPUT "http://localhost:9200/my_index" -d '
{
  "mappings": {
    "blogpost": {
      "properties": {
        "comments": {
          "type": "nested",
          "properties": {
            "name":    { "type": "keyword"   },
            "age":     { "type": "short"   }
          }
        }
      }
    }
  }
}'

索引 3 个文档:

curl  "http://localhost:9200/my_index/blogpost/1" -d '
{
  "title": "doc1",
  "comments": [
    {
      "name":    "John Smith",
      "age":     28
    },
    {
      "name":    "Alice White",
      "age":     31
    }
  ]
}
'

curl  "http://localhost:9200/my_index/blogpost/2" -d '
{
  "title": "doc2",
  "comments": [
    {
      "name":    "Luther Lawrence",
      "age":     21
    },
    {
      "name":    "Alice White",
      "age":     19
    }
  ]
}
'

curl  "http://localhost:9200/my_index/blogpost/3" -d '
{
  "title": "doc3",
  "comments": [
    {
      "name":    "Tadhg Darragh",
      "age":     22
    },
    {
      "name":    "Alice White",
      "age":     31
    },
    {
      "name":    "Lorene Hicks",
      "age":     44
    }
  ]
}
'

我正在寻找在同一文档中具有comments.name"Alice White" "John Smith" 的文档,使用上述数据只有文档id 1 会匹配。我试过这个查询:

curl  "http://localhost:9200/my_index/blogpost/_search" -d '
{
  "_source": { "include": "title" },
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            { "terms": { "comments.name": ["John Smith", "Alice White"] } }
          ]
        }
      }
    }
  }
}
'

它与所有文档匹配,因为所有文档都有“John Smith”或“Alice White”。 改进此查询以具有两个单独的匹配 query.nested.query.bool.must[].terms,每个值一个匹配器:

curl  "http://localhost:9200/my_index/blogpost/_search" -d '
{
  "_source": { "include": "title" },
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            { "term": { "comments.name": "John Smith" } },
            { "term": { "comments.name": "Alice White" } }
          ]
        }
      }
    }
  }
}
'

所以,我的问题是,如何构建一个查询以仅匹配具有"Alice White" "John Smith" 的文档?

ps。删除了带有 example here 的脚本

【问题讨论】:

    标签: elasticsearch bigdata


    【解决方案1】:
    {
      "_source": {
        "include": "title"
      },
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "comments",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "terms": {
                          "comments.name": [
                            "John Smith"
                          ]
                        }
                      }
                    ]
                  }
                }
              }
            },
            {
              "nested": {
                "path": "comments",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "terms": {
                          "comments.name": [
                            "Alice White"
                          ]
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 为每个名称添加一个嵌套块非常冗长,但可以解决问题,谢谢。
    • 可能很冗长,但就是这样做的。
    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多