use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder 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));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder 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.index.query.QueryBuilder in project elasticsearch by elastic.
the class InnerHitsIT method testDontExplode.
public void testDontExplode() throws Exception {
assertAcked(prepareCreate("index1").addMapping("child", "_parent", "type=parent"));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("index1", "parent", "1").setSource("{}", XContentType.JSON));
requests.add(client().prepareIndex("index1", "child", "1").setParent("1").setSource("field", "value1"));
indexRandom(true, requests);
QueryBuilder query = hasChildQuery("child", matchQuery("field", "value1"), ScoreMode.None).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false);
SearchResponse response = client().prepareSearch("index1").setQuery(query).addSort("_uid", SortOrder.ASC).get();
assertNoFailures(response);
assertHitCount(response, 1);
assertAcked(prepareCreate("index2").addMapping("type", "nested", "type=nested"));
client().prepareIndex("index2", "type", "1").setSource(jsonBuilder().startObject().startArray("nested").startObject().field("field", "value1").endObject().endArray().endObject()).setRefreshPolicy(IMMEDIATE).get();
query = nestedQuery("nested", matchQuery("nested.field", "value1"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false);
response = client().prepareSearch("index2").setQuery(query).addSort("_uid", SortOrder.ASC).get();
assertNoFailures(response);
assertHitCount(response, 1);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class HighlighterSearchIT method checkMatchedFieldsCase.
private void checkMatchedFieldsCase(boolean requireFieldMatch) throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("foo").field("type", "text").field("term_vector", "with_positions_offsets").field("store", true).field("analyzer", "english").startObject("fields").startObject("plain").field("type", "text").field("term_vector", "with_positions_offsets").field("analyzer", "standard").endObject().endObject().endObject().startObject("bar").field("type", "text").field("term_vector", "with_positions_offsets").field("store", true).field("analyzer", "english").startObject("fields").startObject("plain").field("type", "text").field("term_vector", "with_positions_offsets").field("analyzer", "standard").endObject().endObject().endObject().endObject().endObject().endObject()));
ensureGreen();
index("test", "type1", "1", "foo", "running with scissors");
index("test", "type1", "2", "foo", "cat cat junk junk junk junk junk junk junk cats junk junk", "bar", "cat cat junk junk junk junk junk junk junk cats junk junk");
index("test", "type1", "3", "foo", "weird", "bar", "result");
refresh();
Field fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
SearchRequestBuilder req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
// First check highlighting without any matched fields set
SearchResponse resp = req.setQuery(queryStringQuery("running scissors").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
// And that matching a subfield doesn't automatically highlight it
resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("running with <em>scissors</em>"));
// Add the subfield to the list of matched fields but don't match it. Everything should still work
// like before we added it.
fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
fooField.matchedFields("foo", "foo.plain");
req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
resp = req.setQuery(queryStringQuery("running scissors").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
// Now make half the matches come from the stored field and half from just a matched field.
resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
// Now remove the stored field from the matched field list. That should work too.
fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
fooField.matchedFields("foo.plain");
req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with scissors"));
// Now make sure boosted fields don't blow up when matched fields is both the subfield and stored field.
fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
fooField.matchedFields("foo", "foo.plain");
req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
resp = req.setQuery(queryStringQuery("foo.plain:running^5 scissors").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
// Now just all matches are against the matched field. This still returns highlighting.
resp = req.setQuery(queryStringQuery("foo.plain:running foo.plain:scissors").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
// And all matched field via the queryString's field parameter, just in case
resp = req.setQuery(queryStringQuery("running scissors").field("foo.plain")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
// Finding the same string two ways is ok too
resp = req.setQuery(queryStringQuery("run foo.plain:running^5 scissors").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
// But we use the best found score when sorting fragments
resp = req.setQuery(queryStringQuery("cats foo.plain:cats^5").field("foo")).get();
assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
// which can also be written by searching on the subfield
resp = req.setQuery(queryStringQuery("cats").field("foo").field("foo.plain", 5)).get();
assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
// Speaking of two fields, you can have two fields, only one of which has matchedFields enabled
QueryBuilder twoFieldsQuery = queryStringQuery("cats").field("foo").field("foo.plain", 5).field("bar").field("bar.plain", 5);
Field barField = new Field("bar").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
assertHighlight(resp, 0, "bar", 0, equalTo("<em>cat</em> <em>cat</em> junk junk junk junk"));
// And you can enable matchedField highlighting on both
barField.matchedFields("bar", "bar.plain");
resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
assertHighlight(resp, 0, "bar", 0, equalTo("junk junk <em>cats</em> junk junk"));
// Setting a matchedField that isn't searched/doesn't exist is simply ignored.
barField.matchedFields("bar", "candy");
resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
assertHighlight(resp, 0, "bar", 0, equalTo("<em>cat</em> <em>cat</em> junk junk junk junk"));
// If the stored field doesn't have a value it doesn't matter what you match, you get nothing.
barField.matchedFields("bar", "foo.plain");
resp = req.setQuery(queryStringQuery("running scissors").field("foo.plain").field("bar")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("bar")));
// If the stored field is found but the matched field isn't then you don't get a result either.
fooField.matchedFields("bar.plain");
resp = req.setQuery(queryStringQuery("running scissors").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("foo")));
// But if you add the stored field to the list of matched fields then you'll get a result again
fooField.matchedFields("foo", "bar.plain");
resp = req.setQuery(queryStringQuery("running scissors").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("bar")));
// You _can_ highlight fields that aren't subfields of one another.
resp = req.setQuery(queryStringQuery("weird").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
assertHighlight(resp, 0, "foo", 0, equalTo("<em>weird</em>"));
assertHighlight(resp, 0, "bar", 0, equalTo("<em>resul</em>t"));
assertFailures(req.setQuery(queryStringQuery("result").field("foo").field("foo.plain").field("bar").field("bar.plain")), RestStatus.INTERNAL_SERVER_ERROR, containsString("IndexOutOfBoundsException"));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class QueryProfilerIT method testNoProfile.
/**
* This test makes sure no profile results are returned when profiling is disabled
*/
public void testNoProfile() throws Exception {
createIndex("test");
ensureGreen();
int numDocs = randomIntBetween(100, 150);
IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
for (int i = 0; i < numDocs; i++) {
docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "field2", i);
}
indexRandom(true, docs);
refresh();
QueryBuilder q = QueryBuilders.rangeQuery("field2").from(0).to(5);
logger.info("Query: {}", q);
SearchResponse resp = client().prepareSearch().setQuery(q).setProfile(false).execute().actionGet();
assertThat("Profile response element should be an empty map", resp.getProfileResults().size(), equalTo(0));
}
Aggregations