use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class UserIndex method search.
public SearchResult<UserDoc> search(@Nullable String searchText, SearchOptions options) {
SearchRequestBuilder request = esClient.prepareSearch(UserIndexDefinition.INDEX_TYPE_USER).setSize(options.getLimit()).setFrom(options.getOffset()).addSort(FIELD_NAME, SortOrder.ASC);
BoolQueryBuilder filter = boolQuery().must(termQuery(FIELD_ACTIVE, true));
QueryBuilder query;
if (StringUtils.isEmpty(searchText)) {
query = matchAllQuery();
} else {
query = QueryBuilders.multiMatchQuery(searchText, FIELD_LOGIN, USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_LOGIN), FIELD_NAME, USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_NAME), FIELD_EMAIL, USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_EMAIL)).operator(MatchQueryBuilder.Operator.AND);
}
request.setQuery(boolQuery().must(query).filter(filter));
return new SearchResult<>(request.get(), DOC_CONVERTER);
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project sonarqube by SonarSource.
the class UserIndex method selectUsersForBatch.
public Iterator<UserDoc> selectUsersForBatch(List<String> logins) {
BoolQueryBuilder filter = boolQuery().filter(termsQuery(FIELD_LOGIN, logins));
SearchRequestBuilder requestBuilder = esClient.prepareSearch(UserIndexDefinition.INDEX_TYPE_USER).setSearchType(SearchType.SCAN).addSort(SortBuilders.fieldSort(FIELD_LOGIN).order(SortOrder.ASC)).setScroll(TimeValue.timeValueMinutes(EsUtils.SCROLL_TIME_IN_MINUTES)).setSize(10_000).setFetchSource(new String[] { FIELD_LOGIN, FIELD_NAME }, null).setQuery(filter);
SearchResponse response = requestBuilder.get();
return EsUtils.scroll(esClient, response.getScrollId(), DOC_CONVERTER);
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.
the class AdjacencyMatrixIT method testWithSubAggregation.
public void testWithSubAggregation() throws Exception {
BoolQueryBuilder boolQ = new BoolQueryBuilder();
boolQ.must(termQuery("tag", "tag1"));
boolQ.must(termQuery("tag", "tag2"));
SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", newMap("tag1", termQuery("tag", "tag1")).add("tag2", termQuery("tag", "tag2")).add("both", boolQ)).subAggregation(avg("avg_value").field("value"))).execute().actionGet();
assertSearchResponse(response);
AdjacencyMatrix matrix = response.getAggregations().get("tags");
assertThat(matrix, notNullValue());
assertThat(matrix.getName(), equalTo("tags"));
int expectedBuckets = 0;
if (numTag1Docs > 0) {
expectedBuckets++;
}
if (numTag2Docs > 0) {
expectedBuckets++;
}
if (numMultiTagDocs > 0) {
// both, both&tag1, both&tag2, tag1&tag2
expectedBuckets += 4;
}
assertThat(matrix.getBuckets().size(), equalTo(expectedBuckets));
assertThat(matrix.getProperty("_bucket_count"), equalTo(expectedBuckets));
Object[] propertiesKeys = (Object[]) matrix.getProperty("_key");
Object[] propertiesDocCounts = (Object[]) matrix.getProperty("_count");
Object[] propertiesCounts = (Object[]) matrix.getProperty("avg_value.value");
assertEquals(expectedBuckets, propertiesKeys.length);
assertEquals(propertiesKeys.length, propertiesDocCounts.length);
assertEquals(propertiesKeys.length, propertiesCounts.length);
for (int i = 0; i < propertiesCounts.length; i++) {
AdjacencyMatrix.Bucket bucket = matrix.getBucketByKey(propertiesKeys[i].toString());
assertThat(bucket, Matchers.notNullValue());
Avg avgValue = bucket.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat((long) propertiesDocCounts[i], equalTo(bucket.getDocCount()));
assertThat((double) propertiesCounts[i], equalTo(avgValue.getValue()));
}
AdjacencyMatrix.Bucket tag1Bucket = matrix.getBucketByKey("tag1");
assertThat(tag1Bucket, Matchers.notNullValue());
assertThat(tag1Bucket.getDocCount(), equalTo((long) numTag1Docs));
long sum = 0;
for (int i = 0; i < numSingleTag1Docs; i++) {
sum += i + 1;
}
for (int i = numSingleTag1Docs + numSingleTag2Docs; i < numDocs; i++) {
sum += i + 1;
}
assertThat(tag1Bucket.getAggregations().asList().isEmpty(), is(false));
Avg avgBucket1Value = tag1Bucket.getAggregations().get("avg_value");
assertThat(avgBucket1Value, notNullValue());
assertThat(avgBucket1Value.getName(), equalTo("avg_value"));
assertThat(avgBucket1Value.getValue(), equalTo((double) sum / numTag1Docs));
Bucket tag2Bucket = matrix.getBucketByKey("tag2");
assertThat(tag2Bucket, Matchers.notNullValue());
assertThat(tag2Bucket.getDocCount(), equalTo((long) numTag2Docs));
sum = 0;
for (int i = numSingleTag1Docs; i < numDocs; i++) {
sum += i + 1;
}
assertThat(tag2Bucket.getAggregations().asList().isEmpty(), is(false));
Avg avgBucket2Value = tag2Bucket.getAggregations().get("avg_value");
assertThat(avgBucket2Value, notNullValue());
assertThat(avgBucket2Value.getName(), equalTo("avg_value"));
assertThat(avgBucket2Value.getValue(), equalTo((double) sum / numTag2Docs));
// Check intersection buckets are computed correctly by comparing with
// ANDed query bucket results
Bucket bucketBothQ = matrix.getBucketByKey("both");
if (numMultiTagDocs == 0) {
// Empty intersections are not returned.
assertThat(bucketBothQ, Matchers.nullValue());
Bucket bucketIntersectQ = matrix.getBucketByKey("tag1&tag2");
assertThat(bucketIntersectQ, Matchers.nullValue());
Bucket tag1Both = matrix.getBucketByKey("both&tag1");
assertThat(tag1Both, Matchers.nullValue());
} else {
assertThat(bucketBothQ, Matchers.notNullValue());
assertThat(bucketBothQ.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueBothQ = bucketBothQ.getAggregations().get("avg_value");
Bucket bucketIntersectQ = matrix.getBucketByKey("tag1&tag2");
assertThat(bucketIntersectQ, Matchers.notNullValue());
assertThat(bucketIntersectQ.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueIntersectQ = bucketBothQ.getAggregations().get("avg_value");
assertThat(avgValueIntersectQ.getValue(), equalTo(avgValueBothQ.getValue()));
Bucket tag1Both = matrix.getBucketByKey("both&tag1");
assertThat(tag1Both, Matchers.notNullValue());
assertThat(tag1Both.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueTag1BothIntersectQ = tag1Both.getAggregations().get("avg_value");
assertThat(avgValueTag1BothIntersectQ.getValue(), equalTo(avgValueBothQ.getValue()));
}
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.
the class AdjacencyMatrixIT 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(adjacencyMatrix("tags", newMap("all", emptyFilter).add("tag1", termQuery("tag", "tag1")))).execute().actionGet();
assertSearchResponse(response);
AdjacencyMatrix filters = response.getAggregations().get("tags");
assertThat(filters, notNullValue());
AdjacencyMatrix.Bucket allBucket = filters.getBucketByKey("all");
assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
AdjacencyMatrix.Bucket bucket = filters.getBucketByKey("tag1");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder 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));
}
Aggregations