Search in sources :

Example 46 with SearchHits

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

the class ExpandSearchPhase method run.

@Override
public void run() throws IOException {
    if (isCollapseRequest()) {
        SearchRequest searchRequest = context.getRequest();
        CollapseBuilder collapseBuilder = searchRequest.source().collapse();
        MultiSearchRequest multiRequest = new MultiSearchRequest();
        if (collapseBuilder.getMaxConcurrentGroupRequests() > 0) {
            multiRequest.maxConcurrentSearchRequests(collapseBuilder.getMaxConcurrentGroupRequests());
        }
        for (SearchHit hit : searchResponse.getHits()) {
            BoolQueryBuilder groupQuery = new BoolQueryBuilder();
            Object collapseValue = hit.field(collapseBuilder.getField()).getValue();
            if (collapseValue != null) {
                groupQuery.filter(QueryBuilders.matchQuery(collapseBuilder.getField(), collapseValue));
            } else {
                groupQuery.mustNot(QueryBuilders.existsQuery(collapseBuilder.getField()));
            }
            QueryBuilder origQuery = searchRequest.source().query();
            if (origQuery != null) {
                groupQuery.must(origQuery);
            }
            SearchSourceBuilder sourceBuilder = buildExpandSearchSourceBuilder(collapseBuilder.getInnerHit()).query(groupQuery);
            SearchRequest groupRequest = new SearchRequest(searchRequest.indices()).types(searchRequest.types()).source(sourceBuilder);
            multiRequest.add(groupRequest);
        }
        context.getSearchTransport().sendExecuteMultiSearch(multiRequest, context.getTask(), ActionListener.wrap(response -> {
            Iterator<MultiSearchResponse.Item> it = response.iterator();
            for (SearchHit hit : searchResponse.getHits()) {
                MultiSearchResponse.Item item = it.next();
                if (item.isFailure()) {
                    context.onPhaseFailure(this, "failed to expand hits", item.getFailure());
                    return;
                }
                SearchHits innerHits = item.getResponse().getHits();
                if (hit.getInnerHits() == null) {
                    hit.setInnerHits(new HashMap<>(1));
                }
                hit.getInnerHits().put(collapseBuilder.getInnerHit().getName(), innerHits);
            }
            context.executeNextPhase(this, nextPhaseFactory.apply(searchResponse));
        }, context::onFailure));
    } else {
        context.executeNextPhase(this, nextPhaseFactory.apply(searchResponse));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Iterator(java.util.Iterator) SearchHits(org.elasticsearch.search.SearchHits) IOException(java.io.IOException) HashMap(java.util.HashMap) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) Function(java.util.function.Function) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ActionListener(org.elasticsearch.action.ActionListener) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Iterator(java.util.Iterator) SearchHits(org.elasticsearch.search.SearchHits)

Example 47 with SearchHits

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

the class InternalTopHits method doReduce.

@Override
public InternalAggregation doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    final SearchHits[] shardHits = new SearchHits[aggregations.size()];
    final int from;
    final int size;
    if (reduceContext.isFinalReduce()) {
        from = this.from;
        size = this.size;
    } else {
        // if we are not in the final reduce we need to ensure we maintain all possible elements during reduce
        // hence for pagination we need to maintain all hits until we are in the final phase.
        from = 0;
        size = this.from + this.size;
    }
    final TopDocs reducedTopDocs;
    final TopDocs[] shardDocs;
    if (topDocs instanceof TopFieldDocs) {
        Sort sort = new Sort(((TopFieldDocs) topDocs).fields);
        shardDocs = new TopFieldDocs[aggregations.size()];
        for (int i = 0; i < shardDocs.length; i++) {
            InternalTopHits topHitsAgg = (InternalTopHits) aggregations.get(i);
            shardDocs[i] = topHitsAgg.topDocs;
            shardHits[i] = topHitsAgg.searchHits;
        }
        reducedTopDocs = TopDocs.merge(sort, from, size, (TopFieldDocs[]) shardDocs, true);
    } else {
        shardDocs = new TopDocs[aggregations.size()];
        for (int i = 0; i < shardDocs.length; i++) {
            InternalTopHits topHitsAgg = (InternalTopHits) aggregations.get(i);
            shardDocs[i] = topHitsAgg.topDocs;
            shardHits[i] = topHitsAgg.searchHits;
        }
        reducedTopDocs = TopDocs.merge(from, size, shardDocs, true);
    }
    final int[] tracker = new int[shardHits.length];
    SearchHit[] hits = new SearchHit[reducedTopDocs.scoreDocs.length];
    for (int i = 0; i < reducedTopDocs.scoreDocs.length; i++) {
        ScoreDoc scoreDoc = reducedTopDocs.scoreDocs[i];
        int position;
        do {
            position = tracker[scoreDoc.shardIndex]++;
        } while (shardDocs[scoreDoc.shardIndex].scoreDocs[position] != scoreDoc);
        hits[i] = shardHits[scoreDoc.shardIndex].getAt(position);
    }
    return new InternalTopHits(name, this.from, this.size, reducedTopDocs, new SearchHits(hits, reducedTopDocs.totalHits, reducedTopDocs.getMaxScore()), pipelineAggregators(), getMetaData());
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) SearchHit(org.elasticsearch.search.SearchHit) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Sort(org.apache.lucene.search.Sort) SearchHits(org.elasticsearch.search.SearchHits) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 48 with SearchHits

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

the class InnerHitsFetchSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if ((context.innerHits() != null && context.innerHits().getInnerHits().size() > 0) == false) {
        return;
    }
    Map<String, SearchHits> results = new HashMap<>();
    for (Map.Entry<String, InnerHitsContext.BaseInnerHits> entry : context.innerHits().getInnerHits().entrySet()) {
        InnerHitsContext.BaseInnerHits innerHits = entry.getValue();
        TopDocs topDocs;
        try {
            topDocs = innerHits.topDocs(context, hitContext);
        } catch (IOException e) {
            throw ExceptionsHelper.convertToElastic(e);
        }
        innerHits.queryResult().topDocs(topDocs, innerHits.sort() == null ? null : innerHits.sort().formats);
        int[] docIdsToLoad = new int[topDocs.scoreDocs.length];
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            docIdsToLoad[i] = topDocs.scoreDocs[i].doc;
        }
        innerHits.docIdsToLoad(docIdsToLoad, 0, docIdsToLoad.length);
        fetchPhase.execute(innerHits);
        FetchSearchResult fetchResult = innerHits.fetchResult();
        SearchHit[] internalHits = fetchResult.fetchResult().hits().internalHits();
        for (int i = 0; i < internalHits.length; i++) {
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            SearchHit searchHitFields = internalHits[i];
            searchHitFields.score(scoreDoc.score);
            if (scoreDoc instanceof FieldDoc) {
                FieldDoc fieldDoc = (FieldDoc) scoreDoc;
                searchHitFields.sortValues(fieldDoc.fields, innerHits.sort().formats);
            }
        }
        results.put(entry.getKey(), fetchResult.hits());
    }
    hitContext.hit().setInnerHits(results);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) FieldDoc(org.apache.lucene.search.FieldDoc) HashMap(java.util.HashMap) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 49 with SearchHits

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

the class TopHitsIT method testTopHitsInNested.

public void testTopHitsInNested() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("articles").addAggregation(histogram("dates").field("date").interval(5).order(Histogram.Order.aggregation("to-comments", true)).subAggregation(nested("to-comments", "comments").subAggregation(topHits("comments").highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "text")))).sort("comments.id", SortOrder.ASC)))).get();
    Histogram histogram = searchResponse.getAggregations().get("dates");
    for (int i = 0; i < numArticles; i += 5) {
        Histogram.Bucket bucket = histogram.getBuckets().get(i / 5);
        assertThat(bucket.getDocCount(), equalTo(5L));
        long numNestedDocs = 10 + (5 * i);
        Nested nested = bucket.getAggregations().get("to-comments");
        assertThat(nested.getDocCount(), equalTo(numNestedDocs));
        TopHits hits = nested.getAggregations().get("comments");
        SearchHits searchHits = hits.getHits();
        assertThat(searchHits.getTotalHits(), equalTo(numNestedDocs));
        for (int j = 0; j < 3; j++) {
            assertThat(searchHits.getAt(j).getNestedIdentity().getField().string(), equalTo("comments"));
            assertThat(searchHits.getAt(j).getNestedIdentity().getOffset(), equalTo(0));
            assertThat((Integer) searchHits.getAt(j).getSourceAsMap().get("id"), equalTo(0));
            HighlightField highlightField = searchHits.getAt(j).getHighlightFields().get("comments.message");
            assertThat(highlightField.getFragments().length, equalTo(1));
            assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em>"));
        }
    }
}
Also used : SearchHitField(org.elasticsearch.search.SearchHitField) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 50 with SearchHits

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

the class TopHitsIT method testNoStoredFields.

public void testNoStoredFields() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").storedField("_none_"))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    for (int i = 0; i < 5; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(10L));
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getTotalHits(), equalTo(10L));
        assertThat(hits.getHits().length, equalTo(3));
        for (SearchHit hit : hits) {
            assertThat(hit.getSourceAsMap(), nullValue());
            assertThat(hit.getId(), nullValue());
            assertThat(hit.getType(), nullValue());
        }
    }
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)95 SearchResponse (org.elasticsearch.action.search.SearchResponse)61 SearchHit (org.elasticsearch.search.SearchHit)52 ArrayList (java.util.ArrayList)24 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)16 IOException (java.io.IOException)15 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)13 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)13 ElasticsearchAssertions.assertSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits)13 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)10 TopDocs (org.apache.lucene.search.TopDocs)9 SearchHitField (org.elasticsearch.search.SearchHitField)9 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)9 Text (org.elasticsearch.common.text.Text)7 ElasticsearchAssertions.assertOrderedSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits)7