Search in sources :

Example 46 with SearchHit

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

the class InnerHitsIT method testRandomNested.

public void testRandomNested() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("type", "field1", "type=nested", "field2", "type=nested"));
    int numDocs = scaledRandomIntBetween(25, 100);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int[] field1InnerObjects = new int[numDocs];
    int[] field2InnerObjects = new int[numDocs];
    for (int i = 0; i < numDocs; i++) {
        int numInnerObjects = field1InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        XContentBuilder source = jsonBuilder().startObject().startArray("field1");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        numInnerObjects = field2InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        source.endArray().startArray("field2");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        source.endArray().endObject();
        requestBuilders.add(client().prepareIndex("idx", "type", String.format(Locale.ENGLISH, "%03d", i)).setSource(source));
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(nestedQuery("field1", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("a").setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)), false));
    boolQuery.should(nestedQuery("field2", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)).setSize(size), false));
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(boolQuery).setSize(numDocs).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits(), equalTo((long) field1InnerObjects[i]));
        for (int j = 0; j < field1InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field1"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits(), equalTo((long) field2InnerObjects[i]));
        for (int j = 0; j < field2InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field2"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchHits(org.elasticsearch.search.SearchHits) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 47 with SearchHit

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

the class MatchedQueriesIT method testFuzzyQuerySupportsName.

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

Example 48 with SearchHit

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

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

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