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