Search in sources :

Example 1 with MultiMatchQueryBuilder

use of org.elasticsearch.index.query.MultiMatchQueryBuilder in project elasticsearch by elastic.

the class MultiMatchQueryIT method testSingleField.

public void testSingleField() throws NoSuchFieldException, IllegalAccessException {
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(randomizeType(multiMatchQuery("15", "skill"))).get();
    assertNoFailures(searchResponse);
    assertFirstHit(searchResponse, hasId("theone"));
    searchResponse = client().prepareSearch("test").setQuery(randomizeType(multiMatchQuery("15", "skill", "int-field")).analyzer("category")).get();
    assertNoFailures(searchResponse);
    assertFirstHit(searchResponse, hasId("theone"));
    String[] fields = { "full_name", "first_name", "last_name", "last_name_phrase", "first_name_phrase", "category_phrase", "category", "missing_field", "missing_fields*" };
    String[] query = { "marvel", "hero", "captain", "america", "15", "17", "1", "5", "ultimate", "Man", "marvel", "wolferine", "ninja" };
    // check if it's equivalent to a match query.
    int numIters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < numIters; i++) {
        String field = RandomPicks.randomFrom(random(), fields);
        int numTerms = randomIntBetween(1, query.length);
        StringBuilder builder = new StringBuilder();
        for (int j = 0; j < numTerms; j++) {
            builder.append(RandomPicks.randomFrom(random(), query)).append(" ");
        }
        MultiMatchQueryBuilder multiMatchQueryBuilder = randomizeType(multiMatchQuery(builder.toString(), field));
        SearchResponse multiMatchResp = client().prepareSearch("test").addSort("_score", SortOrder.DESC).addSort("_uid", SortOrder.ASC).setQuery(multiMatchQueryBuilder).get();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, builder.toString());
        if (multiMatchQueryBuilder.getType() != null) {
            matchQueryBuilder.type(MatchQuery.Type.valueOf(multiMatchQueryBuilder.getType().matchQueryType().toString()));
        }
        SearchResponse matchResp = client().prepareSearch("test").addSort("_score", SortOrder.DESC).addSort("_uid", SortOrder.ASC).setQuery(matchQueryBuilder).get();
        assertThat("field: " + field + " query: " + builder.toString(), multiMatchResp.getHits().getTotalHits(), equalTo(matchResp.getHits().getTotalHits()));
        SearchHits hits = multiMatchResp.getHits();
        if (field.startsWith("missing")) {
            assertEquals(0, hits.getHits().length);
        }
        for (int j = 0; j < hits.getHits().length; j++) {
            assertThat(hits.getHits()[j].getScore(), equalTo(matchResp.getHits().getHits()[j].getScore()));
            assertThat(hits.getHits()[j].getId(), equalTo(matchResp.getHits().getHits()[j].getId()));
        }
    }
}
Also used : MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with MultiMatchQueryBuilder

use of org.elasticsearch.index.query.MultiMatchQueryBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testMultiMatchQueryMinShouldMatch.

