use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class FiltersAggregatorTests method testRandom.
public void testRandom() throws Exception {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
int numDocs = randomIntBetween(100, 200);
int maxTerm = randomIntBetween(10, 50);
int[] expectedBucketCount = new int[maxTerm];
Document document = new Document();
for (int i = 0; i < numDocs; i++) {
if (frequently()) {
// make sure we have more than one segment to test the merge
indexWriter.commit();
}
int value = randomInt(maxTerm - 1);
expectedBucketCount[value] += 1;
document.add(new Field("field", Integer.toString(value), fieldType));
indexWriter.addDocument(document);
document.clear();
}
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
int numFilters = randomIntBetween(1, 10);
QueryBuilder[] filters = new QueryBuilder[numFilters];
int[] filterTerms = new int[numFilters];
int expectedOtherCount = numDocs;
Set<Integer> filterSet = new HashSet<>();
for (int i = 0; i < filters.length; i++) {
int value = randomInt(maxTerm - 1);
filters[i] = QueryBuilders.termQuery("field", Integer.toString(value));
filterTerms[i] = value;
if (filterSet.contains(value) == false) {
expectedOtherCount -= expectedBucketCount[value];
filterSet.add(value);
}
}
FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", filters);
builder.otherBucket(true);
builder.otherBucketKey("other");
for (boolean doReduce : new boolean[] { true, false }) {
final InternalFilters response;
if (doReduce) {
response = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
} else {
response = search(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
}
List<InternalFilters.InternalBucket> buckets = response.getBuckets();
assertEquals(buckets.size(), filters.length + 1);
for (InternalFilters.InternalBucket bucket : buckets) {
if ("other".equals(bucket.getKey())) {
assertEquals(bucket.getDocCount(), expectedOtherCount);
} else {
int index = Integer.parseInt(bucket.getKey());
assertEquals(bucket.getDocCount(), (long) expectedBucketCount[filterTerms[index]]);
}
}
}
indexReader.close();
directory.close();
}
use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class FiltersIT method testEmptyFilterDeclarations.
// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
QueryBuilder emptyFilter = new BoolQueryBuilder();
SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("all", emptyFilter), new KeyedFilter("tag1", termQuery("tag", "tag1"))))).execute().actionGet();
assertSearchResponse(response);
Filters filters = response.getAggregations().get("tags");
assertThat(filters, notNullValue());
Filters.Bucket allBucket = filters.getBucketByKey("all");
assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
Filters.Bucket bucket = filters.getBucketByKey("tag1");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class HighlightBuilderTests method setRandomCommonOptions.
@SuppressWarnings({ "rawtypes" })
private static void setRandomCommonOptions(AbstractHighlighterBuilder highlightBuilder) {
if (randomBoolean()) {
// need to set this together, otherwise parsing will complain
highlightBuilder.preTags(randomStringArray(0, 3));
highlightBuilder.postTags(randomStringArray(0, 3));
}
if (randomBoolean()) {
highlightBuilder.fragmentSize(randomIntBetween(0, 100));
}
if (randomBoolean()) {
highlightBuilder.numOfFragments(randomIntBetween(0, 10));
}
if (randomBoolean()) {
highlightBuilder.highlighterType(randomAsciiOfLengthBetween(1, 10));
}
if (randomBoolean()) {
highlightBuilder.fragmenter(randomAsciiOfLengthBetween(1, 10));
}
if (randomBoolean()) {
QueryBuilder highlightQuery;
switch(randomInt(2)) {
case 0:
highlightQuery = new MatchAllQueryBuilder();
break;
case 1:
highlightQuery = new IdsQueryBuilder();
break;
default:
case 2:
highlightQuery = new TermQueryBuilder(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
break;
}
highlightQuery.boost((float) randomDoubleBetween(0, 10, false));
highlightBuilder.highlightQuery(highlightQuery);
}
if (randomBoolean()) {
if (randomBoolean()) {
highlightBuilder.order(randomFrom(Order.values()));
} else {
// also test the string setter
highlightBuilder.order(randomFrom(Order.values()).toString());
}
}
if (randomBoolean()) {
highlightBuilder.highlightFilter(randomBoolean());
}
if (randomBoolean()) {
highlightBuilder.forceSource(randomBoolean());
}
if (randomBoolean()) {
if (randomBoolean()) {
highlightBuilder.boundaryScannerType(randomFrom(BoundaryScannerType.values()));
} else {
// also test the string setter
highlightBuilder.boundaryScannerType(randomFrom(BoundaryScannerType.values()).toString());
}
}
if (randomBoolean()) {
highlightBuilder.boundaryMaxScan(randomIntBetween(0, 10));
}
if (randomBoolean()) {
highlightBuilder.boundaryChars(randomAsciiOfLengthBetween(1, 10).toCharArray());
}
if (randomBoolean()) {
highlightBuilder.boundaryScannerLocale(randomLocale(random()).toLanguageTag());
}
if (randomBoolean()) {
highlightBuilder.noMatchSize(randomIntBetween(0, 10));
}
if (randomBoolean()) {
highlightBuilder.phraseLimit(randomIntBetween(0, 10));
}
if (randomBoolean()) {
int items = randomIntBetween(0, 5);
Map<String, Object> options = new HashMap<>(items);
for (int i = 0; i < items; i++) {
Object value = null;
switch(randomInt(2)) {
case 0:
value = randomAsciiOfLengthBetween(1, 10);
break;
case 1:
value = new Integer(randomInt(1000));
break;
case 2:
value = new Boolean(randomBoolean());
break;
}
options.put(randomAsciiOfLengthBetween(1, 10), value);
}
}
if (randomBoolean()) {
highlightBuilder.requireFieldMatch(randomBoolean());
}
}
use of 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.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));
}
Aggregations