Search in sources :

Example 11 with HighlightBuilder

use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testSimpleNested.

public void testSimpleNested() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", jsonBuilder().startObject().startObject("article").startObject("properties").startObject("comments").field("type", "nested").startObject("properties").startObject("message").field("type", "text").field("fielddata", true).endObject().endObject().endObject().startObject("title").field("type", "text").endObject().endObject().endObject().endObject()));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject().field("title", "quick brown fox").startArray("comments").startObject().field("message", "fox eat quick").endObject().startObject().field("message", "fox ate rabbit x y z").endObject().startObject().field("message", "rabbit got away").endObject().endArray().endObject()));
    requests.add(client().prepareIndex("articles", "article", "2").setSource(jsonBuilder().startObject().field("title", "big gray elephant").startArray("comments").startObject().field("message", "elephant captured").endObject().startObject().field("message", "mice squashed by elephant x").endObject().startObject().field("message", "elephant scared by mice x y").endObject().endArray().endObject()));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"), 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(2L));
    assertThat(innerHits.getHits().length, equalTo(2));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(1).getId(), equalTo("1"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getShard(), notNullValue());
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(3L));
    assertThat(innerHits.getHits().length, equalTo(3));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(1).getId(), equalTo("2"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(innerHits.getAt(2).getId(), equalTo("2"));
    assertThat(innerHits.getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(2).getNestedIdentity().getOffset(), equalTo(2));
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setHighlightBuilder(new HighlightBuilder().field("comments.message")).setExplain(true).addDocValueField("comments.message").addScriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).setSize(1), false)).get();
    assertNoFailures(response);
    innerHits = response.getHits().getAt(0).getInnerHits().get("comments");
    assertThat(innerHits.getTotalHits(), equalTo(2L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getHighlightFields().get("comments.message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
    assertThat(innerHits.getAt(0).getExplanation().toString(), containsString("weight(comments.message:fox in"));
    assertThat(innerHits.getAt(0).getFields().get("comments.message").getValue().toString(), equalTo("eat"));
    assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("5"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 12 with HighlightBuilder

use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class CustomHighlighterSearchIT method testThatCustomHighlighterCanBeConfiguredPerField.

public void testThatCustomHighlighterCanBeConfiguredPerField() throws Exception {
    HighlightBuilder.Field highlightConfig = new HighlightBuilder.Field("name");
    highlightConfig.highlighterType("test-custom");
    Map<String, Object> options = new HashMap<>();
    options.put("myFieldOption", "someValue");
    highlightConfig.options(options);
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("test").setQuery(QueryBuilders.matchAllQuery()).highlighter(new HighlightBuilder().field(highlightConfig)).execute().actionGet();
    assertHighlight(searchResponse, 0, "name", 0, equalTo("standard response for name at position 1"));
    assertHighlight(searchResponse, 0, "name", 1, equalTo("field:myFieldOption:someValue"));
}
Also used : HashMap(java.util.HashMap) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 13 with HighlightBuilder

use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class ChildQuerySearchIT method testHasChildInnerHitsHighlighting.

public void testHasChildInnerHitsHighlighting() throws Exception {
    assertAcked(prepareCreate("test").addMapping("parent").addMapping("child", "_parent", "type=parent"));
    ensureGreen();
    client().prepareIndex("test", "parent", "1").setSource("p_field", 1).get();
    client().prepareIndex("test", "child", "2").setParent("1").setSource("c_field", "foo bar").get();
    client().admin().indices().prepareFlush("test").get();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", matchQuery("c_field", "foo"), ScoreMode.None).innerHit(new InnerHitBuilder().setHighlightBuilder(new HighlightBuilder().field(new Field("c_field").highlightQuery(QueryBuilders.matchQuery("c_field", "bar")))), false)).get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1"));
    SearchHit[] searchHits = searchResponse.getHits().getHits()[0].getInnerHits().get("child").getHits();
    assertThat(searchHits.length, equalTo(1));
    assertThat(searchHits[0].getHighlightFields().get("c_field").getFragments().length, equalTo(1));
    assertThat(searchHits[0].getHighlightFields().get("c_field").getFragments()[0].string(), equalTo("foo <em>bar</em>"));
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 14 with HighlightBuilder

use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class ChildQuerySearchIT method testHighlightersIgnoreParentChild.

public void testHighlightersIgnoreParentChild() {
    assertAcked(prepareCreate("test").addMapping("parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets").addMapping("child-type", "_parent", "type=parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets"));
    client().prepareIndex("test", "parent-type", "parent-id").setSource("searchText", "quick brown fox").get();
    client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("searchText", "quick brown fox").get();
    refresh();
    String[] highlightTypes = new String[] { "plain", "fvh", "postings" };
    for (String highlightType : highlightTypes) {
        logger.info("Testing with highlight type [{}]", highlightType);
        SearchResponse searchResponse = client().prepareSearch("test").setQuery(new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")).must(new HasChildQueryBuilder("child-type", new MatchAllQueryBuilder(), ScoreMode.None))).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))).get();
        assertHitCount(searchResponse, 1);
        assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("parent-id"));
        HighlightField highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText");
        assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>"));
        searchResponse = client().prepareSearch("test").setQuery(new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")).must(new HasParentQueryBuilder("parent-type", new MatchAllQueryBuilder(), false))).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))).get();
        assertHitCount(searchResponse, 1);
        assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("child-id"));
        highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText");
        assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>"));
    }
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Matchers.containsString(org.hamcrest.Matchers.containsString) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)

Example 15 with HighlightBuilder

use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class SearchService method parseSource.

private void parseSource(DefaultSearchContext context, SearchSourceBuilder source) throws SearchContextException {
    // nothing to parse...
    if (source == null) {
        return;
    }
    QueryShardContext queryShardContext = context.getQueryShardContext();
    context.from(source.from());
    context.size(source.size());
    Map<String, InnerHitBuilder> innerHitBuilders = new HashMap<>();
    if (source.query() != null) {
        InnerHitBuilder.extractInnerHits(source.query(), innerHitBuilders);
        context.parsedQuery(queryShardContext.toQuery(source.query()));
    }
    if (source.postFilter() != null) {
        InnerHitBuilder.extractInnerHits(source.postFilter(), innerHitBuilders);
        context.parsedPostFilter(queryShardContext.toQuery(source.postFilter()));
    }
    if (innerHitBuilders.size() > 0) {
        for (Map.Entry<String, InnerHitBuilder> entry : innerHitBuilders.entrySet()) {
            try {
                entry.getValue().build(context, context.innerHits());
            } catch (IOException e) {
                throw new SearchContextException(context, "failed to build inner_hits", e);
            }
        }
    }
    if (source.sorts() != null) {
        try {
            Optional<SortAndFormats> optionalSort = SortBuilder.buildSort(source.sorts(), context.getQueryShardContext());
            if (optionalSort.isPresent()) {
                context.sort(optionalSort.get());
            }
        } catch (IOException e) {
            throw new SearchContextException(context, "failed to create sort elements", e);
        }
    }
    context.trackScores(source.trackScores());
    if (source.minScore() != null) {
        context.minimumScore(source.minScore());
    }
    if (source.profile()) {
        context.setProfilers(new Profilers(context.searcher()));
    }
    if (source.timeout() != null) {
        context.timeout(source.timeout());
    }
    context.terminateAfter(source.terminateAfter());
    if (source.aggregations() != null) {
        try {
            AggregatorFactories factories = source.aggregations().build(context, null);
            factories.validate();
            context.aggregations(new SearchContextAggregations(factories));
        } catch (IOException e) {
            throw new AggregationInitializationException("Failed to create aggregators", e);
        }
    }
    if (source.suggest() != null) {
        try {
            context.suggest(source.suggest().build(queryShardContext));
        } catch (IOException e) {
            throw new SearchContextException(context, "failed to create SuggestionSearchContext", e);
        }
    }
    if (source.rescores() != null) {
        try {
            for (RescoreBuilder<?> rescore : source.rescores()) {
                context.addRescore(rescore.build(queryShardContext));
            }
        } catch (IOException e) {
            throw new SearchContextException(context, "failed to create RescoreSearchContext", e);
        }
    }
    if (source.explain() != null) {
        context.explain(source.explain());
    }
    if (source.fetchSource() != null) {
        context.fetchSourceContext(source.fetchSource());
    }
    if (source.docValueFields() != null) {
        context.docValueFieldsContext(new DocValueFieldsContext(source.docValueFields()));
    }
    if (source.highlighter() != null) {
        HighlightBuilder highlightBuilder = source.highlighter();
        try {
            context.highlight(highlightBuilder.build(queryShardContext));
        } catch (IOException e) {
            throw new SearchContextException(context, "failed to create SearchContextHighlighter", e);
        }
    }
    if (source.scriptFields() != null) {
        for (org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField field : source.scriptFields()) {
            SearchScript searchScript = scriptService.search(context.lookup(), field.script(), ScriptContext.Standard.SEARCH);
            context.scriptFields().add(new ScriptField(field.fieldName(), searchScript, field.ignoreFailure()));
        }
    }
    if (source.ext() != null) {
        for (SearchExtBuilder searchExtBuilder : source.ext()) {
            context.addSearchExt(searchExtBuilder);
        }
    }
    if (source.version() != null) {
        context.version(source.version());
    }
    if (source.stats() != null) {
        context.groupStats(source.stats());
    }
    if (source.searchAfter() != null && source.searchAfter().length > 0) {
        if (context.scrollContext() != null) {
            throw new SearchContextException(context, "`search_after` cannot be used in a scroll context.");
        }
        if (context.from() > 0) {
            throw new SearchContextException(context, "`from` parameter must be set to 0 when `search_after` is used.");
        }
        FieldDoc fieldDoc = SearchAfterBuilder.buildFieldDoc(context.sort(), source.searchAfter());
        context.searchAfter(fieldDoc);
    }
    if (source.slice() != null) {
        if (context.scrollContext() == null) {
            throw new SearchContextException(context, "`slice` cannot be used outside of a scroll context");
        }
        context.sliceBuilder(source.slice());
    }
    if (source.storedFields() != null) {
        if (source.storedFields().fetchFields() == false) {
            if (context.version()) {
                throw new SearchContextException(context, "`stored_fields` cannot be disabled if version is requested");
            }
            if (context.sourceRequested()) {
                throw new SearchContextException(context, "`stored_fields` cannot be disabled if _source is requested");
            }
        }
        context.storedFieldsContext(source.storedFields());
    }
    if (source.collapse() != null) {
        final CollapseContext collapseContext = source.collapse().build(context);
        context.collapse(collapseContext);
    }
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) HashMap(java.util.HashMap) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) Profilers(org.elasticsearch.search.profile.Profilers) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ScriptField(org.elasticsearch.search.fetch.subphase.ScriptFieldsContext.ScriptField) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) AggregatorFactories(org.elasticsearch.search.aggregations.AggregatorFactories) DocValueFieldsContext(org.elasticsearch.search.fetch.subphase.DocValueFieldsContext) SearchContextAggregations(org.elasticsearch.search.aggregations.SearchContextAggregations) AggregationInitializationException(org.elasticsearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) SortAndFormats(org.elasticsearch.search.sort.SortAndFormats) SearchScript(org.elasticsearch.script.SearchScript) Map(java.util.Map) HashMap(java.util.HashMap) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) CollapseContext(org.elasticsearch.search.collapse.CollapseContext)

Aggregations

HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)15 SearchResponse (org.elasticsearch.action.search.SearchResponse)14 Script (org.elasticsearch.script.Script)5 SearchHits (org.elasticsearch.search.SearchHits)5 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)4 HashMap (java.util.HashMap)3 SearchHit (org.elasticsearch.search.SearchHit)3 SearchHitField (org.elasticsearch.search.SearchHitField)3 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)3 ArrayList (java.util.ArrayList)2 Explanation (org.apache.lucene.search.Explanation)2 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)2 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)2 Field (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)2 IOException (java.io.IOException)1 Map (java.util.Map)1 FieldDoc (org.apache.lucene.search.FieldDoc)1 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)1