Search in sources :

Example 6 with HighlightField

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

the class SearchHit method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeFloat(score);
    out.writeOptionalText(id);
    out.writeOptionalText(type);
    out.writeOptionalWriteable(nestedIdentity);
    out.writeLong(version);
    out.writeBytesReference(source);
    if (explanation == null) {
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        writeExplanation(out, explanation);
    }
    if (fields == null) {
        out.writeVInt(0);
    } else {
        out.writeVInt(fields.size());
        for (SearchHitField hitField : getFields().values()) {
            hitField.writeTo(out);
        }
    }
    if (highlightFields == null) {
        out.writeVInt(0);
    } else {
        out.writeVInt(highlightFields.size());
        for (HighlightField highlightField : highlightFields.values()) {
            highlightField.writeTo(out);
        }
    }
    sortValues.writeTo(out);
    if (matchedQueries.length == 0) {
        out.writeVInt(0);
    } else {
        out.writeVInt(matchedQueries.length);
        for (String matchedFilter : matchedQueries) {
            out.writeString(matchedFilter);
        }
    }
    out.writeOptionalWriteable(shard);
    if (innerHits == null) {
        out.writeVInt(0);
    } else {
        out.writeVInt(innerHits.size());
        for (Map.Entry<String, SearchHits> entry : innerHits.entrySet()) {
            out.writeString(entry.getKey());
            entry.getValue().writeTo(out);
        }
    }
}
Also used : HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) HighlightField.readHighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField.readHighlightField) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 7 with HighlightField

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

the class SearchHit method toInnerXContent.

// public because we render hit as part of completion suggestion option
public XContentBuilder toInnerXContent(XContentBuilder builder, Params params) throws IOException {
    List<SearchHitField> metaFields = new ArrayList<>();
    List<SearchHitField> otherFields = new ArrayList<>();
    if (fields != null && !fields.isEmpty()) {
        for (SearchHitField field : fields.values()) {
            if (field.getValues().isEmpty()) {
                continue;
            }
            if (field.isMetadataField()) {
                metaFields.add(field);
            } else {
                otherFields.add(field);
            }
        }
    }
    // Even if this was included in the inner_hit hits this would be the same, so better leave it out.
    if (getExplanation() != null && shard != null) {
        builder.field(Fields._SHARD, shard.getShardId());
        builder.field(Fields._NODE, shard.getNodeIdText());
    }
    if (nestedIdentity != null) {
        nestedIdentity.toXContent(builder, params);
    } else {
        if (index != null) {
            builder.field(Fields._INDEX, index);
        }
        if (type != null) {
            builder.field(Fields._TYPE, type);
        }
        if (id != null) {
            builder.field(Fields._ID, id);
        }
    }
    if (version != -1) {
        builder.field(Fields._VERSION, version);
    }
    if (Float.isNaN(score)) {
        builder.nullField(Fields._SCORE);
    } else {
        builder.field(Fields._SCORE, score);
    }
    for (SearchHitField field : metaFields) {
        Object value = field.getValue();
        builder.field(field.getName(), value);
    }
    if (source != null) {
        XContentHelper.writeRawField(SourceFieldMapper.NAME, source, builder, params);
    }
    if (!otherFields.isEmpty()) {
        builder.startObject(Fields.FIELDS);
        for (SearchHitField field : otherFields) {
            builder.startArray(field.getName());
            for (Object value : field.getValues()) {
                builder.value(value);
            }
            builder.endArray();
        }
        builder.endObject();
    }
    if (highlightFields != null && !highlightFields.isEmpty()) {
        builder.startObject(Fields.HIGHLIGHT);
        for (HighlightField field : highlightFields.values()) {
            field.toXContent(builder, params);
        }
        builder.endObject();
    }
    sortValues.toXContent(builder, params);
    if (matchedQueries.length > 0) {
        builder.startArray(Fields.MATCHED_QUERIES);
        for (String matchedFilter : matchedQueries) {
            builder.value(matchedFilter);
        }
        builder.endArray();
    }
    if (getExplanation() != null) {
        builder.field(Fields._EXPLANATION);
        buildExplanation(builder, getExplanation());
    }
    if (innerHits != null) {
        builder.startObject(Fields.INNER_HITS);
        for (Map.Entry<String, SearchHits> entry : innerHits.entrySet()) {
            builder.startObject(entry.getKey());
            entry.getValue().toXContent(builder, params);
            builder.endObject();
        }
        builder.endObject();
    }
    return builder;
}
Also used : ArrayList(java.util.ArrayList) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) HighlightField.readHighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField.readHighlightField) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 8 with HighlightField

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

