# Unexpected search response for nested objects

**URL:** <https://discuss.elastic.co/t/unexpected-search-response-for-nested-objects/195388>\
**Category:** Elasticsearch\
**Created:** [August 15, 2019, 8:08pm UTC](https://discuss.elastic.co/t/unexpected-search-response-for-nested-objects/195388 "2019-08-15T20:08:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bterhorst](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@bterhorst](https://discuss.elastic.co/u/bterhorst)\
**Post date:** [August 15, 2019, 8:08pm UTC](https://discuss.elastic.co/t/unexpected-search-response-for-nested-objects/195388/1 "2019-08-15T20:08:17Z")

</div>

**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**

```auto
   ...
      "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".**

```auto
{
 ...

      "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

---

<div class="post-metadata">

**Author:** ![bterhorst](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@bterhorst](https://discuss.elastic.co/u/bterhorst)\
**Post date:** [August 16, 2019, 6:54am UTC](https://discuss.elastic.co/t/unexpected-search-response-for-nested-objects/195388/2 "2019-08-16T06:54:00Z")

</div>

It looks like the document with 2 nested objects some how influences the score even though it does not match the search query.  
Why?

Regards Benny

---

<div class="post-metadata">

**Author:** ![bterhorst](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@bterhorst](https://discuss.elastic.co/u/bterhorst)\
**Post date:** [August 17, 2019, 12:22pm UTC](https://discuss.elastic.co/t/unexpected-search-response-for-nested-objects/195388/3 "2019-08-17T12:22:32Z")

</div>

Problem solved!  
After updating to elasticsearch v7.3.0 expected results are retrieved.

Regards Benny

FYI: Unexpected result were in versions 6.x.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 14, 2019, 12:22pm UTC](https://discuss.elastic.co/t/unexpected-search-response-for-nested-objects/195388/4 "2019-09-14T12:22:33Z")

</div>

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