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));
}
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()));
}
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>"));
}
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"));
}
}
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>"));
}
Aggregations