the class TopHitsIT method testTopHitsInNested.

public void testTopHitsInNested() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("articles").addAggregation(histogram("dates").field("date").interval(5).order(Histogram.Order.aggregation("to-comments", true)).subAggregation(nested("to-comments", "comments").subAggregation(topHits("comments").highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "text")))).sort("comments.id", SortOrder.ASC)))).get();
    Histogram histogram = searchResponse.getAggregations().get("dates");
    for (int i = 0; i < numArticles; i += 5) {
        Histogram.Bucket bucket = histogram.getBuckets().get(i / 5);
        assertThat(bucket.getDocCount(), equalTo(5L));
        long numNestedDocs = 10 + (5 * i);
        Nested nested = bucket.getAggregations().get("to-comments");
        assertThat(nested.getDocCount(), equalTo(numNestedDocs));
        TopHits hits = nested.getAggregations().get("comments");
        SearchHits searchHits = hits.getHits();
        assertThat(searchHits.getTotalHits(), equalTo(numNestedDocs));
        for (int j = 0; j < 3; j++) {
            assertThat(searchHits.getAt(j).getNestedIdentity().getField().string(), equalTo("comments"));
            assertThat(searchHits.getAt(j).getNestedIdentity().getOffset(), equalTo(0));
            assertThat((Integer) searchHits.getAt(j).getSourceAsMap().get("id"), equalTo(0));
            HighlightField highlightField = searchHits.getAt(j).getHighlightFields().get("comments.message");
            assertThat(highlightField.getFragments().length, equalTo(1));
            assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em>"));
        }
    }
}
Also used : SearchHitField(org.elasticsearch.search.SearchHitField) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) 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 9 with HighlightField

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

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

the class CustomHighlighter method highlight.

@Override
public HighlightField highlight(HighlighterContext highlighterContext) {
    SearchContextHighlight.Field field = highlighterContext.field;
    CacheEntry cacheEntry = (CacheEntry) highlighterContext.hitContext.cache().get("test-custom");
    final int docId = highlighterContext.hitContext.readerContext().docBase + highlighterContext.hitContext.docId();
    if (cacheEntry == null) {
        cacheEntry = new CacheEntry();
        highlighterContext.hitContext.cache().put("test-custom", cacheEntry);
        cacheEntry.docId = docId;
        cacheEntry.position = 1;
    } else {
        if (cacheEntry.docId == docId) {
            cacheEntry.position++;
        } else {
            cacheEntry.docId = docId;
            cacheEntry.position = 1;
        }
    }
    List<Text> responses = new ArrayList<>();
    responses.add(new Text(String.format(Locale.ENGLISH, "standard response for %s at position %s", field.field(), cacheEntry.position)));
    if (field.fieldOptions().options() != null) {
        for (Map.Entry<String, Object> entry : field.fieldOptions().options().entrySet()) {
            responses.add(new Text("field:" + entry.getKey() + ":" + entry.getValue()));
        }
    }
    return new HighlightField(highlighterContext.fieldName, responses.toArray(new Text[] {}));
}
Also used : ArrayList(java.util.ArrayList) SearchContextHighlight(org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight) Text(org.elasticsearch.common.text.Text) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Map(java.util.Map)

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