Multiple search filters in has_child query

Hi all!
I have an index with parent-child capability and the has_child query below with a filter-range applied on children (child_field_1). My issues here are 2.

  1. I cannot put multiple filters to children (only one),

My query:

GET npk/_search
    {
      "query": {
        "has_child": {
          "type": "body",
          "min_children": 1,
         "query": {
              "range": {
                 "child_field_1": {
                    "gte": 5,
                    "lte": 10
        },
                 "child_field_2": {
                    "gte": 8,
                    "lte": 12
        }  
} },
       "inner_hits": {}
        }  } }

Response:

"type": "parsing_exception",
        "reason": "[range] query doesn't support multiple fields, found [child_field_1] and [child_field_2]",
  1. I cannot put a filter for parent.

My query:

GET npk/_search
    {
      "query": {
        "range": {
                 "parent_field_1": {
                    "gte": 1,
                    "lte": 10
        }},
        "has_child": {
          "type": "body",
          "min_children": 1,
         "query": {
              "range": {
                 "child_field_1": {
                    "gte": 5,
                    "lte": 10
        }
} },
       "inner_hits": {}
        }  } }

Response:

"type": "parsing_exception",
        "reason": "[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",

Here again with an ansewer to my question.
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"]}
          }
        } 
      ]
    }
  }
}

can you provide a full, but minimal reproduction including index creation & mapping, document indexing, the query you are executing, plus documents that get returned compared to what you expect?

You can use the inner_hits query to check which children have matched, in case that helps debugging.

Thank you!

index creation:

PUT /npk
{ 
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    },
  "dynamic": true,
  "mappings": {
    "doc": {
      "properties": {
        "my_join_field" : {
                "type" : "join",
                "relations": {
                    "header": "body"
                }}
      }}}

A sample of documents indexed. Practically, it's a log file in which as 'parent' i used the header of this file (the vertical list with the fields with '#' in front of). And below this, it's the body of the file, which acts as 'children'.

Below, is a part of an answer i get and as you see, the field "gpsSats" has brought to me values
'17' which are correct, but also values '20' which are out of range i specified.

{
  "took": 35,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 4,
    "hits": [
      {
        "_index": "npk",
        "_type": "doc",
        "_id": "1",
        "_score": 4,
        "_source": {
          "CameraNo": 3
        },
        "inner_hits": {
          "body": {
            "hits": {
              "total": 53,
              "max_score": 1,
              "hits": [
                {
                  "_index": "npk",
                  "_type": "doc",
                  "_id": "12013",
                  "_score": 1,
                  "_routing": "1",
                  "_source": {
                    "ndvi_1": 0.563597,
                    "ndvi_0": 0.494044,
                    "gpsSats": 17
                  }
                },
                {
                  "_index": "npk",
                  "_type": "doc",
                  "_id": "12065",
                  "_score": 1,
                  "_routing": "1",
                  "_source": {
                    "ndvi_1": 0.555584,
                    "ndvi_0": 0.494376,
                    "gpsSats": 17
                  }
                },
                {
                  "_index": "npk",
                  "_type": "doc",
                  "_id": "12074",
                  "_score": 1,
                  "_routing": "1",
                  "_source": {
                    "ndvi_1": 0.562169,
                    "ndvi_0": 0.494615,
                    "gpsSats": 17
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index": "npk",
        "_type": "doc",
        "_id": "51798",
        "_score": 4,
        "_source": {
          "CameraNo": 3
        },
        "inner_hits": {
          "body": {
            "hits": {
              "total": 53,
              "max_score": 1,
              "hits": [
                {
                  "_index": "npk",
                  "_type": "doc",
                  "_id": "52858",
                  "_score": 1,
                  "_routing": "1",
                  "_source": {
                    "ndvi_1": 0.511705,
                    "ndvi_0": 0.494054,
                    "gpsSats": 20
                  }
                },
                {
                  "_index": "npk",
                  "_type": "doc",
                  "_id": "52895",
                  "_score": 1,
                  "_routing": "1",
                  "_source": {
                    "ndvi_1": 0.522266,
                    "ndvi_0": 0.494395,
                    "gpsSats": 20
                  }
                },
                {
                  "_index": "npk",
                  "_type": "doc",
                  "_id": "52917",
                  "_score": 1,
                  "_routing": "1",
                  "_source": {
                    "ndvi_1": 0.523934,
                    "ndvi_0": 0.494879,
                    "gpsSats": 20
                  }
                }
              ]
            }
          }
        }
      }
}

Notes: 'CameraNo' is a parent field and 'ndvi_0', 'ndvi_1', 'gpsSats' are children fields.

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