Search code examples
elasticsearchelasticsearch-queryelasticsearch-nested

Elasticsearch query on arrays of nested objects


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 !


Solution

  • Can you try this?

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