2

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 !

Amit
  • 30,756
  • 6
  • 57
  • 88

3 Answers3

2

Can you try this?

{
  "query": {
    "match": {
      "array_of_nested.nested_id": 2
    }
  }
}
Guillaume Georges
  • 3,878
  • 3
  • 14
  • 32
  • Amazingly: it works !! I can't believe it but it wasn't necessary to go for too complicated a request finally. Thank you ! – user3379897 Oct 20 '20 at 13:25
1

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"
          }
        }
      }
    }
  }
}
Saeed Nasehi
  • 940
  • 1
  • 11
  • 27
1

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"
                        }
                    ]
                }
            }
        ]
Amit
  • 30,756
  • 6
  • 57
  • 88
  • Thank you but actually my inner object hasn't the nested property in it's mapping and I'm not eager to reindex all of it. ;) – user3379897 Oct 20 '20 at 13:26
  • @user3379897 oh anyway glad it worked and would hv provided the working solution if you had provided the mapping :) – Amit Oct 20 '20 at 13:30