Search in sources :

Example 41 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class MatchedQueriesIT method testRegExpQuerySupportsName.

public void testRegExpQuerySupportsName() {
    createIndex("test1");
    ensureGreen();
    client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.regexpQuery("title", "title1").queryName("regex")).get();
    assertHitCount(searchResponse, 1L);
    for (SearchHit hit : searchResponse.getHits()) {
        if (hit.getId().equals("1")) {
            assertThat(hit.getMatchedQueries().length, equalTo(1));
            assertThat(hit.getMatchedQueries(), hasItemInArray("regex"));
        } else {
            fail("Unexpected document returned with id " + hit.getId());
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 42 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class MatchedQueriesIT method testPrefixQuerySupportsName.

public void testPrefixQuerySupportsName() {
    createIndex("test1");
    ensureGreen();
    client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.prefixQuery("title", "title").queryName("prefix")).get();
    assertHitCount(searchResponse, 1L);
    for (SearchHit hit : searchResponse.getHits()) {
        if (hit.getId().equals("1")) {
            assertThat(hit.getMatchedQueries().length, equalTo(1));
            assertThat(hit.getMatchedQueries(), hasItemInArray("prefix"));
        } else {
            fail("Unexpected document returned with id " + hit.getId());
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 43 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class MatchedQueriesIT method testMatchedWithShould.

/**
     * Test case for issue #4361: https://github.com/elastic/elasticsearch/issues/4361
     */
public void testMatchedWithShould() throws Exception {
    createIndex("test");
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource("content", "Lorem ipsum dolor sit amet").get();
    client().prepareIndex("test", "type1", "2").setSource("content", "consectetur adipisicing elit").get();
    refresh();
    // Execute search at least two times to load it in cache
    int iter = scaledRandomIntBetween(2, 10);
    for (int i = 0; i < iter; i++) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery().minimumShouldMatch(1).should(queryStringQuery("dolor").queryName("dolor")).should(queryStringQuery("elit").queryName("elit"))).setPreference("_primary").get();
        assertHitCount(searchResponse, 2L);
        for (SearchHit hit : searchResponse.getHits()) {
            if (hit.getId().equals("1")) {
                assertThat(hit.getMatchedQueries().length, equalTo(1));
                assertThat(hit.getMatchedQueries(), hasItemInArray("dolor"));
            } else if (hit.getId().equals("2")) {
                assertThat(hit.getMatchedQueries().length, equalTo(1));
                assertThat(hit.getMatchedQueries(), hasItemInArray("elit"));
            } else {
                fail("Unexpected document returned with id " + hit.getId());
            }
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 44 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class MatchedQueriesIT method testSimpleMatchedQueryFromTopLevelFilter.

public void testSimpleMatchedQueryFromTopLevelFilter() throws Exception {
    createIndex("test");
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource("name", "test", "title", "title1").get();
    client().prepareIndex("test", "type1", "2").setSource("name", "test").get();
    client().prepareIndex("test", "type1", "3").setSource("name", "test").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(boolQuery().should(termQuery("name", "test").queryName("name")).should(termQuery("title", "title1").queryName("title"))).get();
    assertHitCount(searchResponse, 3L);
    for (SearchHit hit : searchResponse.getHits()) {
        if (hit.getId().equals("1")) {
            assertThat(hit.getMatchedQueries().length, equalTo(2));
            assertThat(hit.getMatchedQueries(), hasItemInArray("name"));
            assertThat(hit.getMatchedQueries(), hasItemInArray("title"));
        } else if (hit.getId().equals("2") || hit.getId().equals("3")) {
            assertThat(hit.getMatchedQueries().length, equalTo(1));
            assertThat(hit.getMatchedQueries(), hasItemInArray("name"));
        } else {
            fail("Unexpected document returned with id " + hit.getId());
        }
    }
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(boolQuery().should(termQuery("name", "test").queryName("name")).should(termQuery("title", "title1").queryName("title"))).get();
    assertHitCount(searchResponse, 3L);
    for (SearchHit hit : searchResponse.getHits()) {
        if (hit.getId().equals("1")) {
            assertThat(hit.getMatchedQueries().length, equalTo(2));
            assertThat(hit.getMatchedQueries(), hasItemInArray("name"));
            assertThat(hit.getMatchedQueries(), hasItemInArray("title"));
        } else if (hit.getId().equals("2") || hit.getId().equals("3")) {
            assertThat(hit.getMatchedQueries().length, equalTo(1));
            assertThat(hit.getMatchedQueries(), hasItemInArray("name"));
        } else {
            fail("Unexpected document returned with id " + hit.getId());
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 45 with SearchHit

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class MatchedQueriesIT method testWildcardQuerySupportsName.

public void testWildcardQuerySupportsName() {
    createIndex("test1");
    ensureGreen();
    client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.wildcardQuery("title", "titl*").queryName("wildcard")).get();
    assertHitCount(searchResponse, 1L);
    for (SearchHit hit : searchResponse.getHits()) {
        if (hit.getId().equals("1")) {
            assertThat(hit.getMatchedQueries().length, equalTo(1));
            assertThat(hit.getMatchedQueries(), hasItemInArray("wildcard"));
        } else {
            fail("Unexpected document returned with id " + hit.getId());
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)318 SearchResponse (org.elasticsearch.action.search.SearchResponse)225 SearchHits (org.elasticsearch.search.SearchHits)87 ArrayList (java.util.ArrayList)83 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)64 HashMap (java.util.HashMap)37 Map (java.util.Map)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)37 IOException (java.io.IOException)35 Test (org.junit.Test)32 TimeValue (org.elasticsearch.common.unit.TimeValue)29 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)22 List (java.util.List)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)17 SearchHitField (org.elasticsearch.search.SearchHitField)16 HashSet (java.util.HashSet)15 Text (org.elasticsearch.common.text.Text)14