Search in sources :

Example 1 with HighlightBuilder

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder in project OpenSearch by opensearch-project.

the class SearchIT method testMultiSearch_withQuery.

public void testMultiSearch_withQuery() throws Exception {
    MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
    SearchRequest searchRequest1 = new SearchRequest("index1");
    searchRequest1.source().query(new TermsQueryBuilder("field", "value2"));
    multiSearchRequest.add(searchRequest1);
    SearchRequest searchRequest2 = new SearchRequest("index2");
    searchRequest2.source().query(new TermsQueryBuilder("field", "value2"));
    multiSearchRequest.add(searchRequest2);
    SearchRequest searchRequest3 = new SearchRequest("index3");
    searchRequest3.source().query(new TermsQueryBuilder("field", "value2"));
    multiSearchRequest.add(searchRequest3);
    MultiSearchResponse multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync);
    assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L));
    assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(3));
    assertThat(multiSearchResponse.getResponses()[0].getFailure(), Matchers.nullValue());
    assertThat(multiSearchResponse.getResponses()[0].isFailure(), Matchers.is(false));
    SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[0].getResponse());
    assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L));
    assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("2"));
    assertThat(multiSearchResponse.getResponses()[1].getFailure(), Matchers.nullValue());
    assertThat(multiSearchResponse.getResponses()[1].isFailure(), Matchers.is(false));
    SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[1].getResponse());
    assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L));
    assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("4"));
    assertThat(multiSearchResponse.getResponses()[2].getFailure(), Matchers.nullValue());
    assertThat(multiSearchResponse.getResponses()[2].isFailure(), Matchers.is(false));
    SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[2].getResponse());
    assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L));
    assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("6"));
    searchRequest1.source().highlighter(new HighlightBuilder().field("field"));
    searchRequest2.source().highlighter(new HighlightBuilder().field("field"));
    searchRequest3.source().highlighter(new HighlightBuilder().field("field"));
    multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync);
    assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L));
    assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(3));
    assertThat(multiSearchResponse.getResponses()[0].getFailure(), Matchers.nullValue());
    assertThat(multiSearchResponse.getResponses()[0].isFailure(), Matchers.is(false));
    SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[0].getResponse());
    assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L));
    assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), Matchers.equalTo("<em>value2</em>"));
    assertThat(multiSearchResponse.getResponses()[1].getFailure(), Matchers.nullValue());
    assertThat(multiSearchResponse.getResponses()[1].isFailure(), Matchers.is(false));
    SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[1].getResponse());
    assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L));
    assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("4"));
    assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), Matchers.equalTo("<em>value2</em>"));
    assertThat(multiSearchResponse.getResponses()[2].getFailure(), Matchers.nullValue());
    assertThat(multiSearchResponse.getResponses()[2].isFailure(), Matchers.is(false));
    SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[2].getResponse());
    assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L));
    assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("6"));
    assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), Matchers.equalTo("<em>value2</em>"));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) HighlightBuilder(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 2 with HighlightBuilder

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder in project OpenSearch by opensearch-project.

the class HighlighterWithAnalyzersTests method testMultiPhraseCutoff.

public void testMultiPhraseCutoff() throws IOException {
    /*
         * MultiPhraseQuery can literally kill an entire node if there are too many terms in the
         * query. We cut off and extract terms if there are more than 16 terms in the query
         */
    assertAcked(prepareCreate("test").addMapping("test", "body", "type=text,analyzer=custom_analyzer," + "search_analyzer=custom_analyzer,term_vector=with_positions_offsets").setSettings(Settings.builder().put(indexSettings()).put("analysis.filter.wordDelimiter.type", "word_delimiter").put("analysis.filter.wordDelimiter.type.split_on_numerics", false).put("analysis.filter.wordDelimiter.generate_word_parts", true).put("analysis.filter.wordDelimiter.generate_number_parts", true).put("analysis.filter.wordDelimiter.catenate_words", true).put("analysis.filter.wordDelimiter.catenate_numbers", true).put("analysis.filter.wordDelimiter.catenate_all", false).put("analysis.analyzer.custom_analyzer.tokenizer", "whitespace").putList("analysis.analyzer.custom_analyzer.filter", "lowercase", "wordDelimiter")));
    ensureGreen();
    client().prepareIndex("test").setId("1").setSource("body", "Test: http://www.facebook.com http://elasticsearch.org " + "http://xing.com http://cnn.com http://quora.com http://twitter.com this is " + "a test for highlighting feature Test: http://www.facebook.com " + "http://elasticsearch.org http://xing.com http://cnn.com http://quora.com " + "http://twitter.com this is a test for highlighting feature").get();
    refresh();
    SearchResponse search = client().prepareSearch().setQuery(matchPhraseQuery("body", "Test: http://www.facebook.com ")).highlighter(new HighlightBuilder().field("body").highlighterType("fvh")).get();
    assertHighlight(search, 0, "body", 0, startsWith("<em>Test: http://www.facebook.com</em>"));
    search = client().prepareSearch().setQuery(matchPhraseQuery("body", "Test: http://www.facebook.com " + "http://elasticsearch.org http://xing.com http://cnn.com " + "http://quora.com http://twitter.com this is a test for highlighting " + "feature Test: http://www.facebook.com http://elasticsearch.org " + "http://xing.com http://cnn.com http://quora.com http://twitter.com this " + "is a test for highlighting feature")).highlighter(new HighlightBuilder().field("body").highlighterType("fvh")).execute().actionGet();
    assertHighlight(search, 0, "body", 0, equalTo("<em>Test</em>: " + "<em>http://www.facebook.com</em> <em>http://elasticsearch.org</em> " + "<em>http://xing.com</em> <em>http://cnn.com</em> http://quora.com"));
}
Also used : HighlightBuilder(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with HighlightBuilder

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder in project OpenSearch by opensearch-project.

the class HighlighterWithAnalyzersTests method testNgramHighlightingWithBrokenPositions.

public void testNgramHighlightingWithBrokenPositions() throws IOException {
    assertAcked(prepareCreate("test").addMapping("test", jsonBuilder().startObject().startObject("test").startObject("properties").startObject("name").field("type", "text").startObject("fields").startObject("autocomplete").field("type", "text").field("analyzer", "autocomplete").field("search_analyzer", "search_autocomplete").field("term_vector", "with_positions_offsets").endObject().endObject().endObject().endObject().endObject().endObject()).setSettings(Settings.builder().put(indexSettings()).put(IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey(), 19).put("analysis.tokenizer.autocomplete.max_gram", 20).put("analysis.tokenizer.autocomplete.min_gram", 1).put("analysis.tokenizer.autocomplete.token_chars", "letter,digit").put("analysis.tokenizer.autocomplete.type", "ngram").put("analysis.filter.wordDelimiter.type", "word_delimiter").putList("analysis.filter.wordDelimiter.type_table", "& => ALPHANUM", "| => ALPHANUM", "! => ALPHANUM", "? => ALPHANUM", ". => ALPHANUM", "- => ALPHANUM", "# => ALPHANUM", "% => ALPHANUM", "+ => ALPHANUM", ", => ALPHANUM", "~ => ALPHANUM", ": => ALPHANUM", "/ => ALPHANUM", "^ => ALPHANUM", "$ => ALPHANUM", "@ => ALPHANUM", ") => ALPHANUM", "( => ALPHANUM", "] => ALPHANUM", "[ => ALPHANUM", "} => ALPHANUM", "{ => ALPHANUM").put("analysis.filter.wordDelimiter.type.split_on_numerics", false).put("analysis.filter.wordDelimiter.generate_word_parts", true).put("analysis.filter.wordDelimiter.generate_number_parts", false).put("analysis.filter.wordDelimiter.catenate_words", true).put("analysis.filter.wordDelimiter.catenate_numbers", true).put("analysis.filter.wordDelimiter.catenate_all", false).put("analysis.analyzer.autocomplete.tokenizer", "autocomplete").putList("analysis.analyzer.autocomplete.filter", "lowercase", "wordDelimiter").put("analysis.analyzer.search_autocomplete.tokenizer", "whitespace").putList("analysis.analyzer.search_autocomplete.filter", "lowercase", "wordDelimiter")));
    client().prepareIndex("test").setId("1").setSource("name", "ARCOTEL Hotels Deutschland").get();
    refresh();
    SearchResponse search = client().prepareSearch("test").setQuery(matchQuery("name.autocomplete", "deut tel").operator(Operator.OR)).highlighter(new HighlightBuilder().field("name.autocomplete")).get();
    assertHighlight(search, 0, "name.autocomplete", 0, equalTo("ARCO<em>TEL</em> Ho<em>tel</em>s <em>Deut</em>schland"));
}
Also used : HighlightBuilder(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with HighlightBuilder

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder in project OpenSearch by opensearch-project.

the class CCSDuelIT method testHighlighting.

public void testHighlighting() throws Exception {
    assumeMultiClusterSetup();
    SearchRequest searchRequest = initSearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.highlighter(new HighlightBuilder().field("tags"));
    sourceBuilder.query(QueryBuilders.matchQuery("tags", "xml"));
    searchRequest.source(sourceBuilder);
    duelSearch(searchRequest, response -> {
        assertHits(response);
        assertFalse(response.getHits().getHits()[0].getHighlightFields().isEmpty());
    });
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) HighlightBuilder(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 5 with HighlightBuilder

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder in project OpenSearch by opensearch-project.

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().value, 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().value, 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().value, 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.opensearch.search.fetch.subphase.highlight.HighlightBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

HighlightBuilder (org.opensearch.search.fetch.subphase.highlight.HighlightBuilder)18 SearchResponse (org.opensearch.action.search.SearchResponse)13 SearchHits (org.opensearch.search.SearchHits)6 HighlightField (org.opensearch.search.fetch.subphase.highlight.HighlightField)6 Script (org.opensearch.script.Script)5 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)5 DocumentField (org.opensearch.common.document.DocumentField)4 SearchHit (org.opensearch.search.SearchHit)4 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)3 SearchRequest (org.opensearch.action.search.SearchRequest)3 InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)3 ArrayList (java.util.ArrayList)2 Explanation (org.apache.lucene.search.Explanation)2 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)2 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)2 AggregatorFactories (org.opensearch.search.aggregations.AggregatorFactories)2 OpenSearchAssertions.assertSearchHits (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHits)2 IOException (java.io.IOException)1