elasticsearch 弹性搜索和运算符,嵌套字段,elasticsearch,elasticsearch-dsl,elasticsearch,elasticsearch Dsl" /> elasticsearch 弹性搜索和运算符,嵌套字段,elasticsearch,elasticsearch-dsl,elasticsearch,elasticsearch Dsl" />

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

elasticsearch 弹性搜索和运算符,嵌套字段,elasticsearch,elasticsearch-dsl,elasticsearch,elasticsearch Dsl,我有一个类似{“姓名”:“abc”,“年龄”:“20”,“属性”:[{“城市”:“纽约”},{“城市”:“新泽西”}]} 如果我用YORK JERSEY搜索,我希望查询不返回任何记录。我使用了“and”操作符,但它不起作用,因为我搜索同一个字段,而不是多个字段,因为york和jersey在这两个地方都存在,它会给我返回2条记录,有什么想法吗?我也读过关于嵌套字段的文章,但它需要重新索引数据,所以有没有不重新索引数据的解决方案 对象数组的工作方式与预期不同:无法查询 每个对象独立于阵列中的其他对象

我有一个类似{“姓名”:“abc”,“年龄”:“20”,“属性”:[{“城市”:“纽约”},{“城市”:“新泽西”}]}

如果我用YORK JERSEY搜索,我希望查询不返回任何记录。我使用了“and”操作符,但它不起作用,因为我搜索同一个字段,而不是多个字段,因为york和jersey在这两个地方都存在,它会给我返回2条记录,有什么想法吗?我也读过关于嵌套字段的文章,但它需要重新索引数据,所以有没有不重新索引数据的解决方案

对象数组的工作方式与预期不同:无法查询 每个对象独立于阵列中的其他对象。如果你 如果您需要能够做到这一点,那么您应该使用嵌套数据类型 而不是对象数据类型

请参阅上的本ES官方文件以获得详细解释,请参阅此

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

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

索引映射:

{
  "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": {}
        }
    }
}
索引数据:

{
  "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": {}
        }
    }
}
搜索查询:

{
  "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": {}
        }
    }
}
搜索结果:

{
  "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": {}
        }
    }
}

返回无结果

您是否有机会查看我的答案,期待得到您的反馈对不起,我以为我已经发表了评论,但看起来我没有按enter键,谢谢@Bhavya,这很有帮助,我尝试了您的示例,效果很好。除了不重新编制数据索引外,还有其他工作吗?谢谢你的更新是的,我重新编制了数据索引,并且与上述示例一起工作,再次感谢@Bhavya