Merge pull request #20400 from jimferenczi/function_score_highlight

Fix highlighting of MultiTermQuery within a FunctionScoreQuery
This commit is contained in:
Jim Ferenczi 2016-09-12 15:56:06 +02:00 committed by GitHub
commit 82fd95fd24
2 changed files with 23 additions and 6 deletions

View File

@ -78,10 +78,7 @@ public final class CustomQueryScorer extends QueryScorer {
@Override
protected void extractUnknownQuery(Query query,
Map<String, WeightedSpanTerm> terms) throws IOException {
if (query instanceof FunctionScoreQuery) {
query = ((FunctionScoreQuery) query).getSubQuery();
extract(query, 1F, terms);
} else if (query instanceof FiltersFunctionScoreQuery) {
if (query instanceof FiltersFunctionScoreQuery) {
query = ((FiltersFunctionScoreQuery) query).getSubQuery();
extract(query, 1F, terms);
} else if (terms.isEmpty()) {
@ -97,9 +94,11 @@ public final class CustomQueryScorer extends QueryScorer {
} else if (query instanceof HasChildQueryBuilder.LateParsingQuery) {
// skip has_child or has_parent queries, see: https://github.com/elastic/elasticsearch/issues/14999
return;
} else if (query instanceof FunctionScoreQuery) {
super.extract(((FunctionScoreQuery) query).getSubQuery(), boost, terms);
} else {
super.extract(query, boost, terms);
}
super.extract(query, boost, terms);
}
}
}

View File

@ -38,6 +38,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestStatus;
@ -2851,4 +2852,21 @@ public class HighlighterSearchIT extends ESIntegTestCase {
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
assertThat(field.getFragments()[1].string(), equalTo("<em>cow</em>"));
}
public void testFunctionScoreQueryHighlight() throws Exception {
client().prepareIndex("test", "type", "1")
.setSource(jsonBuilder().startObject().field("text", "brown").endObject())
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
.get();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(new FunctionScoreQueryBuilder(QueryBuilders.prefixQuery("text", "bro")))
.highlighter(new HighlightBuilder()
.field(new Field("text")))
.get();
assertHitCount(searchResponse, 1);
HighlightField field = searchResponse.getHits().getAt(0).highlightFields().get("text");
assertThat(field.getFragments().length, equalTo(1));
assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
}
}