# Function score with nested docs stops working after adding attribute with not unique name

**URL:** <https://discuss.elastic.co/t/function-score-with-nested-docs-stops-working-after-adding-attribute-with-not-unique-name/19746>\
**Category:** Elasticsearch\
**Created:** [September 11, 2014, 9:41pm UTC](https://discuss.elastic.co/t/function-score-with-nested-docs-stops-working-after-adding-attribute-with-not-unique-name/19746 "2014-09-11T21:41:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gosia](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@Gosia](https://discuss.elastic.co/u/Gosia)\
**Post date:** [September 11, 2014, 9:41pm UTC](https://discuss.elastic.co/t/function-score-with-nested-docs-stops-working-after-adding-attribute-with-not-unique-name/19746/1 "2014-09-11T21:41:45Z")

</div>

Hi guys,

I am trying to calculate the `_score` of the document as a sum of IDs of my  
nested docs.

Here is the mapping:

```auto
PUT /test/property/_mapping
{
   "property": {
      "properties": {
         "destination_id": {
            "type": "long"
         },
         "rates": {
            "type": "nested",
            "properties": {
               "id": {
                  "type": "long"
               },
               "end_date": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },
               "start_date": {
                  "type": "date",
                  "format": "dateOptionalTime"
               }
            }
         }
      }
   }
} 

```

and here is my query:

```auto
GET /test/property/_search
{
   "query": {
      "filtered": {
         "query": {
            "nested": {
               "path": "rates",
               "score_mode": "sum",
               "query": {
                  "function_score": {
                     "functions": [
                        {
                           "script_score": {
                              "script": "doc['id'].value"
                           }
                        }
                     ]
                  }
               }
            }
         }
      }
   }
}

```

It works fine and score is calculated properly when I don't have field `id`  
in my main (top level) document.  
Something unexpected happens though as soon as I add document constructed  
that way:

```auto
POST /test/property/3
{
   "destination_id": 1,
   "id": 12,
   "rates": [
      {
         "id": 9,
         "end_date": "2014-10-15"
      },
      {
         "id": 10,
         "end_date": "2014-10-20"
      }
   ]
}

```

after that all my `_scores` are reduced to 0.  
Seems like the top attribute kind of 'overshadows' the nested ones...  
anyway shouldn't happen as I explicitly use nested path?  
I wonder why is that happening and how can I make sure that one incorrectly  
indexed document will not have such an impact on the search query?

Thank you,  
Gosia

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ef1c870b-080c-41ec-99c7-5854d6c5a54d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ef1c870b-080c-41ec-99c7-5854d6c5a54d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:02am UTC](https://discuss.elastic.co/t/function-score-with-nested-docs-stops-working-after-adding-attribute-with-not-unique-name/19746/2 "2017-07-06T01:02:58Z")

</div>


