Search in sources :

Example 21 with SearchHit

use of 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 22 with SearchHit

use of 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 23 with SearchHit

use of 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)

Example 24 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class MatchedQueriesIT method testSimpleMatchedQueryFromTopLevelFilterAndFilteredQuery.

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

Example 25 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class MultiMatchQueryIT method testFuzzyFieldLevelBoosting.

/**
     * Test for edge case where field level boosting is applied to field that doesn't exist on documents on
     * one shard. There was an issue reported in https://github.com/elastic/elasticsearch/issues/18710 where a
     * `multi_match` query using the fuzziness parameter with a boost on one of two fields returns the
     * same document score if both documents are placed on different shard. This test recreates that scenario
     * and checks that the returned scores are different.
     */
public void testFuzzyFieldLevelBoosting() throws InterruptedException, ExecutionException {
    String idx = "test18710";
    CreateIndexRequestBuilder builder = prepareCreate(idx).setSettings(Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, 3).put(SETTING_NUMBER_OF_REPLICAS, 0));
    assertAcked(builder.addMapping("type", "title", "type=text", "body", "type=text"));
    ensureGreen();
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex(idx, "type", "1").setSource("title", "foo", "body", "bar"));
    builders.add(client().prepareIndex(idx, "type", "2").setSource("title", "bar", "body", "foo"));
    indexRandom(true, false, builders);
    SearchResponse searchResponse = client().prepareSearch(idx).setExplain(true).setQuery(multiMatchQuery("foo").field("title", 100).field("body").fuzziness(0)).get();
    SearchHit[] hits = searchResponse.getHits().getHits();
    assertNotEquals("both documents should be on different shards", hits[0].getShard().getShardId(), hits[1].getShard().getShardId());
    assertEquals("1", hits[0].getId());
    assertEquals("2", hits[1].getId());
    assertThat(hits[0].getScore(), greaterThan(hits[1].getScore()));
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)160 SearchResponse (org.elasticsearch.action.search.SearchResponse)112 SearchHits (org.elasticsearch.search.SearchHits)49 ArrayList (java.util.ArrayList)27 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)20 IOException (java.io.IOException)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)16 HashMap (java.util.HashMap)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)14 SearchHitField (org.elasticsearch.search.SearchHitField)14 HashSet (java.util.HashSet)12 Test (org.junit.Test)12 Map (java.util.Map)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 TopDocs (org.apache.lucene.search.TopDocs)10 Text (org.elasticsearch.common.text.Text)10 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)9 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)9