John Karistinakis
John Karistinakis

Reputation: 121

Get certain fields from children in has-child query

I have an index with parent-child capability. I managed the has-child query to bring me specific fields from the parent with the command:

_source": ["parent_field_1","parent_field_2"...]

and it worked. Now i want to do the same for the children fields putting the same command again, but i receive the error message:

"type": "parsing_exception",
        "reason": "[has_child] query does not support [_source]",

My query:

GET npk/_search
    {
      "_source": ["parent_field_1","parent_field_2"...],
      "query": {
        "has_child": {
          "type": "body",
          "min_children": 1,
         "_source": ["child_field_1","child_field_2"...]
         "query": {
              "range": {
                 "child_field_1": {
                    "gte": 5,
                    "lte": 10
        }  } },
       "inner_hits": {}
        }  } }
  1. How can i choose which fields from children i receive?
  2. Is there any way to put multiple search filters both in parent and child? In child i am able to put only one filter and in parent none! I just get the error bellow when i tried it:

    "type": "parsing_exception", "reason": "[range] query doesn't support multiple fields, found [child_field_3] and [child_field_4]"

Upvotes: 1

Views: 443

Answers (3)

John Karistinakis
John Karistinakis

Reputation: 121

Here again with an ansewer to the second part of my question:

2. Is there any way to put multiple search filters both in parent and child? In child i am able to put only one filter and in parent none!

I managed to insert multiple filters with multiple has_child buckets, but it doesn't work 100%. I mean, it gives me results but not in the right range for field: "gpsSats": {"gte": 17,"lte": 17}. It brings me results also out of this range despite i have included in my query the "must" clause which stands for AND. What is wrong here? Any help please?

My query:

GET npk/_search
{   "_source": ["CameraNo"],
  "query": {
      "bool": {  
        "must": [
          {
            "range": {
                "CameraNo": {
                  "gte": 1,
                  "lte": 4
                  }
                } 
              },
          {
            "has_child": {
               "type": "body",
                 "query": { 
                   "range": {
                     "gpsSats": {
                       "gte": 17,
                       "lte": 17
                 }
               }
             }
           }
          },
          {
            "has_child": {
               "type": "body",
                 "query": {
                   "range": {
                     "ndvi_1": {
                       "gte": 0.4,
                       "lte": 0.5
                  }
                }
              }
            }
          },
          { 
             "has_child": {
               "type": "body",
                 "query": {
                   "range": {
                     "ndvi_0": {
                       "gte": 0.494,
                       "lte": 0.495
                }
              }
            },
              "inner_hits": {"_source": ["ndvi_0","ndvi_1","gpsSats"]}
          }
        } 
      ]
    }
  }
}

Upvotes: 0

John Karistinakis
John Karistinakis

Reputation: 121

Here i am again with a little bit answer! I just found the answer to the first part of my question. So, to define which fields i will get from children, i just put the "_source": [child_field_3","child_field_4" to "inner_hits" at the and of the query, and voila!

Upvotes: 1

apt-get_install_skill
apt-get_install_skill

Reputation: 2908

To answer your first question:

You get the error

[has_child] query does not support [_source]

because you can not define the key _source inside a query element. You can only set this key once top-level.

Upvotes: 0

Related Questions