0

can somebody help me to understand what Elastic means by nested. In documentations https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_nested_sorting_examples is an example which does not show how the document object looks like. It look like I should imagine the mapping from the search query. Query looks like:

POST /_search
{
   "query": {
      "nested": {
         "path": "parent",
         "query": {
            "bool": {
                "must": {"range": {"parent.age": {"gte": 21}}},
                "filter": {
                    "nested": {
                        "path": "parent.child",
                        "query": {"match": {"parent.child.name": "matt"}}
                    }
                }
            }
         }
      }
   },
   "sort" : [
      {
         "parent.child.age" : {
            "mode" :  "min",
            "order" : "asc",
            "nested": {
               "path": "parent",
               "filter": {
                  "range": {"parent.age": {"gte": 21}}
               },
               "nested": {
                  "path": "parent.child",
                  "filter": {
                     "match": {"parent.child.name": "matt"}
                  }
               }
            }
         }
      }
   ]
}

Can somebody write a document structure on which this query will work?

2 Answers 2

1

Something like this.

{
    "parent": {
        "name": "Elasti Sorch",
        "age": 23,
        "child": [
           {
              "name": "Kibana Lion",
              "age": 12
           }, 
           {
              "name": "Matt",
              "age": 15
           }
        ] 
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I think child.age should be an array because of "mode": "min" has to be used there. Am I right? elastic.co/guide/en/elasticsearch/reference/current/…
Yes. Totally missed that. Added it now.
Can you tell me please why "must": {"range": {"parent.age": {"gte": 21}}}, part of the query is in nested sub-query if this condition is related to the parent?
Because age is child object of parent. If document would have been like {"parent_name":"Elastic Sorch", "parent_age":23 }, then it would not have been nested query.
I think I am wrong. The child is an array of objects as you write.
|
1

In Elastic nested means it's an array of objects. To store an array of objects into a field in elastic search you have to map the field to a nested while creating the index.

PUT parent
       {
       "mappings": {
        "doc":{
      "properties": {
        "name":{
          "type": "text"
        },
        "age":{
          "type": "integer"
        },
        "child":{
          "type": "nested",
          "properties": {
            "name":{
              "type":"text"
            },
            "age":{
              "type":"integer"
            }
          }

        }
      }
    }
  }
}

and a sample nested document cab be inserted like this

POST parent/doc
{
  "name":"abc",
  "age":50,
  "child":[
    {
      "name":"son1",
      "age":25
    },
    {
      "name":"adughter1",
      "age":20
    }
    ]
}

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.