Search in sources :

Example 21 with BoolQueryBuilder

use of 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);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 22 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project zipkin by openzipkin.

the class ElasticsearchSpanStore method getTraces.

@Override
public ListenableFuture<List<List<Span>>> getTraces(final QueryRequest request) {
    long endMillis = request.endTs;
    long beginMillis = endMillis - request.lookback;
    BoolQueryBuilder filter = boolQuery().must(rangeQuery("timestamp_millis").gte(beginMillis).lte(endMillis));
    if (request.serviceName != null) {
        filter.must(boolQuery().should(nestedQuery("annotations", termQuery("annotations.endpoint.serviceName", request.serviceName))).should(nestedQuery("binaryAnnotations", termQuery("binaryAnnotations.endpoint.serviceName", request.serviceName))));
    }
    if (request.spanName != null) {
        filter.must(termQuery("name", request.spanName));
    }
    for (String annotation : request.annotations) {
        BoolQueryBuilder annotationQuery = boolQuery().must(termQuery("annotations.value", annotation));
        if (request.serviceName != null) {
            annotationQuery.must(termQuery("annotations.endpoint.serviceName", request.serviceName));
        }
        filter.must(nestedQuery("annotations", annotationQuery));
    }
    for (Map.Entry<String, String> kv : request.binaryAnnotations.entrySet()) {
        // In our index template, we make sure the binaryAnnotation value is indexed as string,
        // meaning non-string values won't even be indexed at all. This means that we can only
        // match string values here, which happens to be exactly what we want.
        BoolQueryBuilder binaryAnnotationQuery = boolQuery().must(termQuery("binaryAnnotations.key", kv.getKey())).must(termQuery("binaryAnnotations.value", kv.getValue()));
        if (request.serviceName != null) {
            binaryAnnotationQuery.must(termQuery("binaryAnnotations.endpoint.serviceName", request.serviceName));
        }
        filter.must(nestedQuery("binaryAnnotations", binaryAnnotationQuery));
    }
    if (request.minDuration != null) {
        RangeQueryBuilder durationQuery = rangeQuery("duration").gte(request.minDuration);
        if (request.maxDuration != null) {
            durationQuery.lte(request.maxDuration);
        }
        filter.must(durationQuery);
    }
    Set<String> strings = indexNameFormatter.indexNamePatternsForRange(beginMillis, endMillis);
    final String[] indices = strings.toArray(new String[0]);
    // We need to filter to traces that contain at least one span that matches the request,
    // but the zipkin API is supposed to order traces by first span, regardless of if it was
    // filtered or not. This is not possible without either multiple, heavyweight queries
    // or complex multiple indexing, defeating much of the elegance of using elasticsearch for this.
    // So we fudge and order on the first span among the filtered spans - in practice, there should
    // be no significant difference in user experience since span start times are usually very
    // close to each other in human time.
    ListenableFuture<List<String>> traceIds = client.collectBucketKeys(indices, boolQuery().must(matchAllQuery()).filter(filter), AggregationBuilders.terms("traceId_agg").field("traceId").subAggregation(AggregationBuilders.min("timestamps_agg").field("timestamp_millis")).order(Order.aggregation("timestamps_agg", false)).size(request.limit));
    return transform(traceIds, new AsyncFunction<List<String>, List<List<Span>>>() {

        @Override
        public ListenableFuture<List<List<Span>>> apply(List<String> input) {
            return getTracesByIds(input, indices, request);
        }
    });
}
Also used : RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Span(zipkin.Span) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) Map(java.util.Map)

Example 23 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project graylog2-server by Graylog2.

the class Searches method standardFilters.

@Nullable
private QueryBuilder standardFilters(TimeRange range, String filter) {
    BoolQueryBuilder bfb = null;
    if (range != null) {
        bfb = QueryBuilders.boolQuery();
        bfb.must(IndexHelper.getTimestampRangeFilter(range));
    }
    // Not creating a filter for a "*" value because an empty filter used to be submitted that way.
    if (!isNullOrEmpty(filter) && !"*".equals(filter)) {
        if (bfb == null) {
            bfb = QueryBuilders.boolQuery();
        }
        bfb.must(queryStringQuery(filter));
    }
    return bfb;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Nullable(javax.annotation.Nullable)

Example 24 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project gerrit by GerritCodeReview.

the class ElasticQueryBuilder method not.

private <T> QueryBuilder not(Predicate<T> p) throws QueryParseException {
    Predicate<T> n = p.getChild(0);
    if (n instanceof TimestampRangePredicate) {
        return notTimestamp((TimestampRangePredicate<T>) n);
    }
    // Lucene does not support negation, start with all and subtract.
    BoolQueryBuilder q = QueryBuilders.boolQuery();
    q.must(QueryBuilders.matchAllQuery());
    q.mustNot(toQueryBuilder(n));
    return q;
}
Also used : TimestampRangePredicate(com.google.gerrit.server.index.TimestampRangePredicate) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 25 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project fess by codelibs.

the class SearchService method getDocumentListByDocIds.

public List<Map<String, Object>> getDocumentListByDocIds(final String[] docIds, final String[] fields, final OptionalThing<FessUserBean> userBean, final SearchRequestType searchRequestType) {
    return fessEsClient.getDocumentList(fessConfig.getIndexDocumentSearchIndex(), fessConfig.getIndexDocumentType(), builder -> {
        final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(fessConfig.getIndexFieldDocId(), docIds));
        if (searchRequestType != SearchRequestType.ADMIN_SEARCH) {
            final Set<String> roleSet = ComponentUtil.getRoleQueryHelper().build(searchRequestType);
            if (!roleSet.isEmpty()) {
                final BoolQueryBuilder roleQuery = QueryBuilders.boolQuery();
                roleSet.stream().forEach(name -> {
                    roleQuery.should(QueryBuilders.termQuery(fessConfig.getIndexFieldRole(), name));
                });
                boolQuery.filter(roleQuery);
            }
        }
        builder.setQuery(boolQuery);
        builder.setSize(fessConfig.getPagingSearchPageMaxSizeAsInteger().intValue());
        builder.setFetchSource(fields, null);
        fessConfig.processSearchPreference(builder, userBean);
        return true;
    });
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)98 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)25 SearchResponse (org.elasticsearch.action.search.SearchResponse)15 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)9 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)9 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)8 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)5 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)5 Map (java.util.Map)4 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)4 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)4 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 StickyFacetBuilder (org.sonar.server.es.StickyFacetBuilder)4 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)3 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)3