Skip to content

Commit

Permalink
Fix highlighter support in PinnedQuery and added test (elastic#53716)
Browse files Browse the repository at this point in the history
  • Loading branch information
markharwood committed Mar 18, 2020
1 parent d56dee5 commit 9062d7d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public Query getQuery() {
return query;
}

@Override
public void visit(QueryVisitor visitor) {
query.visit(visitor.getSubVisitor(BooleanClause.Occur.MUST, this));
}

@Override
public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = query.rewrite(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.test.ESIntegTestCase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.action.search.SearchType.DFS_QUERY_THEN_FETCH;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
Expand Down Expand Up @@ -197,5 +200,29 @@ public void testExplain() throws Exception {


}

public void testHighlight() throws Exception {
// Issue raised in https://github.com/elastic/elasticsearch/issues/53699
assertAcked(prepareCreate("test").setMapping(
jsonBuilder().startObject().startObject("_doc").startObject("properties").startObject("field1")
.field("analyzer", "whitespace").field("type", "text").endObject().endObject().endObject().endObject()));
ensureGreen();
client().prepareIndex("test").setId("1").setSource("field1", "the quick brown fox").get();
refresh();

PinnedQueryBuilder pqb = new PinnedQueryBuilder(QueryBuilders.matchQuery("field1", "the quick brown").operator(Operator.OR), "2");

HighlightBuilder testHighlighter = new HighlightBuilder();
testHighlighter.field("field1");

SearchResponse searchResponse = client().prepareSearch().setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(pqb)
.highlighter(testHighlighter)
.setExplain(true).get();
assertHitCount(searchResponse, 1);
Map<String, HighlightField> highlights = searchResponse.getHits().getHits()[0].getHighlightFields();
assertThat(highlights.size(), equalTo(1));
HighlightField highlight = highlights.get("field1");
assertThat(highlight.fragments()[0].toString(), equalTo("<em>the</em> <em>quick</em> <em>brown</em> fox"));
}
}

0 comments on commit 9062d7d

Please sign in to comment.