Search in sources :

Example 1 with SearchHit

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

the class ScriptFieldIT method testNativeScript.

public void testNativeScript() throws InterruptedException, ExecutionException {
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("text", "doc1"), client().prepareIndex("test", "type1", "2").setSource("text", "doc2"), client().prepareIndex("test", "type1", "3").setSource("text", "doc3"), client().prepareIndex("test", "type1", "4").setSource("text", "doc4"), client().prepareIndex("test", "type1", "5").setSource("text", "doc5"), client().prepareIndex("test", "type1", "6").setSource("text", "doc6"));
    client().admin().indices().prepareFlush("test").execute().actionGet();
    SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("int", new Script(ScriptType.INLINE, "native", "int", Collections.emptyMap())).addScriptField("float", new Script(ScriptType.INLINE, "native", "float", Collections.emptyMap())).addScriptField("double", new Script(ScriptType.INLINE, "native", "double", Collections.emptyMap())).addScriptField("long", new Script(ScriptType.INLINE, "native", "long", Collections.emptyMap())).execute().actionGet();
    assertThat(sr.getHits().getHits().length, equalTo(6));
    for (SearchHit hit : sr.getHits().getHits()) {
        Object result = hit.getFields().get("int").getValues().get(0);
        assertThat(result, equalTo((Object) intArray));
        result = hit.getFields().get("long").getValues().get(0);
        assertThat(result, equalTo((Object) longArray));
        result = hit.getFields().get("float").getValues().get(0);
        assertThat(result, equalTo((Object) floatArray));
        result = hit.getFields().get("double").getValues().get(0);
        assertThat(result, equalTo((Object) doubleArray));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with SearchHit

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

the class ChildrenIT method testParentWithMultipleBuckets.

public void testParentWithMultipleBuckets() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("randomized", false)).addAggregation(terms("category").field("category").size(10000).subAggregation(children("to_comment", "comment").subAggregation(topHits("top_comments").sort("_uid", SortOrder.ASC)))).get();
    assertSearchResponse(searchResponse);
    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(3));
    for (Terms.Bucket bucket : categoryTerms.getBuckets()) {
        logger.info("bucket={}", bucket.getKey());
        Children childrenBucket = bucket.getAggregations().get("to_comment");
        TopHits topHits = childrenBucket.getAggregations().get("top_comments");
        logger.info("total_hits={}", topHits.getHits().getTotalHits());
        for (SearchHit searchHit : topHits.getHits()) {
            logger.info("hit= {} {} {}", searchHit.getSortValues()[0], searchHit.getType(), searchHit.getId());
        }
    }
    Terms.Bucket categoryBucket = categoryTerms.getBucketByKey("a");
    assertThat(categoryBucket.getKeyAsString(), equalTo("a"));
    assertThat(categoryBucket.getDocCount(), equalTo(3L));
    Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(2L));
    TopHits topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(2L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("a"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
    assertThat(topHits.getHits().getAt(1).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(1).getType(), equalTo("comment"));
    categoryBucket = categoryTerms.getBucketByKey("b");
    assertThat(categoryBucket.getKeyAsString(), equalTo("b"));
    assertThat(categoryBucket.getDocCount(), equalTo(2L));
    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1L));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(1L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
    categoryBucket = categoryTerms.getBucketByKey("c");
    assertThat(categoryBucket.getKeyAsString(), equalTo("c"));
    assertThat(categoryBucket.getDocCount(), equalTo(2L));
    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1L));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(1L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Children(org.elasticsearch.search.aggregations.bucket.children.Children) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with SearchHit

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

Example 4 with SearchHit

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

the class FetchSourceSubPhaseTests method hitExecuteMultiple.

private FetchSubPhase.HitContext hitExecuteMultiple(XContentBuilder source, boolean fetchSource, String[] includes, String[] excludes) {
    FetchSourceContext fetchSourceContext = new FetchSourceContext(fetchSource, includes, excludes);
    SearchContext searchContext = new FetchSourceSubPhaseTestSearchContext(fetchSourceContext, source == null ? null : source.bytes());
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    hitContext.reset(new SearchHit(1, null, null, null), null, 1, null);
    FetchSourceSubPhase phase = new FetchSourceSubPhase();
    phase.hitExecute(searchContext, hitContext);
    return hitContext;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchContext(org.elasticsearch.search.internal.SearchContext) TestSearchContext(org.elasticsearch.test.TestSearchContext) FetchSubPhase(org.elasticsearch.search.fetch.FetchSubPhase)

Example 5 with SearchHit

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

the class InnerHitsIT method testNestedInnerHitWrappedInParentChildInnerhit.

public void testNestedInnerHitWrappedInParentChildInnerhit() throws Exception {
    assertAcked(prepareCreate("test").addMapping("child_type", "_parent", "type=parent_type", "nested_type", "type=nested"));
    client().prepareIndex("test", "parent_type", "1").setSource("key", "value").get();
    client().prepareIndex("test", "child_type", "2").setParent("1").setSource("nested_type", Collections.singletonMap("key", "value")).get();
    refresh();
    SearchResponse response = client().prepareSearch("test").setQuery(boolQuery().must(matchQuery("key", "value")).should(hasChildQuery("child_type", nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder(), false), ScoreMode.None).innerHit(new InnerHitBuilder(), false))).get();
    assertHitCount(response, 1);
    SearchHit hit = response.getHits().getAt(0);
    assertThat(hit.getInnerHits().get("child_type").getAt(0).field("_parent").getValue(), equalTo("1"));
    assertThat(hit.getInnerHits().get("child_type").getAt(0).getInnerHits().get("nested_type").getAt(0).field("_parent"), nullValue());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

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