use of org.opensearch.index.query.MultiMatchQueryBuilder in project OpenSearch by opensearch-project.
the class HighlighterSearchIT method testMultiMatchQueryHighlight.
public void testMultiMatchQueryHighlight() throws IOException {
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("field1").field("type", "text").field("index_options", "offsets").field("term_vector", "with_positions_offsets").endObject().startObject("field2").field("type", "text").field("index_options", "offsets").field("term_vector", "with_positions_offsets").endObject().endObject().endObject();
assertAcked(prepareCreate("test").setMapping(mapping));
ensureGreen();
client().prepareIndex("test").setSource("field1", "The quick brown fox jumps over", "field2", "The quick brown fox jumps over").get();
refresh();
final int iters = scaledRandomIntBetween(20, 30);
for (int i = 0; i < iters; i++) {
String highlighterType = rarely() ? null : RandomPicks.randomFrom(random(), ALL_TYPES);
MultiMatchQueryBuilder.Type matchQueryType = RandomPicks.randomFrom(random(), MultiMatchQueryBuilder.Type.values());
MultiMatchQueryBuilder multiMatchQueryBuilder = multiMatchQuery("the quick brown fox", "field1", "field2").type(matchQueryType);
SearchSourceBuilder source = searchSource().query(multiMatchQueryBuilder).highlighter(highlight().highlightQuery(randomBoolean() ? multiMatchQueryBuilder : null).highlighterType(highlighterType).field(new Field("field1").requireFieldMatch(true).preTags("<field1>").postTags("</field1>")));
logger.info("Running multi-match type: [{}] highlight with type: [{}]", matchQueryType, highlighterType);
SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet();
assertHitCount(searchResponse, 1L);
assertHighlight(searchResponse, 0, "field1", 0, anyOf(equalTo("<field1>The quick brown fox</field1> jumps over"), equalTo("<field1>The</field1> <field1>quick</field1> <field1>brown</field1> <field1>fox</field1> jumps over")));
}
}
use of org.opensearch.index.query.MultiMatchQueryBuilder in project OpenSearch by opensearch-project.
the class SearchAsYouTypeFieldMapperTests method testMultiMatchBoolPrefix.
public void testMultiMatchBoolPrefix() throws IOException {
QueryShardContext queryShardContext = createQueryShardContext(createMapperService(fieldMapping(b -> b.field("type", "search_as_you_type").field("max_shingle_size", 4))));
MultiMatchQueryBuilder builder = new MultiMatchQueryBuilder("quick brown fox jump lazy dog", "field", "field._2gram", "field._3gram", "field._4gram");
builder.type(MultiMatchQueryBuilder.Type.BOOL_PREFIX);
final Query actual = builder.toQuery(queryShardContext);
assertThat(actual, instanceOf(DisjunctionMaxQuery.class));
final DisjunctionMaxQuery disMaxQuery = (DisjunctionMaxQuery) actual;
assertThat(disMaxQuery.getDisjuncts(), hasSize(4));
assertThat(disMaxQuery.getDisjuncts(), containsInAnyOrder(buildBoolPrefixQuery("field", "field._index_prefix", asList("quick", "brown", "fox", "jump", "lazy", "dog")), buildBoolPrefixQuery("field._2gram", "field._index_prefix", asList("quick brown", "brown fox", "fox jump", "jump lazy", "lazy dog")), buildBoolPrefixQuery("field._3gram", "field._index_prefix", asList("quick brown fox", "brown fox jump", "fox jump lazy", "jump lazy dog")), buildBoolPrefixQuery("field._4gram", "field._index_prefix", asList("quick brown fox jump", "brown fox jump lazy", "fox jump lazy dog"))));
}
Aggregations