elastic / elasticsearch

Free and Open Source, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
1.19k stars 24.84k forks source link

has_child query returns no results when used with date range query #19353

Closed ayadav77 closed 8 years ago

ayadav77 commented 8 years ago

Elasticsearch version: 5.0.0-alpha4

JVM version: 1.8.0_60

Description of the problem including expected versus actual behavior: The has_child query does not return expected results when it contains a date based range query. I created a document type called questions which has a child document type called answers. The answers document has a date field. Once I insert sample data in elasticsearch, I can query the answers document successfully with a date range query. However, the has_child query does not return any results when the date range query utilizes any date value that precedes the answer dates. The following steps explain the issue further.

Steps to reproduce: 1) Create an index called "sandbox"

PUT /sandbox

2) Create a document mapping for "answers" with a parent type of "questions".

PUT /sandbox/_mapping/answers
{
  "_parent": {
      "type": "questions" 
  }
  , "properties": {
    "answer" : {"type": "text"},
    "date" : {"type": "date"}
  }
}

3) Add a "questions" document

PUT /sandbox/questions/1
{
  "question" : "Why is the sky blue?"
}

4) Add a couple of "answers" document

PUT /sandbox/answers/1?parent=1
{
  "answer" : "Due to scattering of sunlight",
  "date" : "2016-05-01"
}
PUT /sandbox/answers/2?parent=1
{
  "answer" : "Due to refraction of light",
  "date" : "2016-06-01"
}

5) Find all answers with a date value of gte "2016-04-01". THIS WORKS AS EXPECTED. It returns the 2 answers documents as expected.

GET /sandbox/answers/_search
{
  "query" : {
    "range": {
      "date": {
        "gte": "2016-04-01"
      }
    }
  }
}

6) Now use the has_child query to get questions with answers that have date gte "2016-04-01". DOES NOT WORK AS EXPECTED. This returns 0 hits. I expected the 1 "questions" document in the response.

GET /sandbox/questions/_search
{
  "query" : {
    "has_child": {
      "type": "answers",
      "query": {
        "range": {
          "date": {
            "gte": "2016-04-01"
          }
        }
      }
    }
  }
}
jimczi commented 8 years ago

The has_child query operates at the index level, you cannot restrict the type of your query like you're doing in your snippet: GET /sandbox/questions/_search Try with /sandbox/_search

martijnvg commented 8 years ago

The has_child query operates at the index level, you cannot restrict the type of your query like you're doing in your snippet:

This is possible, since 2.0 if I recall correctly.

This really seems to be caused by a bug in: HasChildQueryBuilder#rewrite(...)

Fixing that makes the query work as expected. I'll open a PR.