# Score is 0 when using function score on nested documents

**URL:** <https://discuss.elastic.co/t/score-is-0-when-using-function-score-on-nested-documents/49936>\
**Category:** Elasticsearch\
**Created:** [May 12, 2016, 8:27pm UTC](https://discuss.elastic.co/t/score-is-0-when-using-function-score-on-nested-documents/49936 "2016-05-12T20:27:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barak\_Yaish](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Barak\_Yaish](https://discuss.elastic.co/u/Barak_Yaish)\
**Post date:** [May 12, 2016, 8:27pm UTC](https://discuss.elastic.co/t/score-is-0-when-using-function-score-on-nested-documents/49936/1 "2016-05-12T20:27:30Z")

</div>

Hi,

Having the following mapping, 'reviews.positive.aspects' is a nested array:

```
{
   "template":"products*",
   "mappings":{
   "Product":{
     "dynamic":"false",
     "properties":{
        "id":{
           "type":"long"
        },
        "name":{
           "type":"string",
           "analyzer":"standard"
        },
        "score":{
           "type":"double"
        },
        "reviews":{
           "type":"nested",
           "dynamic":"false",
           "properties":{
              "positive":{
                 "type":"nested",
                 "dynamic":"false",
                 "properties":{
                    "reviewCount":{
                       "type":"integer"
                    },
                    "quote":{
                       "type":"string",
                       "analyzer":"standard"
                    },
                    "aspects":{
                       "type":"nested",
                       "dynamic":"false",
                       "properties":{
                          "id":{
                             "type":"long"
                          },
                          "name":{
                             "type":"string",
                             "analyzer":"standard"
                          },
                          "score":{
                             "type":"double"
                          },
                          "frequency":{
                             "type":"integer"
                          }
                       }
                    }
                 }
              }
           }
        }
     }
  }
   }
}

```

I'm trying to set a query to match documents with with score higher than X, aspects.score higher than Y and [aspects.id](http://aspects.id) equals to Z, and results sorted by aspects.frequency. I'm using FunctionScoreQuery, but all scores in the results are 0. Here is my code:

```
        AndFilterBuilder aspectFilter = andFilter( termFilter( "reviews.positive.aspects.id", aspectId ), rangeFilter( "reviews.positive.aspects.score" ).from( aspectScore ).includeLower( true ) );

	NestedFilterBuilder nestedAspectFilter = nestedFilter( "reviews.positive.aspects", filteredQuery( matchAllQuery(), aspectFilter ) );

	RangeFilterBuilder productScoreFilter = rangeFilter( "score" ).from( productScore ).includeLower( true );

	QueryBuilder productScoreQuery = filteredQuery( matchAllQuery(), andFilter( productScoreFilter ) );

	QueryBuilder aspectScoreQuery = QueryBuilders.nestedQuery( "reviews", filteredQuery( matchAllQuery(), nestedAspectFilter ) );
	
	String inlineScript = "doc['reviews.positive.aspects.frequency'].value";
	
	ScoreFunctionBuilder sfb = ScoreFunctionBuilders.scriptFunction( inlineScript, "groovy" );
	
	FunctionScoreQueryBuilder functionScore = QueryBuilders.functionScoreQuery( boolQuery().must( productScoreQuery ).must( aspectScoreQuery ), nestedAspectFilter, sfb );

            client.prepareSearch( "products" ).
                 setQuery( functionScore ).
                  addSort( SortBuilders.scoreSort().order( SortOrder.DESC) ).
                  setSearchType( SearchType.QUERY_THEN_FETCH ).
	      get();

```

The explain looks as follow (similar to all docs):

```
 0.0 = (MATCH) function score, product of:
  0.0 = (MATCH) Math.min of
    0.0 = script score function, computed with script:"doc['reviews.positive.aspects.frequency'].value
      1.4142135 = _score: 
        1.4142135 = (MATCH) sum of:
          0.70710677 = (MATCH) ConstantScore(+cache(score:[8.0 TO *]) , product of:
            1.0 = boost
            0.70710677 = queryNorm
          0.70710677 = (MATCH) Score based on child doc range from 110571 to 110598
    3.4028235E38 = maxBoost
  1.0 = queryBoost

```

Documents in the results indeed match the filtering criteria, but not sorted (and score is 0). Can you direct me how should I fix the code?

Thanks!

---

<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 5, 2017, 10:51pm UTC](https://discuss.elastic.co/t/score-is-0-when-using-function-score-on-nested-documents/49936/2 "2017-07-05T22:51:56Z")

</div>


