public String createQuery( boolean includeFacets, int simpleSelections, int rangeSelections, int pathSelections) { SenseiClientRequest clientRequest = senseiQueryProducer.createQuery( includeFacets, simpleSelections, rangeSelections, pathSelections); FilterBuilder[] filters = new FilterBuilder[clientRequest.getSelections().size()]; for (int i = 0; i < filters.length; i++) { Selection selection = clientRequest.getSelections().get(i); if (selection instanceof Terms) { filters[i] = createFilterFromTerm((Terms) selection); } if (selection instanceof Range) { Range range = (Range) selection; RangeFilterBuilder rangeFilter = FilterBuilders.rangeFilter(range.getField()); filters[i] = rangeFilter; if (!"*".equals(range.getFrom())) rangeFilter.from(range.getFrom()); if (!"*".equals(range.getTo())) rangeFilter.to(range.getTo()); } if (selection instanceof Path) { Path path = (Path) selection; filters[i] = FilterBuilders.prefixFilter(path.getField(), path.getValue()); } } FilterBuilder selections = FilterBuilders.orFilter(filters); SearchRequestBuilder requestBuilder = new SearchRequestBuilder(new MockClient()); requestBuilder.setFilter(selections); try { JSONObject request = new JSONObject(requestBuilder.toString()); JSONObject facets = new JSONObject(); for (Selection selection : clientRequest.getSelections()) { if (selection instanceof Terms) { String field = selection.getField(); facets.put( field, new JSONObject("{\"terms\": {\"field\":\"" + field + "\", \"size\" : 300}}")); } } request.put("facets", facets); return request.toString(); } catch (Exception ex) { throw new RuntimeException(ex); } }
// Make sure that unordered, reversed, disjoint and/or overlapping ranges are supported // Duel with filters public void testRandomRanges() throws Exception { final int numDocs = scaledRandomIntBetween(500, 5000); final double[][] docs = new double[numDocs][]; for (int i = 0; i < numDocs; ++i) { final int numValues = randomInt(5); docs[i] = new double[numValues]; for (int j = 0; j < numValues; ++j) { docs[i][j] = randomDouble() * 100; } } createIndex("idx"); for (int i = 0; i < docs.length; ++i) { XContentBuilder source = jsonBuilder().startObject().startArray("values"); for (int j = 0; j < docs[i].length; ++j) { source = source.value(docs[i][j]); } source = source.endArray().endObject(); client().prepareIndex("idx", "type").setSource(source).execute().actionGet(); } assertNoFailures( client() .admin() .indices() .prepareRefresh("idx") .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .execute() .get()); final int numRanges = randomIntBetween(1, 20); final double[][] ranges = new double[numRanges][]; for (int i = 0; i < ranges.length; ++i) { switch (randomInt(2)) { case 0: ranges[i] = new double[] {Double.NEGATIVE_INFINITY, randomInt(100)}; break; case 1: ranges[i] = new double[] {randomInt(100), Double.POSITIVE_INFINITY}; break; case 2: ranges[i] = new double[] {randomInt(100), randomInt(100)}; break; default: throw new AssertionError(); } } RangeBuilder query = range("range").field("values"); for (int i = 0; i < ranges.length; ++i) { String key = Integer.toString(i); if (ranges[i][0] == Double.NEGATIVE_INFINITY) { query.addUnboundedTo(key, ranges[i][1]); } else if (ranges[i][1] == Double.POSITIVE_INFINITY) { query.addUnboundedFrom(key, ranges[i][0]); } else { query.addRange(key, ranges[i][0], ranges[i][1]); } } SearchRequestBuilder reqBuilder = client().prepareSearch("idx").addAggregation(query); for (int i = 0; i < ranges.length; ++i) { RangeFilterBuilder filter = FilterBuilders.rangeFilter("values"); if (ranges[i][0] != Double.NEGATIVE_INFINITY) { filter = filter.from(ranges[i][0]); } if (ranges[i][1] != Double.POSITIVE_INFINITY) { filter = filter.to(ranges[i][1]); } reqBuilder = reqBuilder.addAggregation(filter("filter" + i).filter(filter)); } SearchResponse resp = reqBuilder.execute().actionGet(); Range range = resp.getAggregations().get("range"); for (int i = 0; i < ranges.length; ++i) { long count = 0; for (double[] values : docs) { for (double value : values) { if (value >= ranges[i][0] && value < ranges[i][1]) { ++count; break; } } } final Range.Bucket bucket = range.getBucketByKey(Integer.toString(i)); assertEquals(bucket.getKey(), count, bucket.getDocCount()); final Filter filter = resp.getAggregations().get("filter" + i); assertThat(filter.getDocCount(), equalTo(count)); } }