Search in sources :

Example 21 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method testGeoFieldHighlightingWithDifferentHighlighters.

public void testGeoFieldHighlightingWithDifferentHighlighters() throws IOException {
    // check that we do not get an exception for geo_point fields in case someone tries to highlight
    // it accidentially with a wildcard
    // see https://github.com/elastic/elasticsearch/issues/17537
    XContentBuilder mappings = jsonBuilder();
    mappings.startObject();
    mappings.startObject("type").startObject("properties").startObject("geo_point").field("type", "geo_point").endObject().startObject("text").field("type", "text").field("term_vector", "with_positions_offsets_payloads").field("index_options", "offsets").endObject().endObject().endObject();
    mappings.endObject();
    assertAcked(prepareCreate("test").addMapping("type", mappings));
    client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("text", "Arbitrary text field which will should not cause a failure").endObject()).get();
    refresh();
    String highlighterType = randomFrom("plain", "fvh", "postings", "unified");
    QueryBuilder query = QueryBuilders.boolQuery().should(QueryBuilders.geoBoundingBoxQuery("geo_point").setCorners(61.10078883158897, -170.15625, -64.92354174306496, 118.47656249999999)).should(QueryBuilders.termQuery("text", "failure"));
    SearchResponse search = client().prepareSearch().setSource(new SearchSourceBuilder().query(query).highlighter(new HighlightBuilder().field("*").highlighterType(highlighterType))).get();
    assertNoFailures(search);
    assertThat(search.getHits().getTotalHits(), equalTo(1L));
    assertThat(search.getHits().getAt(0).getHighlightFields().get("text").fragments().length, equalTo(1));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 22 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method testHighlighterWithSentenceBoundaryScannerAndLocale.

