0

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.

1 Answer 1

1

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"
                  }
                }
              ]
            }
          }
        }
      }
    ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.