Search in sources :

Example 6 with MultiMatchQueryBuilder

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")));
    }
}
Also used : Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) Matchers.containsString(org.hamcrest.Matchers.containsString) MultiMatchQueryBuilder(org.opensearch.index.query.MultiMatchQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with MultiMatchQueryBuilder

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"))));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MultiPhrasePrefixQuery(org.opensearch.common.lucene.search.MultiPhrasePrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) FieldMaskingSpanQuery(org.apache.lucene.queries.spans.FieldMaskingSpanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) QueryShardContext(org.opensearch.index.query.QueryShardContext) MultiMatchQueryBuilder(org.opensearch.index.query.MultiMatchQueryBuilder)

Aggregations

MultiMatchQueryBuilder (org.opensearch.index.query.MultiMatchQueryBuilder)7 SearchResponse (org.opensearch.action.search.SearchResponse)4 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)2 Query (org.apache.lucene.search.Query)2 SynonymQuery (org.apache.lucene.search.SynonymQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 MatchQueryBuilder (org.opensearch.index.query.MatchQueryBuilder)2 Collection (java.util.Collection)1 FieldMaskingSpanQuery (org.apache.lucene.queries.spans.FieldMaskingSpanQuery)1 SpanNearQuery (org.apache.lucene.queries.spans.SpanNearQuery)1 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 PhraseQuery (org.apache.lucene.search.PhraseQuery)1 TimeRange (org.bedework.caldav.util.TimeRange)1 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)1