Elasticsearch query on arrays of nested objects

341 views Asked by At

I'm struggling with a query problem on ElasticSearch. I have this kind of objects recorded :

{
    "obj_id": 1,
    "label": "label obj 1",
    "array_of_nested": [{
            "nested_id": 1,
            "label": "label nested obj1"
        }, {
            "nested_id": 2,
            "label": "label nested obj2"
        }
    ]
}, {
    "obj_id": 2,
    "label": "label obj 2",
    "array_of_nested": [{
            "nested_id": 3,
            "label": "label nested obj1"
        }, {
            "nested_id": 4,
            "label": "label nested obj2"
        }
    ]
}

I'm trying to write a query to find all objects with a nested_id of 2 in the array_of_nested property, but couldn't make it work so far. :/

Thank you !

3

There are 3 answers

1
Guillaume Georges On BEST ANSWER

Can you try this?

{
  "query": {
    "match": {
      "array_of_nested.nested_id": 2
    }
  }
}
2
Amit On

Adding a working example with mapping, example docs, and working search query. you need to use the path param of nested field

Mapping

{
    "mappings": {
        "properties": {
            "array_of_nested": {
                "type": "nested"
            },
            "obj_id" :{
                "type" : "text"
            },
            "label" :{
                "type" : "text"
            }
        }
    }
}

Sample docs

{
    "obj_id": 1,
    "label": "label obj 1",
    "array_of_nested": [
        {
            "nested_id": 1,
            "label": "label nested obj1"
        },
        {
            "nested_id": 2,
            "label": "label nested obj2"
        }
    ]
}

And second doc

{
    "obj_id": 2,
    "label": "label obj 2",
    "array_of_nested": [
        {
            "nested_id": 3,
            "label": "label nested obj1"
        },
        {
            "nested_id": 4,
            "label": "label nested obj2"
        }
    ]
}

Search query

{
    "query": {
        "nested": {
            "path": "array_of_nested",
            "query": {
                "term": {
                    "array_of_nested.nested_id": {
                        "value": "2"
                    }
                }
            }
        }
    }
}

And your expected search result

"hits": [
            {
                "_index": "nestedobj",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "obj_id": 1,
                    "label": "label obj 1",
                    "array_of_nested": [
                        {
                            "nested_id": 1,
                            "label": "label nested obj1"
                        },
                        {
                            "nested_id": 2,
                            "label": "label nested obj2"
                        }
                    ]
                }
            }
        ]
1
Saeed Nasehi On

In nested types you need to define path in your query and your query would be something like this:

{
  "query": {
    "nested": {
      "path": "array_of_nested",
      "query": {
        "term": {
          "array_of_nested.nested_id": {
            "value": "2"
          }
        }
      }
    }
  }
}