Search in sources :

Example 26 with SearchHits

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

the class InnerHitsIT method testParentChildMultipleLayers.

public void testParentChildMultipleLayers() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", "title", "type=text").addMapping("comment", "_parent", "type=article", "message", "type=text").addMapping("remark", "_parent", "type=comment", "message", "type=text"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource("title", "quick brown fox"));
    requests.add(client().prepareIndex("articles", "comment", "1").setParent("1").setSource("message", "fox eat quick"));
    requests.add(client().prepareIndex("articles", "remark", "1").setParent("1").setRouting("1").setSource("message", "good"));
    requests.add(client().prepareIndex("articles", "article", "2").setSource("title", "big gray elephant"));
    requests.add(client().prepareIndex("articles", "comment", "2").setParent("2").setSource("message", "elephant captured"));
    requests.add(client().prepareIndex("articles", "remark", "2").setParent("2").setRouting("2").setSource("message", "bad"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "good"), ScoreMode.None).innerHit(new InnerHitBuilder(), false), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getType(), equalTo("comment"));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getType(), equalTo("remark"));
    response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "bad"), ScoreMode.None).innerHit(new InnerHitBuilder(), false), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getType(), equalTo("comment"));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getType(), equalTo("remark"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 27 with SearchHits

use of org.elasticsearch.search.SearchHits 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 28 with SearchHits

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

the class MultiMatchQueryIT method testSingleField.

public void testSingleField() throws NoSuchFieldException, IllegalAccessException {
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(randomizeType(multiMatchQuery("15", "skill"))).get();
    assertNoFailures(searchResponse);
    assertFirstHit(searchResponse, hasId("theone"));
    searchResponse = client().prepareSearch("test").setQuery(randomizeType(multiMatchQuery("15", "skill", "int-field")).analyzer("category")).get();
    assertNoFailures(searchResponse);
    assertFirstHit(searchResponse, hasId("theone"));
    String[] fields = { "full_name", "first_name", "last_name", "last_name_phrase", "first_name_phrase", "category_phrase", "category", "missing_field", "missing_fields*" };
    String[] query = { "marvel", "hero", "captain", "america", "15", "17", "1", "5", "ultimate", "Man", "marvel", "wolferine", "ninja" };
    // check if it's equivalent to a match query.
    int numIters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < numIters; i++) {
        String field = RandomPicks.randomFrom(random(), fields);
        int numTerms = randomIntBetween(1, query.length);
        StringBuilder builder = new StringBuilder();
        for (int j = 0; j < numTerms; j++) {
            builder.append(RandomPicks.randomFrom(random(), query)).append(" ");
        }
        MultiMatchQueryBuilder multiMatchQueryBuilder = randomizeType(multiMatchQuery(builder.toString(), field));
        SearchResponse multiMatchResp = client().prepareSearch("test").addSort("_score", SortOrder.DESC).addSort("_uid", SortOrder.ASC).setQuery(multiMatchQueryBuilder).get();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, builder.toString());
        if (multiMatchQueryBuilder.getType() != null) {
            matchQueryBuilder.type(MatchQuery.Type.valueOf(multiMatchQueryBuilder.getType().matchQueryType().toString()));
        }
        SearchResponse matchResp = client().prepareSearch("test").addSort("_score", SortOrder.DESC).addSort("_uid", SortOrder.ASC).setQuery(matchQueryBuilder).get();
        assertThat("field: " + field + " query: " + builder.toString(), multiMatchResp.getHits().getTotalHits(), equalTo(matchResp.getHits().getTotalHits()));
        SearchHits hits = multiMatchResp.getHits();
        if (field.startsWith("missing")) {
            assertEquals(0, hits.getHits().length);
        }
        for (int j = 0; j < hits.getHits().length; j++) {
            assertThat(hits.getHits()[j].getScore(), equalTo(matchResp.getHits().getHits()[j].getScore()));
            assertThat(hits.getHits()[j].getId(), equalTo(matchResp.getHits().getHits()[j].getId()));
        }
    }
}
Also used : MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 29 with SearchHits

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

the class FunctionScorePluginIT method testPlugin.

public void testPlugin() throws Exception {
    client().admin().indices().prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("num1").field("type", "date").endObject().endObject().endObject().endObject()).execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();
    client().index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-26").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-27").endObject())).actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    DecayFunctionBuilder<?> gfb = new CustomDistanceScoreBuilder("num1", "2013-05-28", "+1d");
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().explain(false).query(functionScoreQuery(termQuery("test", "value"), gfb))));
    SearchResponse sr = response.actionGet();
    ElasticsearchAssertions.assertNoFailures(sr);
    SearchHits sh = sr.getHits();
    assertThat(sh.getHits().length, equalTo(2));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat(sh.getAt(1).getId(), equalTo("2"));
}
Also used : SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 30 with SearchHits

use of org.elasticsearch.search.SearchHits 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

SearchHits (org.elasticsearch.search.SearchHits)136 SearchResponse (org.elasticsearch.action.search.SearchResponse)92 SearchHit (org.elasticsearch.search.SearchHit)86 ArrayList (java.util.ArrayList)45 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)27 IOException (java.io.IOException)24 Map (java.util.Map)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 HashMap (java.util.HashMap)19 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)16 SearchRequest (org.elasticsearch.action.search.SearchRequest)15 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 TotalHits (org.apache.lucene.search.TotalHits)12 Test (org.junit.Test)12 ScoreDoc (org.apache.lucene.search.ScoreDoc)11 Text (org.elasticsearch.common.text.Text)11 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)10