Search in sources :

Example 31 with SearchSourceBuilder

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

the class HighlighterSearchIT method testPostingsHighlighterMultiTermQueryMultipleLevels.

public void testPostingsHighlighterMultiTermQueryMultipleLevels() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
    ensureGreen();
    client().prepareIndex("test", "type1").setSource("field1", "The photography word will get highlighted").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        logger.info("--> highlighting and searching on field1");
        SearchSourceBuilder source = searchSource().query(boolQuery().should(boolQuery().mustNot(QueryBuilders.existsQuery("field1"))).should(matchQuery("field1", "test")).should(constantScoreQuery(queryStringQuery("field1:photo*")))).highlighter(highlight().field("field1").highlighterType(type));
        SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
        assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted"));
    }
}
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 32 with SearchSourceBuilder

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

the class HighlighterSearchIT method testPostingsHighlighterQueryString.

public void testPostingsHighlighterQueryString() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
    ensureGreen();
    client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        logger.info("--> highlighting and searching on field2");
        SearchSourceBuilder source = searchSource().query(queryStringQuery("qui*").defaultField("field2")).highlighter(highlight().field("field2").highlighterType(type));
        SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
        assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
    }
}
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 33 with SearchSourceBuilder

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

the class HighlighterSearchIT method testPostingsHighlighterRegexpQuery.

public void testPostingsHighlighterRegexpQuery() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
    ensureGreen();
    client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        logger.info("--> highlighting and searching on field2");
        SearchSourceBuilder source = searchSource().query(regexpQuery("field2", "qu[a-l]+k")).highlighter(highlight().field("field2").highlighterType(type));
        SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
        assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over the lazy dog!"));
    }
}
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 34 with SearchSourceBuilder

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

the class HighlighterSearchIT method testPostingsHighlighterCommonTermsQuery.

public void testPostingsHighlighterCommonTermsQuery() throws IOException {
    assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
    ensureGreen();
    client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog! Second sentence.").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        logger.info("--> highlighting and searching on field1");
        SearchSourceBuilder source = searchSource().query(commonTermsQuery("field2", "quick brown").cutoffFrequency(100)).highlighter(highlight().field("field2").preTags("<x>").postTags("</x>").highlighterType(type));
        SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet();
        assertHitCount(searchResponse, 1L);
        assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <x>quick</x> <x>brown</x> fox jumps over the lazy dog!"));
    }
}
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 35 with SearchSourceBuilder

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

the class HighlighterSearchIT method testPostingsHighlighterPrefixQueryWithinBooleanQuery.

public void testPostingsHighlighterPrefixQueryWithinBooleanQuery() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
    ensureGreen();
    client().prepareIndex("test", "type1").setSource("field1", "The photography word will get highlighted").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        logger.info("--> highlighting and searching on field1");
        SearchSourceBuilder source = searchSource().query(boolQuery().must(prefixQuery("field1", "photo")).should(matchQuery("field1", "test").minimumShouldMatch("0"))).highlighter(highlight().field("field1").highlighterType(type));
        SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
        assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <em>photography</em> word will get highlighted"));
    }
}
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)152 SearchResponse (org.elasticsearch.action.search.SearchResponse)77 SearchRequest (org.elasticsearch.action.search.SearchRequest)52 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)37 Matchers.containsString (org.hamcrest.Matchers.containsString)32 IOException (java.io.IOException)31 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)28 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)26 List (java.util.List)25 SearchSourceBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder)24 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)24 Search (io.searchbox.core.Search)23 Map (java.util.Map)23 Set (java.util.Set)19 ArrayList (java.util.ArrayList)18 Collectors (java.util.stream.Collectors)18 Test (org.junit.Test)18 Collections (java.util.Collections)15 SearchHit (org.elasticsearch.search.SearchHit)15 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)15