Search in sources :

Example 1 with HighlightBuilder

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

the class PercolatorQuerySearchIT method testPercolatorQueryWithHighlighting.

public void testPercolatorQueryWithHighlighting() throws Exception {
    StringBuilder fieldMapping = new StringBuilder("type=text").append(",store=").append(randomBoolean());
    if (randomBoolean()) {
        fieldMapping.append(",term_vector=with_positions_offsets");
    } else if (randomBoolean()) {
        fieldMapping.append(",index_options=offsets");
    }
    createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type", "field1", fieldMapping).addMapping("queries", "query", "type=percolator"));
    client().prepareIndex("test", "queries", "1").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "brown fox")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "2").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "lazy dog")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "3").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "jumps")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "4").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "dog")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "5").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "fox")).endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().get();
    BytesReference document = jsonBuilder().startObject().field("field1", "The quick brown fox jumps over the lazy dog").endObject().bytes();
    SearchResponse searchResponse = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", "type", document, XContentType.JSON)).highlighter(new HighlightBuilder().field("field1")).addSort("_uid", SortOrder.ASC).get();
    assertHitCount(searchResponse, 5);
    assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick <em>brown</em> <em>fox</em> jumps over the lazy dog"));
    assertThat(searchResponse.getHits().getAt(1).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the <em>lazy</em> <em>dog</em>"));
    assertThat(searchResponse.getHits().getAt(2).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox <em>jumps</em> over the lazy dog"));
    assertThat(searchResponse.getHits().getAt(3).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the lazy <em>dog</em>"));
    assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown <em>fox</em> jumps over the lazy dog"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 2 with HighlightBuilder

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

the class ExternalValuesMapperIntegrationIT method testHighlightingOnCustomString.

public void testHighlightingOnCustomString() throws Exception {
    prepareCreate("test-idx").addMapping("type", XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", FakeStringFieldMapper.CONTENT_TYPE).endObject().endObject().endObject().endObject()).execute().get();
    index("test-idx", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", "Every day is exactly the same").endObject());
    refresh();
    SearchResponse response;
    // test if the highlighting is excluded when we use wildcards
    response = client().prepareSearch("test-idx").setQuery(QueryBuilders.matchQuery("field", "exactly the same")).highlighter(new HighlightBuilder().field("*")).execute().actionGet();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getHighlightFields().size(), equalTo(0));
    // make sure it is not excluded when we explicitly provide the fieldname
    response = client().prepareSearch("test-idx").setQuery(QueryBuilders.matchQuery("field", "exactly the same")).highlighter(new HighlightBuilder().field("field")).execute().actionGet();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getHighlightFields().size(), equalTo(1));
    assertThat(response.getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), equalTo("Every day is " + "<em>exactly</em> <em>the</em> <em>same</em>"));
    // make sure it is not excluded when we explicitly provide the fieldname and a wildcard
    response = client().prepareSearch("test-idx").setQuery(QueryBuilders.matchQuery("field", "exactly the same")).highlighter(new HighlightBuilder().field("*").field("field")).execute().actionGet();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getHighlightFields().size(), equalTo(1));
    assertThat(response.getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), equalTo("Every day is " + "<em>exactly</em> <em>the</em> <em>same</em>"));
}
Also used : HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with HighlightBuilder

use of org.graylog.shaded.elasticsearch6.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 4 with HighlightBuilder

use of org.graylog.shaded.elasticsearch6.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 5 with HighlightBuilder

use of org.graylog.shaded.elasticsearch6.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)

Aggregations

HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)23 SearchResponse (org.elasticsearch.action.search.SearchResponse)16 SearchHits (org.elasticsearch.search.SearchHits)7 ArrayList (java.util.ArrayList)5 Script (org.elasticsearch.script.Script)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 Map (java.util.Map)4 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)4 SearchHit (org.elasticsearch.search.SearchHit)4 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)4 HashMap (java.util.HashMap)3 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3 SearchHitField (org.elasticsearch.search.SearchHitField)3 IOException (java.io.IOException)2 Date (java.util.Date)2 List (java.util.List)2 ServiceResponse (net.yacy.grid.http.ServiceResponse)2 ElasticsearchClient (net.yacy.grid.io.index.ElasticsearchClient)2 Sort (net.yacy.grid.io.index.Sort)2