用于索引数组元素的 Elasticsearch

Elasticsearch for index array element

您好,我想使用弹性搜索查询从索引中搜索数组元素

{
  "name": "Karan",
  "address": [
                        {
                            "city": "newyork",
                            "zip": 12345
                        },
                        {
                            "city": "mumbai",
                            "zip": 23456
                        }]
}}

当我尝试使用匹配查询进行搜索时,它不起作用

{
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "address.city": "newyork"
            }
          }
        ]
      }
    }
  }

当我访问像 "name" 这样的简单字段时:"Karan" 它有效,只有数组元素有问题。

因为嵌套对象被索引为单独的隐藏文档,我们不能直接查询它们。相反,我们必须使用 嵌套查询 来访问它们:

GET /my_index/blogpost/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "eggs" 
          }
        },
        {
          "nested": {
            "path": "comments", 
            "query": {
              "bool": {
                "must": [ 
                  {
                    "match": {
                      "comments.name": "john"
                    }
                  },
                  {
                    "match": {
                      "comments.age": 28
                    }
                  }
                ]
              }
            }
          }
        }
      ]
}}}

See the docs

我遵循的方式..

映射:

{
  "mappings": {
    "job": {
      "properties": {
        "name": {
          "type": "text"
        },
        "skills": {
          "type": "nested",
          "properties": {
            "value": {
              "type": "text"
            }
          }
        }
      }
    }
  }

记录

[{"_index":"jobs","_type":"job","_id":"2","_score":1.0,"_source":{"name":"sr soft eng","skills":[{"value": "java"}, {"value": "oracle"}]}},{"_index":"jobs","_type":"job","_id":"1","_score":1.0,"_source":{"name":"sr soft eng","skills":[{"value": "java"}, {"value": "oracle"}, {"value": "javascript"}]}},

搜索查询

{
  "query": {
    "nested": {
      "path": "skills",
      "query": {
        "bool": {
          "must": [
            { "match": {"skills.value": "java"}}
          ]
        }
      }
    }
  }
}