弹性搜索,和运算符,嵌套字段

Elastic search , And operator , Nested fields

我有一个文档,类似 { "name": "abc", "age": "20", "attributes": [{ "city": "NEW YORK" }, { "city": “新泽西州”} ] }

如果我使用 YORK JERSEY 进行搜索,我想查询 return 没有记录。我使用了“and”运算符,但它不起作用,因为我在同一个字段而不是多个字段上搜索,因为 york 和 jersey 在两个地方都存在,它给了我 2 条记录,有什么想法吗?我还阅读了有关嵌套字段的信息,但它需要重新索引数据,所以有没有重新索引数据的解决方案?

Arrays of objects do not work as you would expect: you cannot query each object independently of the other objects in the array. If you need to be able to do this then you should use the nested data type instead of the object data type.

参考这篇ES官方文档Arrays to get a detailed explanation, and please refer to this

添加包含索引数据、映射、搜索查询和搜索结果的工作示例

应用嵌套数据类型后,您必须重新索引数据

索引映射:

{
  "mappings":{
    "properties":{
      "attributes":{
        "type":"nested"
      }
    }
  }
}

索引数据:

{
  "name": "abc",
  "age": "20",
  "attributes": [
    {
      "city": "NEW YORK"
    },
    {
      "city": "NEW JERSEY"
    }
  ]
}

搜索查询:

{
    "query": {
        "nested": {
            "path": "attributes",
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "attributes.city": "YORK"
                            }
                        },
                        {
                            "match": {
                                "attributes.city": "JERSEY"
                            }
                        }
                    ]
                }
            },
            "inner_hits": {}
        }
    }
}

搜索结果:

Returns无结果