Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner_hits in should clauses override each other #37584

Closed
imotov opened this issue Jan 17, 2019 · 5 comments
Closed

Inner_hits in should clauses override each other #37584

imotov opened this issue Jan 17, 2019 · 5 comments
Labels
>bug good first issue low hanging fruit help wanted adoptme :Search/Search Search-related issues that do not fall into other categories

Comments

@imotov
Copy link
Contributor

imotov commented Jan 17, 2019

This issues is somewhat similar to #16218, but it manifests itself if two should clauses contain has_child with inner_hits and different queries and only one of the query in the last clause doesn't matches the document returned by the first clause.

This is a repro for 6.x

DELETE test
PUT test
{
  "mappings": {
    "_doc": {
      "properties": {
        "my_join_field": {
          "type": "join",
          "relations": {
            "question": "answer"
          }
        },
        "text": {
          "type": "text"
        }
      }
    }
  }
}

POST test/_doc/_bulk
{"index":{"_id": "1"}}
{"text": "This is a question","my_join_field": {"name": "question"}}
{"index":{"_id": "2"}}
{"text": "This is another question","my_join_field": {"name": "question"}}
{"index":{"_id": "3", "_routing": "1"}}
{"text": "This is an answer", "my_join_field": {"name": "answer","parent":"1"}}
{"index":{"_id": "4", "_routing": "1"}}
{"text": "This is another answer", "my_join_field": {"name": "answer","parent":"1"}}

POST test/_search

# Doesn't return inner hits
POST test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "has_child": {
            "type": "answer",
            "query": {
              "match": {
                "text": "answer"
              }
            },
            "inner_hits": {}
          }
        },
        {
          "has_child": {
            "type": "answer",
            "query": {
              "match": {
                "text": "noanswer"
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}

# Returns inner hits:
POST test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "has_child": {
            "type": "answer",
            "query": {
              "match": {
                "text": "noanswer"
              }
            },
            "inner_hits": {}
          }
        },
        {
          "has_child": {
            "type": "answer",
            "query": {
              "match": {
                "text": "answer"
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}

Repro for 7.x can be found here

Originally reported in https://discuss.elastic.co/t/inner-hits-has-child/164627/10

@imotov imotov added >bug :Search/Search Search-related issues that do not fall into other categories labels Jan 17, 2019
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search

@jimczi
Copy link
Contributor

jimczi commented Jan 17, 2019

inner_hits takes the name of the nested path by default. If you have two queries that uses the same path you need to use a different name to disambiguate. It's not explained well in the docs and we should throw an error if we find two inner_hits with the same name so I am not closing this issue and will mark it as a bug. However the workaround is simple, just set a different name on each inner_hits and they will be returned in different sections in the search response.

@jimczi jimczi added good first issue low hanging fruit help wanted adoptme labels Jan 17, 2019
@jimczi
Copy link
Contributor

jimczi commented Jan 17, 2019

looking to take a crack at this

This is the function that populates the inner_hits map at each level.
We should check that the name is not already used when entries are added in the map. This can happen in three places:

All these functions should throw an illegal argument exception if the map already contains an entry with the same name.

@kwojcicki
Copy link

Perfect thanks @jimczi :)

dvehar added a commit to dvehar/elasticsearch that referenced this issue Jan 25, 2019
jimczi pushed a commit that referenced this issue Feb 1, 2019
This change throws an error if two inner_hits have the same name

Closes #37584
jimczi pushed a commit to jimczi/elasticsearch that referenced this issue Feb 1, 2019
This change throws an error if two inner_hits have the same name

Closes elastic#37584
jimczi added a commit that referenced this issue Feb 4, 2019
This change throws an error if two inner_hits have the same name

Closes #37584
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug good first issue low hanging fruit help wanted adoptme :Search/Search Search-related issues that do not fall into other categories
Projects
None yet
Development

No branches or pull requests

4 participants