public void testHighlighterWithSentenceBoundaryScannerAndLocale() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping()));
    ensureGreen();
    indexRandom(true, client().prepareIndex("test", "type1").setSource("field1", "A sentence with few words. Another sentence with even more words."));
    for (String type : new String[] { "fvh", "unified" }) {
        logger.info("--> highlighting and searching on 'field' with sentence boundary_scanner");
        SearchSourceBuilder source = searchSource().query(termQuery("field1", "sentence")).highlighter(highlight().field("field1", 21, 2).highlighterType(type).preTags("<xxx>").postTags("</xxx>").boundaryScannerType(BoundaryScannerType.SENTENCE).boundaryScannerLocale(Locale.ENGLISH.toLanguageTag()));
        SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
        assertHighlight(searchResponse, 0, "field1", 0, 2, anyOf(equalTo("A <xxx>sentence</xxx> with few words"), equalTo("A <xxx>sentence</xxx> with few words. ")));
        assertHighlight(searchResponse, 0, "field1", 1, 2, anyOf(equalTo("Another <xxx>sentence</xxx> with"), equalTo("Another <xxx>sentence</xxx> with even more words. ")));
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 23 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method testPostingsHighlighterNumberOfFragments.

public void testPostingsHighlighterNumberOfFragments() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource("field1", "The quick brown fox jumps over the lazy dog. The lazy red fox jumps over the quick dog. " + "The quick brown dog jumps over the lazy fox.", "field2", "The quick brown fox jumps over the lazy dog. The lazy red fox jumps over the quick dog. " + "The quick brown dog jumps over the lazy fox.").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        logger.info("--> highlighting and searching on field1");
        SearchSourceBuilder source = searchSource().query(termQuery("field1", "fox")).highlighter(highlight().field(new Field("field1").numOfFragments(5).preTags("<field1>").postTags("</field1>").highlighterType(type)));
        SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet();
        assertHighlight(searchResponse, 0, "field1", 0, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog."));
        assertHighlight(searchResponse, 0, "field1", 1, equalTo("The lazy red <field1>fox</field1> jumps over the quick dog."));
        assertHighlight(searchResponse, 0, "field1", 2, 3, equalTo("The quick brown dog jumps over the lazy <field1>fox</field1>."));
        client().prepareIndex("test", "type1", "2").setSource("field1", new String[] { "The quick brown fox jumps over the lazy dog. Second sentence not finished", "The lazy red fox jumps over the quick dog.", "The quick brown dog jumps over the lazy fox." }).get();
        refresh();
        source = searchSource().query(termQuery("field1", "fox")).highlighter(highlight().field(new Field("field1").numOfFragments(0).preTags("<field1>").postTags("</field1>").highlighterType(type)));
        searchResponse = client().search(searchRequest("test").source(source)).actionGet();
        assertHitCount(searchResponse, 2L);
        for (SearchHit searchHit : searchResponse.getHits()) {
            if ("1".equals(searchHit.getId())) {
                assertHighlight(searchHit, "field1", 0, 1, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog. " + "The lazy red <field1>fox</field1> jumps over the quick dog. " + "The quick brown dog jumps over the lazy <field1>fox</field1>."));
            } else if ("2".equals(searchHit.getId())) {
                assertHighlight(searchHit, "field1", 0, 3, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog. Second sentence not finished"));
                assertHighlight(searchHit, "field1", 1, 3, equalTo("The lazy red <field1>fox</field1> jumps over the quick dog."));
                assertHighlight(searchHit, "field1", 2, 3, equalTo("The quick brown dog jumps over the lazy <field1>fox</field1>."));
            } else {
                fail("Only hits with id 1 and 2 are returned");
            }
        }
    }
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) SearchHit(org.elasticsearch.search.SearchHit) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 24 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method testSynonyms.

public void testSynonyms() throws IOException {
    Builder builder = Settings.builder().put(indexSettings()).put("index.analysis.analyzer.synonym.tokenizer", "whitespace").putArray("index.analysis.analyzer.synonym.filter", "synonym", "lowercase").put("index.analysis.filter.synonym.type", "synonym").putArray("index.analysis.filter.synonym.synonyms", "fast,quick");
    assertAcked(prepareCreate("test").setSettings(builder.build()).addMapping("type1", "field1", "type=text,term_vector=with_positions_offsets,search_analyzer=synonym," + "analyzer=english,index_options=offsets"));
    ensureGreen();
    client().prepareIndex("test", "type1", "0").setSource("field1", "The quick brown fox jumps over the lazy dog").get();
    refresh();
    for (String highlighterType : ALL_TYPES) {
        logger.info("--> highlighting (type=" + highlighterType + ") and searching on field1");
        SearchSourceBuilder source = searchSource().query(matchQuery("field1", "quick brown fox").operator(Operator.AND)).highlighter(highlight().field("field1").order("score").preTags("<x>").postTags("</x>").highlighterType(highlighterType));
        SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet();
        assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <x>quick</x> <x>brown</x> <x>fox</x> jumps over the lazy dog"));
        source = searchSource().query(matchQuery("field1", "fast brown fox").operator(Operator.AND)).highlighter(highlight().field("field1").order("score").preTags("<x>").postTags("</x>"));
        searchResponse = client().search(searchRequest("test").source(source)).actionGet();
        assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <x>quick</x> <x>brown</x> <x>fox</x> jumps over the lazy dog"));
    }
}
Also used : IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) RandomScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 25 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method testHighlighterWithWordBoundaryScannerAndLocale.

public void testHighlighterWithWordBoundaryScannerAndLocale() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping()));
    ensureGreen();
    indexRandom(true, client().prepareIndex("test", "type1").setSource("field1", "some quick and hairy brown:fox jumped over the lazy dog"));
    for (String type : new String[] { "unified", "fvh" }) {
        SearchSourceBuilder source = searchSource().query(termQuery("field1", "some")).highlighter(highlight().field("field1", 23, 1).highlighterType(type).preTags("<xxx>").postTags("</xxx>").boundaryScannerType(BoundaryScannerType.WORD).boundaryScannerLocale(Locale.ENGLISH.toLanguageTag()));
        SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
        assertHighlight(searchResponse, 0, "field1", 0, 1, anyOf(equalTo("<xxx>some</xxx> quick and hairy brown"), equalTo("<xxx>some</xxx>")));
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)193 SearchResponse (org.elasticsearch.action.search.SearchResponse)101 SearchRequest (org.elasticsearch.action.search.SearchRequest)76 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)41 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)37 IOException (java.io.IOException)36 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)34 Search (io.searchbox.core.Search)32 Matchers.containsString (org.hamcrest.Matchers.containsString)32 Test (org.junit.Test)29 List (java.util.List)26 Map (java.util.Map)25 SearchSourceBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder)24 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)24 ArrayList (java.util.ArrayList)20 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)20 Set (java.util.Set)19 Collectors (java.util.stream.Collectors)18 SearchHit (org.elasticsearch.search.SearchHit)18 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)16