Search in sources :

Example 46 with SearchHit

use of org.graylog.shaded.elasticsearch7.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 47 with SearchHit

use of org.graylog.shaded.elasticsearch7.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 48 with SearchHit

use of org.graylog.shaded.elasticsearch7.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 49 with SearchHit

use of org.graylog.shaded.elasticsearch7.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 50 with SearchHit

use of org.graylog.shaded.elasticsearch7.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)

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