Merge pull request #20400 from jimferenczi/function_score_highlight
Fix highlighting of MultiTermQuery within a FunctionScoreQuery
This commit is contained in:
commit
82fd95fd24
|
@ -78,10 +78,7 @@ public final class CustomQueryScorer extends QueryScorer {
|
||||||
@Override
|
@Override
|
||||||
protected void extractUnknownQuery(Query query,
|
protected void extractUnknownQuery(Query query,
|
||||||
Map<String, WeightedSpanTerm> terms) throws IOException {
|
Map<String, WeightedSpanTerm> terms) throws IOException {
|
||||||
if (query instanceof FunctionScoreQuery) {
|
if (query instanceof FiltersFunctionScoreQuery) {
|
||||||
query = ((FunctionScoreQuery) query).getSubQuery();
|
|
||||||
extract(query, 1F, terms);
|
|
||||||
} else if (query instanceof FiltersFunctionScoreQuery) {
|
|
||||||
query = ((FiltersFunctionScoreQuery) query).getSubQuery();
|
query = ((FiltersFunctionScoreQuery) query).getSubQuery();
|
||||||
extract(query, 1F, terms);
|
extract(query, 1F, terms);
|
||||||
} else if (terms.isEmpty()) {
|
} else if (terms.isEmpty()) {
|
||||||
|
@ -97,9 +94,11 @@ public final class CustomQueryScorer extends QueryScorer {
|
||||||
} else if (query instanceof HasChildQueryBuilder.LateParsingQuery) {
|
} else if (query instanceof HasChildQueryBuilder.LateParsingQuery) {
|
||||||
// skip has_child or has_parent queries, see: https://github.com/elastic/elasticsearch/issues/14999
|
// skip has_child or has_parent queries, see: https://github.com/elastic/elasticsearch/issues/14999
|
||||||
return;
|
return;
|
||||||
}
|
} else if (query instanceof FunctionScoreQuery) {
|
||||||
|
super.extract(((FunctionScoreQuery) query).getSubQuery(), boost, terms);
|
||||||
|
} else {
|
||||||
super.extract(query, boost, terms);
|
super.extract(query, boost, terms);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder;
|
||||||
import org.elasticsearch.index.query.Operator;
|
import org.elasticsearch.index.query.Operator;
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
||||||
import org.elasticsearch.index.search.MatchQuery;
|
import org.elasticsearch.index.search.MatchQuery;
|
||||||
import org.elasticsearch.plugins.Plugin;
|
import org.elasticsearch.plugins.Plugin;
|
||||||
import org.elasticsearch.rest.RestStatus;
|
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()[0].string(), equalTo("<em>brown</em>"));
|
||||||
assertThat(field.getFragments()[1].string(), equalTo("<em>cow</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>"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue