Search in sources :

Example 96 with SearchHit

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

the class IndexLookupIT method checkValueInEachDocWithFunctionScore.

private void checkValueInEachDocWithFunctionScore(Script fieldScript, Map<String, Object> expectedFieldVals, Script scoreScript, Map<String, Object> expectedScore, int numExpectedDocs) {
    SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(ScoreFunctionBuilders.scriptFunction(scoreScript))).addScriptField("tvtest", fieldScript).execute().actionGet();
    assertHitCount(sr, numExpectedDocs);
    for (SearchHit hit : sr.getHits().getHits()) {
        Object result = hit.getFields().get("tvtest").getValues().get(0);
        Object expectedResult = expectedFieldVals.get(hit.getId());
        assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
        assertThat("for doc " + hit.getId(), ((Float) expectedScore.get(hit.getId())).doubleValue(), Matchers.closeTo(hit.getScore(), 1.e-4));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 97 with SearchHit

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

the class IndexLookupIT method checkArrayValsInEachDoc.

private void checkArrayValsInEachDoc(Script script, HashMap<String, List<Object>> expectedArray, int expectedHitSize) {
    SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script).execute().actionGet();
    assertHitCount(sr, expectedHitSize);
    int nullCounter = 0;
    for (SearchHit hit : sr.getHits().getHits()) {
        Object result = hit.getFields().get("tvtest").getValues();
        Object expectedResult = expectedArray.get(hit.getId());
        assertThat("for doc " + hit.getId(), result, equalTo(expectedResult));
        if (expectedResult != null) {
            nullCounter++;
        }
    }
    assertThat(nullCounter, equalTo(expectedArray.size()));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 98 with SearchHit

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

Example 99 with SearchHit

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

the class TopHitsIT method testFetchFeatures.

public void testFetchFeatures() {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").setQuery(matchQuery("text", "text").queryName("test")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").size(1).highlighter(new HighlightBuilder().field("text")).explain(true).storedField("text").fieldDataField("field1").scriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).fetchSource("text", null).version(true))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    for (Terms.Bucket bucket : terms.getBuckets()) {
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getTotalHits(), equalTo(10L));
        assertThat(hits.getHits().length, equalTo(1));
        SearchHit hit = hits.getAt(0);
        HighlightField highlightField = hit.getHighlightFields().get("text");
        assertThat(highlightField.getFragments().length, equalTo(1));
        assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em> to entertain"));
        Explanation explanation = hit.getExplanation();
        assertThat(explanation.toString(), containsString("text:text"));
        long version = hit.getVersion();
        assertThat(version, equalTo(1L));
        assertThat(hit.getMatchedQueries()[0], equalTo("test"));
        SearchHitField field = hit.field("field1");
        assertThat(field.getValue().toString(), equalTo("5"));
        assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
        field = hit.field("script");
        assertThat(field.getValue().toString(), equalTo("5"));
        assertThat(hit.getSourceAsMap().size(), equalTo(1));
        assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
    }
}
Also used : Script(org.elasticsearch.script.Script) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Explanation(org.apache.lucene.search.Explanation) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) SearchHitField(org.elasticsearch.search.SearchHitField) 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 100 with SearchHit

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

the class SearchSliceIT method assertSearchSlicesWithScroll.

private void assertSearchSlicesWithScroll(SearchRequestBuilder request, String field, int numSlice) {
    int totalResults = 0;
    List<String> keys = new ArrayList<>();
    for (int id = 0; id < numSlice; id++) {
        SliceBuilder sliceBuilder = new SliceBuilder(field, id, numSlice);
        SearchResponse searchResponse = request.slice(sliceBuilder).get();
        totalResults += searchResponse.getHits().getHits().length;
        int expectedSliceResults = (int) searchResponse.getHits().getTotalHits();
        int numSliceResults = searchResponse.getHits().getHits().length;
        String scrollId = searchResponse.getScrollId();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            keys.add(hit.getId());
        }
        while (searchResponse.getHits().getHits().length > 0) {
            searchResponse = client().prepareSearchScroll("test").setScrollId(scrollId).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).get();
            scrollId = searchResponse.getScrollId();
            totalResults += searchResponse.getHits().getHits().length;
            numSliceResults += searchResponse.getHits().getHits().length;
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                keys.add(hit.getId());
            }
        }
        assertThat(numSliceResults, equalTo(expectedSliceResults));
        clearScroll(scrollId);
    }
    assertThat(totalResults, equalTo(NUM_DOCS));
    assertThat(keys.size(), equalTo(NUM_DOCS));
    assertThat(new HashSet(keys).size(), equalTo(NUM_DOCS));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Scroll(org.elasticsearch.search.Scroll) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) HashSet(java.util.HashSet)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)160 SearchResponse (org.elasticsearch.action.search.SearchResponse)112 SearchHits (org.elasticsearch.search.SearchHits)49 ArrayList (java.util.ArrayList)27 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)20 IOException (java.io.IOException)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)16 HashMap (java.util.HashMap)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)14 SearchHitField (org.elasticsearch.search.SearchHitField)14 HashSet (java.util.HashSet)12 Test (org.junit.Test)12 Map (java.util.Map)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 TopDocs (org.apache.lucene.search.TopDocs)10 Text (org.elasticsearch.common.text.Text)10 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)9 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)9