Help me to query this document

This is my documents:

//document 1
{
    "place": "galaxy",
    "range": {
        "area": {
            "planet": "mars",
            "country": [

              -----------country 1------------------

                {
                    "name": "mars-country_1",
                    "use_by": "alien",
                    "region": [ 
   
             -------region 1--------

                        {
                            "name": "ABC",
                            "value": "ABC"
                        },

             -------region 2--------

                        {
                            "name": "DEF",
                            "value": "DEF"
                        }
                    ]
                },

              -----------country 2------------------

                {
                    "name": "mars-country_2",
                    "use_by": "alien_2",
                    "region": [
                        {
                            "name": "ABC",
                            "value": "ABC"
                        },
                        {
                            "name": "DEF",
                            "value": "DEF"
                        }
                    ]
                }
              -----------country 3------------------
              -----------country 4------------------
            ]
        }
    }
}
//document 2
//document 3

Now if I provide country name and region name: "mars-country_1" and "ABC", the result will:

                {
                    "name": "mars-country_1",
                    "use_by": "alien",
                    "region": [
                        {
                            "name": "ABC",
                            "value": "ABC"
                        }
                    ]
                }

How can I do that, I do not know how to query in this case. Can anyone help me? Many thanks

Welcome!

You can't really get that as a result as the full document you indexed will be returned.

But the question is: what are you searching for? A region?

Mapping is very normal, every object array is nested (country and region).
I search for a region, but because region in mars-country_1 and mars-country_2 is similar, so if I query for example ABC:, it will return both {"name": "ABC", "value": "ABC"} in mars-country_1 and mars-country_2 while I want result only have 1 object of region.
==> I provide country name as well to distinct them, so the input is mars-country_1 and ABC, the result I want is:

                {
                    "name": "mars-country_1",
                    "use_by": "alien",
                    "region": [
                        {
                            "name": "ABC",
                            "value": "ABC"
                        }
                    ]
                }

But I do not know how to query them to have result like above

If you are searching for region, you should index regions instead. So denormalize your data. It will make your life easier.

1 Like

I actually cannot edit data in this case :frowning: can you help me to solve this

Have a look at this: Retrieve inner hits | Elasticsearch Guide [8.9] | Elastic

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.