Search in sources :

Example 1 with HighlightBuilder

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

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

the class InnerHitsIT method testSimpleParentChild.

public void testSimpleParentChild() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", "title", "type=text").addMapping("comment", "_parent", "type=article", "message", "type=text,fielddata=true"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource("title", "quick brown fox"));
    requests.add(client().prepareIndex("articles", "comment", "1").setParent("1").setSource("message", "fox eat quick"));
    requests.add(client().prepareIndex("articles", "comment", "2").setParent("1").setSource("message", "fox ate rabbit x y z"));
    requests.add(client().prepareIndex("articles", "comment", "3").setParent("1").setSource("message", "rabbit got away"));
    requests.add(client().prepareIndex("articles", "article", "2").setSource("title", "big gray elephant"));
    requests.add(client().prepareIndex("articles", "comment", "4").setParent("2").setSource("message", "elephant captured"));
    requests.add(client().prepareIndex("articles", "comment", "5").setParent("2").setSource("message", "mice squashed by elephant x"));
    requests.add(client().prepareIndex("articles", "comment", "6").setParent("2").setSource("message", "elephant scared by mice x y"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("1"));
    assertThat(response.getHits().getAt(0).getShard(), notNullValue());
    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.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getType(), equalTo("comment"));
    assertThat(innerHits.getAt(1).getId(), equalTo("2"));
    assertThat(innerHits.getAt(1).getType(), equalTo("comment"));
    response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", matchQuery("message", "elephant"), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(3L));
    assertThat(innerHits.getAt(0).getId(), equalTo("4"));
    assertThat(innerHits.getAt(0).getType(), equalTo("comment"));
    assertThat(innerHits.getAt(1).getId(), equalTo("5"));
    assertThat(innerHits.getAt(1).getType(), equalTo("comment"));
    assertThat(innerHits.getAt(2).getId(), equalTo("6"));
    assertThat(innerHits.getAt(2).getType(), equalTo("comment"));
    response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit(new InnerHitBuilder().addDocValueField("message").setHighlightBuilder(new HighlightBuilder().field("message")).setExplain(true).setSize(1).addScriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())), false)).get();
    assertNoFailures(response);
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getHighlightFields().get("message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
    assertThat(innerHits.getAt(0).getExplanation().toString(), containsString("weight(message:fox"));
    assertThat(innerHits.getAt(0).getFields().get("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 3 with HighlightBuilder

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

the class CustomHighlighterSearchIT method testThatCustomHighlighterReceivesFieldsInOrder.

public void testThatCustomHighlighterReceivesFieldsInOrder() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("test").setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).should(QueryBuilders.termQuery("name", "arbitrary"))).highlighter(new HighlightBuilder().highlighterType("test-custom").field("name").field("other_name").field("other_other_name").useExplicitFieldOrder(true)).get();
    assertHighlight(searchResponse, 0, "name", 0, equalTo("standard response for name at position 1"));
    assertHighlight(searchResponse, 0, "other_name", 0, equalTo("standard response for other_name at position 2"));
    assertHighlight(searchResponse, 0, "other_other_name", 0, equalTo("standard response for other_other_name at position 3"));
    assertHighlight(searchResponse, 1, "name", 0, equalTo("standard response for name at position 1"));
    assertHighlight(searchResponse, 1, "other_name", 0, equalTo("standard response for other_name at position 2"));
    assertHighlight(searchResponse, 1, "other_other_name", 0, equalTo("standard response for other_other_name at position 3"));
}
Also used : HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with HighlightBuilder

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

the class CustomHighlighterSearchIT method testThatCustomHighlighterCanBeConfiguredGlobally.

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

Example 5 with HighlightBuilder

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

the class CustomHighlighterSearchIT method testThatCustomHighlightersAreSupported.

public void testThatCustomHighlightersAreSupported() throws IOException {
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("test").setQuery(QueryBuilders.matchAllQuery()).highlighter(new HighlightBuilder().field("name").highlighterType("test-custom")).execute().actionGet();
    assertHighlight(searchResponse, 0, "name", 0, equalTo("standard response for name at position 1"));
}
Also used : HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

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