Search in sources :

Example 51 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 52 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 53 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 54 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)

Example 55 with SearchHit

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

the class QueryRescorerIT method assertEquivalent.

private static void assertEquivalent(String query, SearchResponse plain, SearchResponse rescored) {
    assertNoFailures(plain);
    assertNoFailures(rescored);
    SearchHits leftHits = plain.getHits();
    SearchHits rightHits = rescored.getHits();
    assertThat(leftHits.getTotalHits(), equalTo(rightHits.getTotalHits()));
    assertThat(leftHits.getHits().length, equalTo(rightHits.getHits().length));
    SearchHit[] hits = leftHits.getHits();
    SearchHit[] rHits = rightHits.getHits();
    Arrays.sort(hits, searchHitsComparator);
    Arrays.sort(rHits, searchHitsComparator);
    for (int i = 0; i < hits.length; i++) {
        assertThat("query: " + query, hits[i].getScore(), equalTo(rHits[i].getScore()));
    }
    for (int i = 0; i < hits.length; i++) {
        if (hits[i].getScore() == hits[hits.length - 1].getScore()) {
            // we need to cut off here since this is the tail of the queue and we might not have fetched enough docs
            return;
        }
        assertThat("query: " + query, hits[i].getId(), equalTo(rHits[i].getId()));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)166 SearchResponse (org.elasticsearch.action.search.SearchResponse)114 SearchHits (org.elasticsearch.search.SearchHits)52 ArrayList (java.util.ArrayList)31 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)22 IOException (java.io.IOException)20 Matchers.containsString (org.hamcrest.Matchers.containsString)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)16 HashMap (java.util.HashMap)15 ScoreDoc (org.apache.lucene.search.ScoreDoc)14 SearchHitField (org.elasticsearch.search.SearchHitField)14 HashSet (java.util.HashSet)13 Map (java.util.Map)12 Test (org.junit.Test)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 TopDocs (org.apache.lucene.search.TopDocs)10 Text (org.elasticsearch.common.text.Text)10 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)9 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)9