Search in sources :

Example 26 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)

Example 27 with SearchHit

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

the class QueryRescorerIT method assertEquivalentOrSubstringMatch.

private static void assertEquivalentOrSubstringMatch(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[] otherHits = rightHits.getHits();
    if (!hits[0].getId().equals(otherHits[0].getId())) {
        assertThat(((String) otherHits[0].getSourceAsMap().get("field1")).contains(query), equalTo(true));
    } else {
        Arrays.sort(hits, searchHitsComparator);
        Arrays.sort(otherHits, searchHitsComparator);
        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, hits[i].getId(), equalTo(rightHits.getHits()[i].getId()));
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits)

Example 28 with SearchHit

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

the class GeoPolygonIT method testSimplePolygon.

public void testSimplePolygon() throws Exception {
    List<GeoPoint> points = new ArrayList<>();
    points.add(new GeoPoint(40.7, -74.0));
    points.add(new GeoPoint(40.7, -74.1));
    points.add(new GeoPoint(40.8, -74.1));
    points.add(new GeoPoint(40.8, -74.0));
    points.add(new GeoPoint(40.7, -74.0));
    SearchResponse searchResponse = // from NY
    client().prepareSearch("test").setQuery(boolQuery().must(geoPolygonQuery("location", points))).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    for (SearchHit hit : searchResponse.getHits()) {
        assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5")));
    }
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 29 with SearchHit

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

the class GeoPolygonIT method testSimpleUnclosedPolygon.

public void testSimpleUnclosedPolygon() throws Exception {
    List<GeoPoint> points = new ArrayList<>();
    points.add(new GeoPoint(40.7, -74.0));
    points.add(new GeoPoint(40.7, -74.1));
    points.add(new GeoPoint(40.8, -74.1));
    points.add(new GeoPoint(40.8, -74.0));
    SearchResponse searchResponse = // from NY
    client().prepareSearch("test").setQuery(boolQuery().must(geoPolygonQuery("location", points))).execute().actionGet();
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    for (SearchHit hit : searchResponse.getHits()) {
        assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5")));
    }
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 30 with SearchHit

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

the class QueryProfilerIT method testProfileMatchesRegular.

/**
     * This test generates 1-10 random queries and executes a profiled and non-profiled
     * search for each query.  It then does some basic sanity checking of score and hits
     * to make sure the profiling doesn't interfere with the hits being returned
     */
public void testProfileMatchesRegular() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = randomIntBetween(100, 150);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "field2", i);
    }
    List<String> stringFields = Arrays.asList("field1");
    List<String> numericFields = Arrays.asList("field2");
    indexRandom(true, docs);
    refresh();
    int iters = between(1, 10);
    for (int i = 0; i < iters; i++) {
        QueryBuilder q = randomQueryBuilder(stringFields, numericFields, numDocs, 3);
        logger.info("Query: {}", q);
        SearchRequestBuilder vanilla = client().prepareSearch("test").setQuery(q).setProfile(false).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
        SearchRequestBuilder profile = client().prepareSearch("test").setQuery(q).setProfile(true).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
        MultiSearchResponse.Item[] responses = client().prepareMultiSearch().add(vanilla).add(profile).execute().actionGet().getResponses();
        SearchResponse vanillaResponse = responses[0].getResponse();
        SearchResponse profileResponse = responses[1].getResponse();
        float vanillaMaxScore = vanillaResponse.getHits().getMaxScore();
        float profileMaxScore = profileResponse.getHits().getMaxScore();
        if (Float.isNaN(vanillaMaxScore)) {
            assertTrue("Vanilla maxScore is NaN but Profile is not [" + profileMaxScore + "]", Float.isNaN(profileMaxScore));
        } else {
            assertTrue("Profile maxScore of [" + profileMaxScore + "] is not close to Vanilla maxScore [" + vanillaMaxScore + "]", nearlyEqual(vanillaMaxScore, profileMaxScore, 0.001));
        }
        assertThat("Profile totalHits of [" + profileResponse.getHits().getTotalHits() + "] is not close to Vanilla totalHits [" + vanillaResponse.getHits().getTotalHits() + "]", vanillaResponse.getHits().getTotalHits(), equalTo(profileResponse.getHits().getTotalHits()));
        SearchHit[] vanillaHits = vanillaResponse.getHits().getHits();
        SearchHit[] profileHits = profileResponse.getHits().getHits();
        for (int j = 0; j < vanillaHits.length; j++) {
            assertThat("Profile hit #" + j + " has a different ID from Vanilla", vanillaHits[j].getId(), equalTo(profileHits[j].getId()));
        }
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) RandomQueryGenerator.randomQueryBuilder(org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder)

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