Search in sources :

Example 11 with HighlightField

use of org.elasticsearch.search.fetch.subphase.highlight.HighlightField 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 12 with HighlightField

use of org.elasticsearch.search.fetch.subphase.highlight.HighlightField in project vertigo by KleeGroup.

the class ESFacetedQueryResultBuilder method createHighlight.

private static Map<DtField, String> createHighlight(final SearchHit searchHit, final DtDefinition resultDtDefinition) {
    final Map<DtField, String> highlights = new HashMap<>();
    final Map<String, HighlightField> highlightsMap = searchHit.getHighlightFields();
    for (final Map.Entry<String, HighlightField> entry : highlightsMap.entrySet()) {
        final String fieldName = entry.getKey();
        if (resultDtDefinition.contains(fieldName)) {
            // We only keep highlighs match on result's fields
            final DtField dtField = resultDtDefinition.getField(fieldName);
            final StringBuilder sb = new StringBuilder();
            for (final Text fragment : entry.getValue().getFragments()) {
                sb.append("<hlfrag>").append(fragment).append("</hlfrag>");
            }
            highlights.put(dtField, sb.toString());
        }
    }
    return highlights;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Text(org.elasticsearch.common.text.Text) MessageText(io.vertigo.core.locale.MessageText) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DtField(io.vertigo.dynamo.domain.metamodel.DtField)

Aggregations

HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)12 HashMap (java.util.HashMap)7 Map (java.util.Map)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Text (org.elasticsearch.common.text.Text)5 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)5 ArrayList (java.util.ArrayList)4 SearchHits (org.elasticsearch.search.SearchHits)4 SearchHit (org.elasticsearch.search.SearchHit)3 SearchHitField (org.elasticsearch.search.SearchHitField)3 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)3 HighlightField.readHighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField.readHighlightField)3 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)3 Collections.emptyMap (java.util.Collections.emptyMap)2 Collections.singletonMap (java.util.Collections.singletonMap)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Explanation (org.apache.lucene.search.Explanation)2 Script (org.elasticsearch.script.Script)2