Search in sources :

Example 26 with SearchSourceBuilder

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

the class HighlighterSearchIT method testGeoFieldHighlightingWhenQueryGetsRewritten.

public void testGeoFieldHighlightingWhenQueryGetsRewritten() throws IOException {
    // same as above but in this example the query gets rewritten during highlighting
    // see https://github.com/elastic/elasticsearch/issues/17537#issuecomment-244939633
    XContentBuilder mappings = jsonBuilder();
    mappings.startObject();
    mappings.startObject("jobs").startObject("properties").startObject("loc").field("type", "geo_point").endObject().startObject("jd").field("type", "text").endObject().endObject().endObject();
    mappings.endObject();
    assertAcked(prepareCreate("test").addMapping("jobs", mappings));
    ensureYellow();
    client().prepareIndex("test", "jobs", "1").setSource(jsonBuilder().startObject().field("jd", "some आवश्यकता है- आर्य समाज अनाथालय, 68 सिविल लाइन्स, बरेली को एक पुरूष" + " रस text").field("loc", "12.934059,77.610741").endObject()).get();
    refresh();
    QueryBuilder query = QueryBuilders.functionScoreQuery(QueryBuilders.boolQuery().filter(QueryBuilders.geoBoundingBoxQuery("loc").setCorners(new GeoPoint(48.934059, 41.610741), new GeoPoint(-23.065941, 113.610741))));
    SearchResponse search = client().prepareSearch().setSource(new SearchSourceBuilder().query(query).highlighter(new HighlightBuilder().highlighterType("plain").field("jd"))).get();
    assertNoFailures(search);
    assertThat(search.getHits().getTotalHits(), equalTo(1L));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) 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 27 with SearchSourceBuilder

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

the class HighlighterSearchIT method testFVHManyMatches.

/**
     * The FHV can spend a long time highlighting degenerate documents if
     * phraseLimit is not set. Its default is now reasonably low.
     */
public void testFVHManyMatches() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping()));
    ensureGreen();
    // Index one megabyte of "t   " over and over and over again
    String pattern = "t   ";
    String value = new String(new char[1024 * 256 / pattern.length()]).replace("\0", pattern);
    client().prepareIndex("test", "type1").setSource("field1", value).get();
    refresh();
    logger.info("--> highlighting and searching on field1 with default phrase limit");
    SearchSourceBuilder source = searchSource().query(termQuery("field1", "t")).highlighter(highlight().highlighterType("fvh").field("field1", 20, 1).order("score").preTags("<xxx>").postTags("</xxx>"));
    SearchResponse defaultPhraseLimit = client().search(searchRequest("test").source(source)).actionGet();
    assertHighlight(defaultPhraseLimit, 0, "field1", 0, 1, containsString("<xxx>t</xxx>"));
    logger.info("--> highlighting and searching on field1 with large phrase limit");
    source = searchSource().query(termQuery("field1", "t")).highlighter(highlight().highlighterType("fvh").field("field1", 20, 1).order("score").preTags("<xxx>").postTags("</xxx>").phraseLimit(30000));
    SearchResponse largePhraseLimit = client().search(searchRequest("test").source(source)).actionGet();
    assertHighlight(largePhraseLimit, 0, "field1", 0, 1, containsString("<xxx>t</xxx>"));
    /*
         * I hate comparing times because it can be inconsistent but default is
         * in the neighborhood of 300ms and the large phrase limit is in the
         * neighborhood of 8 seconds.
         */
    assertThat(defaultPhraseLimit.getTookInMillis(), lessThan(largePhraseLimit.getTookInMillis()));
}
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 28 with SearchSourceBuilder

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

the class HighlighterSearchIT method testGlobalHighlightingSettingsOverriddenAtFieldLevel.

public void testGlobalHighlightingSettingsOverriddenAtFieldLevel() {
    createIndex("test");
    ensureGreen();
    client().prepareIndex("test", "type1").setSource("field1", new String[] { "this is a test", "this is the second test" }, "field2", new String[] { "this is another test", "yet another test" }).get();
    refresh();
    logger.info("--> highlighting and searching on field1 and field2 produces different tags");
    SearchSourceBuilder source = searchSource().query(termQuery("field1", "test")).highlighter(highlight().order("score").preTags("<global>").postTags("</global>").fragmentSize(1).numOfFragments(1).field(new HighlightBuilder.Field("field1").numOfFragments(2)).field(new HighlightBuilder.Field("field2").preTags("<field2>").postTags("</field2>").fragmentSize(50).requireFieldMatch(false)));
    SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
    assertHighlight(searchResponse, 0, "field1", 0, 2, equalTo(" <global>test</global>"));
    assertHighlight(searchResponse, 0, "field1", 1, 2, equalTo(" <global>test</global>"));
    assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("this is another <field2>test</field2>"));
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) 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 29 with SearchSourceBuilder

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

the class HighlighterSearchIT method testCommonTermsQuery.

public void testCommonTermsQuery() {
    createIndex("test");
    ensureGreen();
    client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog").get();
    refresh();
    logger.info("--> highlighting and searching on field1");
    for (String type : UNIFIED_AND_NULL) {
        SearchSourceBuilder source = searchSource().query(commonTermsQuery("field2", "quick brown").cutoffFrequency(100)).highlighter(highlight().field("field2").order("score").preTags("<x>").postTags("</x>").highlighterType(type));
        SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
        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 30 with SearchSourceBuilder

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

the class HighlighterSearchIT method testKeywordFieldHighlighting.

public void testKeywordFieldHighlighting() throws IOException {
    // check that keyword highlighting works
    XContentBuilder mappings = jsonBuilder();
    mappings.startObject();
    mappings.startObject("type").startObject("properties").startObject("keyword_field").field("type", "keyword").endObject().endObject().endObject();
    mappings.endObject();
    assertAcked(prepareCreate("test").addMapping("type", mappings));
    client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("keyword_field", "some text").endObject()).get();
    refresh();
    SearchResponse search = client().prepareSearch().setSource(new SearchSourceBuilder().query(QueryBuilders.matchQuery("keyword_field", "some text")).highlighter(new HighlightBuilder().field("*"))).get();
    assertNoFailures(search);
    assertThat(search.getHits().getTotalHits(), equalTo(1L));
    assertThat(search.getHits().getAt(0).getHighlightFields().get("keyword_field").getFragments()[0].string(), equalTo("<em>some text</em>"));
}
Also used : 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)

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