Step 1: Create mapping nested document:
curl -XPUT --header 'Content-Type: application/json' http://es:9200/search -d '{"mappings" : { "hometable" : { "properties":{ "recipes":{ "type":"nested", "properties":{ "id":{ "type":"text" }, "description":{ "type":"text" } } } } } } }'
Step 2: Insert nested doc. 1:
curl -H "Content-Type: application/json" -XPUT "http://es:9200/search/hometable/1" -d '{"recipes":[{ "description":"the green curry is fresh and hot","id":"1" }, { "description":"the pasta saus is creamy", "id":"2" }] }'
Step 3: Insert nested doc. 2:
curl -H "Content-Type: application/json" -XPUT "http://es:9200/search/hometable/2" -d '{"recipes": [{ "description":"red curry full of wonderful spices", "id":"1" }] }'
,
Step 4: Search request:
curl -XGET --header 'Content-Type: application/json' http://es:9200/search/_search/?pretty=true -d '{"query":{ "bool":{ "must":[{ "nested":{ "query":{ "bool":{ "should":[{ "match":{ "recipes.description":{ "query": "red curry" } } }] } }, "path":"recipes", "inner_hits":{} } }] } } }'
Result 1: Search response: red curry has the lower score
...
"hits" : {
"total" : 2,
"max_score" : 0.6489038,
"hits" : [
{
...
"_id" : "1",
"_score" : 0.6489038,
"_source" : {
"recipes" : [
{
"description" : "the green curry is fresh and hot",
"id" : "1"
},
{
"description" : "the pasta saus is creamy",
"id" : "2"
}
]
},
"inner_hits" : {
"recipes" : {
"hits" : {
"total" : 1,
"max_score" : 0.6489038,
"hits" : [
{
...
"_id" : "1",
"_nested" : {
"field" : "recipes"
},
"_score" : 0.6489038,
"_source" : {
"description" : "the green curry is fresh and hot",
"id" : "1"
}
}
]
}
}
}
},
{
...
"_id" : "2",
"_score" : 0.5753642,
"_source" : {
"recipes" : [
{
"description" : "red curry full of wonderful spices",
"id" : "1"
}
]
},
"inner_hits" : {
"recipes" : {
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "search",
"_type" : "hometable",
"_id" : "2",
"_nested" : {
"field" : "recipes"
},
"_score" : 0.5753642,
"_source" : {
"description" : "red curry full of wonderful spices",
"id" : "1"
}
}
]
...
}
The following test shows this is probably due to the 2nd nested doc. that some how influences the overall score.
Step 5: Update nested document 1 (remove 2 nested document):
curl -H "Content-Type: application/json" -XPUT "http://es:9200/search/hometable/1" -d '{"recipes":[{ "description":"the green curry is fresh and hot","id":"1" }] }'
Result 2: Search response for the previous search request: the expected result is returned with the higher score for "red curry".
{
...
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
..
"_id" : "2",
"_score" : 0.5753642,
"_source" : {
"recipes" : [
{
"description" : "red curry full of wonderful spices",
"id" : "1"
}
]
},
"inner_hits" : {
"recipes" : {
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
...
"_id" : "2",
"_nested" : {
"field" : "recipes",
"offset" : 0
},
"_score" : 0.5753642,
"_source" : {
"description" : "red curry full of wonderful spices",
"id" : "1"
}
}
]
}
}
}
},
{
...
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"recipes" : [
{
"description" : "the green curry is fresh and hot",
"id" : "1"
}
]
},
"inner_hits" : {
"recipes" : {
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
...
"_nested" : {
"field" : "recipes"
},
"_score" : 0.2876821,
"_source" : {
"description" : "the green curry is fresh and hot",
"id" : "1"
}
}
]
}
...
}
Question
Why does "red curry" have the lower score when its the better match as can be seen in result 1"
What can/must I do in order to achieve the excepted result?
Any help would greatly be appreciated!
Regards Benny