Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

elasticsearch - Elastic cannot find nested element

This is the mapping:

    {
        myindex: {
            mappings: {
                properties: {
                    identifier: {
                        type: "keyword"
                    },
                    parts: {
                        type: "nested",
                        properties: {
                            _id: {
                                type: "text",
                                fields: {
                                    keyword: {
                                        type: "keyword",
                                        ignore_above: 256
                                    }
                                }
                            },
                            user_id: {
                                type: "text",
                                fields: {
                                    keyword: {
                                        type: "keyword",
                                        ignore_above: 256
                                    }
                                }
                            }
                        }   
                    }
                }
            }
        }
    }       

The index contains this entry:

_source: {
    identifier: "6002af6aa8672d4f8d06be3b6002c4db0fa23460882e82ab",
    parts: [
        {
            _id: "6002c4dc0fa23460882e82af",
            user_id: "6002c4db0fa23460882e82ab",
        },
        {
            _id: "6002c4dc0fa23460882e82b0",
            user_id: "6002af6aa8672d4f8d06be3b",
        }
    ]
}

When searching for a top level field, then the entry above is returned:

curl -X GET "localhost:9200/myindex/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "identifier": "6002af6aa8672d4f8d06be3b6002c4db0fa23460882e82ab"
                    }
                }
            ]
        }
    }
}'  

However, when I run this query (for searching inside the "parts" array), I don't get any result (i.e. an empty hits):

curl -X GET "localhost:9200/myindex/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "parts.user_id": "6002c4db0fa23460882e82ab"
                    }
                }
            ]
        }
    }
}'

Why? I read about nested elements. What I understood, mapping the field named "parts" as nested is the right way to search inside arrays.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use a nested query to search inside arrays. Modify your search query as shown below-

 {
  "query": {
    "nested": {
      "path": "parts",
      "query": {
        "bool": {
          "must": {
            "term": {
              "parts.user_id.keyword": "6002c4db0fa23460882e82ab"
            }
          }
        }
      },
      "inner_hits":{}
    }
  }
}

The search result will be

"hits": [
      {
        "_index": "65749032",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.6931471,
        "_source": {
          "identifier": "6002af6aa8672d4f8d06be3b6002c4db0fa23460882e82ab",
          "parts": [
            {
              "_id": "6002c4dc0fa23460882e82af",
              "user_id": "6002c4db0fa23460882e82ab"
            },
            {
              "_id": "6002c4dc0fa23460882e82b0",
              "user_id": "6002af6aa8672d4f8d06be3b"
            }
          ]
        },
        "inner_hits": {
          "parts": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.6931471,
              "hits": [
                {
                  "_index": "65749032",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "parts",
                    "offset": 0
                  },
                  "_score": 0.6931471,
                  "_source": {
                    "_id": "6002c4dc0fa23460882e82af",
                    "user_id": "6002c4db0fa23460882e82ab"
                  }
                }
              ]
            }
          }
        }
      }
    ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...