public void testMultiMatchQueryMinShouldMatch() {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("field1", new String[] { "value1", "value2", "value3" }).get();
    client().prepareIndex("test", "type1", "2").setSource("field2", "value1").get();
    refresh();
    MultiMatchQueryBuilder multiMatchQuery = multiMatchQuery("value1 value2 foo", "field1", "field2");
    multiMatchQuery.useDisMax(true);
    multiMatchQuery.minimumShouldMatch("70%");
    SearchResponse searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    multiMatchQuery.minimumShouldMatch("30%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasId("1"));
    assertSecondHit(searchResponse, hasId("2"));
    multiMatchQuery.useDisMax(false);
    multiMatchQuery.minimumShouldMatch("70%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    multiMatchQuery.minimumShouldMatch("30%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasId("1"));
    assertSecondHit(searchResponse, hasId("2"));
    multiMatchQuery = multiMatchQuery("value1 value2 bar", "field1");
    multiMatchQuery.minimumShouldMatch("100%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 0L);
    multiMatchQuery.minimumShouldMatch("70%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    // Min should match > # optional clauses returns no docs.
    multiMatchQuery = multiMatchQuery("value1 value2 value3", "field1", "field2");
    multiMatchQuery.minimumShouldMatch("4");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 0L);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with MultiMatchQueryBuilder

use of org.elasticsearch.index.query.MultiMatchQueryBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testMultiMatchQuery.

public void testMultiMatchQuery() throws Exception {
    createIndex("test");
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value4", "field3", "value3"), client().prepareIndex("test", "type1", "2").setSource("field1", "value2", "field2", "value5", "field3", "value2"), client().prepareIndex("test", "type1", "3").setSource("field1", "value3", "field2", "value6", "field3", "value1"));
    MultiMatchQueryBuilder builder = multiMatchQuery("value1 value2 value4", "field1", "field2");
    SearchResponse searchResponse = client().prepareSearch().setQuery(builder).addAggregation(AggregationBuilders.terms("field1").field("field1.keyword")).get();
    assertHitCount(searchResponse, 2L);
    // this uses dismax so scores are equal and the order can be arbitrary
    assertSearchHits(searchResponse, "1", "2");
    builder.useDisMax(false);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "2");
    client().admin().indices().prepareRefresh("test").get();
    builder = multiMatchQuery("value1", "field1", "field2").operator(// Operator only applies on terms inside a field! Fields are always OR-ed together.
    Operator.AND);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    refresh();
    builder = multiMatchQuery("value1", "field1").field("field3", 1.5f).operator(// Operator only applies on terms inside a field! Fields are always OR-ed together.
    Operator.AND);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "3", "1");
    client().admin().indices().prepareRefresh("test").get();
    builder = multiMatchQuery("value1").field("field1").field("field3", 1.5f).operator(// Operator only applies on terms inside a field! Fields are always OR-ed together.
    Operator.AND);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "3", "1");
    // Test lenient
    client().prepareIndex("test", "type1", "3").setSource("field1", "value7", "field2", "value8", "field4", 5).get();
    refresh();
    builder = multiMatchQuery("value1", "field1", "field2", "field4");
    assertFailures(client().prepareSearch().setQuery(builder), RestStatus.BAD_REQUEST, containsString("NumberFormatException[For input string: \"value1\"]"));
    builder.lenient(true);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
}
Also used : MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with MultiMatchQueryBuilder

use of org.elasticsearch.index.query.MultiMatchQueryBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method testMultiMatchQueryHighlight.

public void testMultiMatchQueryHighlight() throws IOException {
    String[] highlighterTypes = new String[] { "fvh", "plain", "postings", "unified" };
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").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().endObject();
    assertAcked(prepareCreate("test").addMapping("type1", mapping));
    ensureGreen();
    client().prepareIndex("test", "type1").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(), highlighterTypes);
        MultiMatchQueryBuilder.Type[] supportedQueryTypes;
        if ("postings".equals(highlighterType)) {
            /*
                 * phrase_prefix is not supported by postings highlighter, as it rewrites against an empty reader, the prefix will never
                 * match any term
                 */
            supportedQueryTypes = new MultiMatchQueryBuilder.Type[] { MultiMatchQueryBuilder.Type.BEST_FIELDS, MultiMatchQueryBuilder.Type.CROSS_FIELDS, MultiMatchQueryBuilder.Type.MOST_FIELDS, MultiMatchQueryBuilder.Type.PHRASE };
        } else {
            supportedQueryTypes = MultiMatchQueryBuilder.Type.values();
        }
        MultiMatchQueryBuilder.Type matchQueryType = RandomPicks.randomFrom(random(), supportedQueryTypes);
        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.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) BoundaryScannerType(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.BoundaryScannerType) XContentType(org.elasticsearch.common.xcontent.XContentType) Matchers.containsString(org.hamcrest.Matchers.containsString) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)4 MultiMatchQueryBuilder (org.elasticsearch.index.query.MultiMatchQueryBuilder)4 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)3 Matchers.containsString (org.hamcrest.Matchers.containsString)2 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)1 SearchHits (org.elasticsearch.search.SearchHits)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 BoundaryScannerType (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.BoundaryScannerType)1 Field (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)1 ElasticsearchAssertions.assertSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits)1