Elasticsearch OR query with nested objects returns inner_hits not matching the criteria

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Elasticsearch OR query with nested objects returns inner_hits not matching the criteria



I'm getting weird results when querying nested objects. Imagine the following structure:


owner.name = "fred",
...,
pets [
name = "daisy", ... ,
name = "flopsy", ...
]



If I only have the document shown above, and I search pets matching this criteria:


pets.name = "daisy" OR
(owner.name = "julie" and pet.name = "flopsy")



I would expect to only get one result ("daisy"), but I'm getting both pet names.



This is one way to reproduce this:


# Create nested mapping
PUT pet-owners

"mappings":
"animals":
"properties":
"owner": "type": "text",
"pets":
"type": "nested",
"properties":
"name": "type": "text", "fielddata": true







# Insert nested object
PUT pet-owners/animals/1?op_type=create

"owner" : "fred",
"pets" : [
"name" : "daisy",
"name" : "flopsy"
]


# Query
GET pet-owners/_search
"from": 0, "size": 50,
"query":
"constant_score":
"filter": "bool": "must": [
"bool": "should": [
"nested": "query":
"term": "pets.name": "daisy",
"path":"pets",
"inner_hits":
"name": "pets_hits_1",
"size": 99,
"_source": false,
"docvalue_fields": ["pets.name"]

,
"bool": "must": [
"term": "owner": "julie",
"nested": "query":
"term": "pets.name": "flopsy",
"path":"pets",
"inner_hits":
"name": "pets_hits_2",
"size": 99,
"_source": false,
"docvalue_fields": ["pets.name"]


]
]
],
"_source": false



The query returns both pets names (as opposed to the expected one).



Is this behavior normal? Am I doing something wrong, or my reasoning about the nested structure or the query behavior is flawed?



Any help or guidance will be much appreciated.



I'm running this query under ElasticSearch 6.3.x



EDIT: I'm adding the response received, to better illustrate the case



"took": 16,
"timed_out": false,
"_shards":
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
,
"hits":
"total": 1,
"max_score": 1,
"hits": [

"_index": "pet-owners",
"_type": "animals",
"_id": "1",
"_score": 1,
"inner_hits":
"pets_hits_1":
"hits":
"total": 1,
"max_score": 0.6931472,
"hits": [

"_index": "pet-owners",
"_type": "animals",
"_id": "1",
"_nested":
"field": "pets",
"offset": 0
,
"_score": 0.6931472,
"fields":
"pets.name": [
"daisy"
]


]

,
"pets_hits_2":
"hits":
"total": 1,
"max_score": 0.6931472,
"hits": [

"_index": "pet-owners",
"_type": "animals",
"_id": "1",
"_nested":
"field": "pets",
"offset": 1
,
"_score": 0.6931472,
"fields":
"pets.name": [
"flopsy"
]


]




]




So we can see that it's not that the query matches and returns the whole existing document, but that it returns each of the pets independently, one inside each of the inner_hits. It's this result that's surprising to me.




1 Answer
1



(edited) - in summary this issue is around the context of the 'inner_hits':



It looks like the inner_hits 'pets_hits_2' is returning a match because it is belonging to the nested query that simply searches the pets field for 'flopsy'.



As an independent query on our single document, that is a valid hit.



However, because that query is within a list of bool/must queries, where other queries will not match on our document, you may well expect that the inner_hits should pick up on this and therefore not return a hit.



I haven't been able to find any docs to clarify whether this is intentional behaviour or not - might be worth raising with elastic ...





Thanks for your comment! I have edited the question to post more info.
– Roberto
Aug 16 at 16:11






Have edited my answer, though it is admittedly somewhat unfinished
– ifo20
Aug 16 at 21:29






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered