Search in sources :

Example 6 with SearchHits

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

the class MoreExpressionTests method testScore.

public void testScore() throws Exception {
    createIndex("test");
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("text", "hello goodbye"), client().prepareIndex("test", "doc", "2").setSource("text", "hello hello hello goodbye"), client().prepareIndex("test", "doc", "3").setSource("text", "hello hello goodebye"));
    ScoreFunctionBuilder<?> score = ScoreFunctionBuilders.scriptFunction(new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap()));
    SearchRequestBuilder req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE));
    // make sure DF is consistent
    req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    SearchResponse rsp = req.get();
    assertSearchResponse(rsp);
    SearchHits hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals("1", hits.getAt(0).getId());
    assertEquals("3", hits.getAt(1).getId());
    assertEquals("2", hits.getAt(2).getId());
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with SearchHits

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

the class MoreExpressionTests method testDateObjectMethods.

public void testDateObjectMethods() throws Exception {
    ElasticsearchAssertions.assertAcked(prepareCreate("test").addMapping("doc", "date0", "type=date", "date1", "type=date"));
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("date0", "2015-04-28T04:02:07Z", "date1", "1985-09-01T23:11:01Z"), client().prepareIndex("test", "doc", "2").setSource("date0", "2013-12-25T11:56:45Z", "date1", "1983-10-13T23:15:00Z"));
    SearchResponse rsp = buildRequest("doc['date0'].date.secondOfMinute - doc['date0'].date.minuteOfHour").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    SearchHits hits = rsp.getHits();
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(-11.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date0'].date.getHourOfDay() + doc['date1'].date.dayOfMonth").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(24.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date1'].date.monthOfYear + 1").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(10.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(11.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date1'].date.year").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(1985.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(1983.0, hits.getAt(1).field("foo").getValue(), 0.0D);
}
Also used : SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with SearchHits

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

the class SearchPhaseController method getHits.

private SearchHits getHits(ReducedQueryPhase reducedQueryPhase, boolean ignoreFrom, ScoreDoc[] sortedDocs, AtomicArray<? extends QuerySearchResultProvider> fetchResultsArr) {
    List<? extends AtomicArray.Entry<? extends QuerySearchResultProvider>> fetchResults = fetchResultsArr.asList();
    boolean sorted = false;
    int sortScoreIndex = -1;
    if (reducedQueryPhase.oneResult.topDocs() instanceof TopFieldDocs) {
        TopFieldDocs fieldDocs = (TopFieldDocs) reducedQueryPhase.oneResult.queryResult().topDocs();
        if (fieldDocs instanceof CollapseTopFieldDocs && fieldDocs.fields.length == 1 && fieldDocs.fields[0].getType() == SortField.Type.SCORE) {
            sorted = false;
        } else {
            sorted = true;
            for (int i = 0; i < fieldDocs.fields.length; i++) {
                if (fieldDocs.fields[i].getType() == SortField.Type.SCORE) {
                    sortScoreIndex = i;
                }
            }
        }
    }
    // clean the fetch counter
    for (AtomicArray.Entry<? extends QuerySearchResultProvider> entry : fetchResults) {
        entry.value.fetchResult().initCounter();
    }
    int from = ignoreFrom ? 0 : reducedQueryPhase.oneResult.queryResult().from();
    int numSearchHits = (int) Math.min(reducedQueryPhase.fetchHits - from, reducedQueryPhase.oneResult.size());
    // with collapsing we can have more fetch hits than sorted docs
    numSearchHits = Math.min(sortedDocs.length, numSearchHits);
    // merge hits
    List<SearchHit> hits = new ArrayList<>();
    if (!fetchResults.isEmpty()) {
        for (int i = 0; i < numSearchHits; i++) {
            ScoreDoc shardDoc = sortedDocs[i];
            QuerySearchResultProvider fetchResultProvider = fetchResultsArr.get(shardDoc.shardIndex);
            if (fetchResultProvider == null) {
                continue;
            }
            FetchSearchResult fetchResult = fetchResultProvider.fetchResult();
            int index = fetchResult.counterGetAndIncrement();
            if (index < fetchResult.hits().internalHits().length) {
                SearchHit searchHit = fetchResult.hits().internalHits()[index];
                searchHit.score(shardDoc.score);
                searchHit.shard(fetchResult.shardTarget());
                if (sorted) {
                    FieldDoc fieldDoc = (FieldDoc) shardDoc;
                    searchHit.sortValues(fieldDoc.fields, reducedQueryPhase.oneResult.sortValueFormats());
                    if (sortScoreIndex != -1) {
                        searchHit.score(((Number) fieldDoc.fields[sortScoreIndex]).floatValue());
                    }
                }
                hits.add(searchHit);
            }
        }
    }
    return new SearchHits(hits.toArray(new SearchHit[hits.size()]), reducedQueryPhase.totalHits, reducedQueryPhase.maxScore);
}
Also used : AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) SearchHit(org.elasticsearch.search.SearchHit) FieldDoc(org.apache.lucene.search.FieldDoc) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) ArrayList(java.util.ArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) ScoreDoc(org.apache.lucene.search.ScoreDoc) SearchHits(org.elasticsearch.search.SearchHits) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs)

Example 9 with SearchHits

use of org.elasticsearch.search.SearchHits in project sonarqube by SonarSource.

the class ComponentIndex method bucketToQualifier.

private static ComponentsPerQualifier bucketToQualifier(Bucket bucket) {
    InternalTopHits docs = bucket.getAggregations().get(DOCS_AGGREGATION_NAME);
    SearchHits hitList = docs.getHits();
    SearchHit[] hits = hitList.getHits();
    List<String> componentUuids = Arrays.stream(hits).map(SearchHit::getId).collect(Collectors.toList(hits.length));
    return new ComponentsPerQualifier(bucket.getKey(), componentUuids, hitList.totalHits());
}
Also used : InternalTopHits(org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHits) SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits)

Example 10 with SearchHits

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

the class TopHitsIT method testNestedFetchFeatures.

public void testNestedFetchFeatures() {
    String hlType = randomFrom("plain", "fvh", "postings");
    HighlightBuilder.Field hlField = new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "comment")).forceSource(// randomly from stored field or _source
    randomBoolean()).highlighterType(hlType);
    SearchResponse searchResponse = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test"), ScoreMode.Avg)).addAggregation(nested("to-comments", "comments").subAggregation(topHits("top-comments").size(1).highlighter(new HighlightBuilder().field(hlField)).explain(true).fieldDataField("comments.user").scriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).fetchSource("comments.message", null).version(true).sort("comments.date", SortOrder.ASC))).get();
    assertHitCount(searchResponse, 2);
    Nested nested = searchResponse.getAggregations().get("to-comments");
    assertThat(nested.getDocCount(), equalTo(4L));
    SearchHits hits = ((TopHits) nested.getAggregations().get("top-comments")).getHits();
    assertThat(hits.getTotalHits(), equalTo(4L));
    SearchHit searchHit = hits.getAt(0);
    assertThat(searchHit.getId(), equalTo("1"));
    assertThat(searchHit.getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(searchHit.getNestedIdentity().getOffset(), equalTo(0));
    HighlightField highlightField = searchHit.getHighlightFields().get("comments.message");
    assertThat(highlightField.getFragments().length, equalTo(1));
    assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>comment</em>"));
    // Can't explain nested hit with the main query, since both are in a different scopes, also the nested doc may not even have matched with the main query
    // If top_hits would have a query option then we can explain that query
    Explanation explanation = searchHit.getExplanation();
    assertFalse(explanation.isMatch());
    // Returns the version of the root document. Nested docs don't have a separate version
    long version = searchHit.getVersion();
    assertThat(version, equalTo(1L));
    assertThat(searchHit.getMatchedQueries(), arrayContaining("test"));
    SearchHitField field = searchHit.field("comments.user");
    assertThat(field.getValue().toString(), equalTo("a"));
    field = searchHit.field("script");
    assertThat(field.getValue().toString(), equalTo("5"));
    assertThat(searchHit.getSourceAsMap().size(), equalTo(1));
    assertThat(XContentMapValues.extractValue("comments.message", searchHit.getSourceAsMap()), equalTo("some comment"));
}
Also used : Script(org.elasticsearch.script.Script) SearchHit(org.elasticsearch.search.SearchHit) Explanation(org.apache.lucene.search.Explanation) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

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