use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilders.boolQuery in project graylog2-server by Graylog2.
the class SearchesAdapterES6 method buildSearchRequest.
private SearchSourceBuilder buildSearchRequest(ScrollCommand scrollCommand) {
final String query = normalizeQuery(scrollCommand.query());
final QueryBuilder queryBuilder = isWildcardQuery(query) ? matchAllQuery() : queryStringQuery(query).allowLeadingWildcard(configuration.isAllowLeadingWildcardSearches());
final Optional<BoolQueryBuilder> rangeQueryBuilder = scrollCommand.range().map(range -> QueryBuilders.boolQuery().must(TimeRangeQueryFactory.create(range)));
final Optional<BoolQueryBuilder> filterQueryBuilder = scrollCommand.filter().filter(filter -> !isWildcardQuery(filter)).map(QueryBuilders::queryStringQuery).map(filter -> rangeQueryBuilder.orElse(QueryBuilders.boolQuery()).must(filter));
final BoolQueryBuilder filteredQueryBuilder = QueryBuilders.boolQuery().must(queryBuilder);
filterQueryBuilder.ifPresent(filteredQueryBuilder::filter);
applyStreamsFilter(filteredQueryBuilder, scrollCommand);
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(filteredQueryBuilder);
applyPaginationIfPresent(searchSourceBuilder, scrollCommand);
applySortingIfPresent(searchSourceBuilder, scrollCommand);
applyHighlighting(searchSourceBuilder, scrollCommand);
return searchSourceBuilder;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilders.boolQuery in project graylog2-server by Graylog2.
the class SearchesAdapterES6 method standardSearchRequest.
private SearchSourceBuilder standardSearchRequest(String query, int limit, int offset, TimeRange range, String filter, Sorting sort, boolean highlight) {
if (query == null || query.trim().isEmpty()) {
query = "*";
}
final QueryBuilder queryBuilder;
if ("*".equals(query.trim())) {
queryBuilder = matchAllQuery();
} else {
queryBuilder = queryStringQuery(query).allowLeadingWildcard(configuration.isAllowLeadingWildcardSearches());
}
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.boolQuery().must(queryBuilder).filter(standardFilters(range, filter)));
if (offset >= 0) {
searchSourceBuilder.from(offset);
}
if (limit > 0) {
searchSourceBuilder.size(limit);
}
if (sort != null) {
searchSourceBuilder.sort(sort.getField(), sortOrderMapper.fromSorting(sort));
}
if (highlight && configuration.isAllowHighlighting()) {
final HighlightBuilder highlightBuilder = new HighlightBuilder().requireFieldMatch(false).field("*").fragmentSize(0).numOfFragments(0);
searchSourceBuilder.highlighter(highlightBuilder);
}
return searchSourceBuilder;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilders.boolQuery in project graylog2-server by Graylog2.
the class ElasticsearchBackend method generateFilterClause.
// TODO make pluggable
public Optional<QueryBuilder> generateFilterClause(Filter filter, SearchJob job, Query query) {
if (filter == null) {
return Optional.empty();
}
switch(filter.type()) {
case AndFilter.NAME:
final BoolQueryBuilder andBuilder = QueryBuilders.boolQuery();
filter.filters().stream().map(filter1 -> generateFilterClause(filter1, job, query)).forEach(optQueryBuilder -> optQueryBuilder.ifPresent(andBuilder::must));
return Optional.of(andBuilder);
case OrFilter.NAME:
final BoolQueryBuilder orBuilder = QueryBuilders.boolQuery();
// TODO for the common case "any of these streams" we can optimize the filter into
// a single "termsQuery" instead of "termQuery OR termQuery" if all direct children are "StreamFilter"
filter.filters().stream().map(filter1 -> generateFilterClause(filter1, job, query)).forEach(optQueryBuilder -> optQueryBuilder.ifPresent(orBuilder::should));
return Optional.of(orBuilder);
case StreamFilter.NAME:
// Skipping stream filter, will be extracted elsewhere
return Optional.empty();
case QueryStringFilter.NAME:
return Optional.of(QueryBuilders.queryStringQuery(this.queryStringDecorators.decorate(((QueryStringFilter) filter).query(), job, query)));
}
return Optional.empty();
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilders.boolQuery in project graylog2-server by Graylog2.
the class ElasticsearchBackend method generate.
@Override
public ESGeneratedQueryContext generate(SearchJob job, Query query, SearchConfig searchConfig) {
final BackendQuery backendQuery = query.query();
validateQueryTimeRange(query, searchConfig);
final Set<SearchType> searchTypes = query.searchTypes();
final String queryString = this.queryStringDecorators.decorate(backendQuery.queryString(), job, query);
final QueryBuilder normalizedRootQuery = normalizeQueryString(queryString);
final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().filter(normalizedRootQuery);
// add the optional root query filters
generateFilterClause(query.filter(), job, query).map(boolQuery::filter);
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(boolQuery).from(0).size(0);
final ESGeneratedQueryContext queryContext = queryContextFactory.create(this, searchSourceBuilder, job, query);
for (SearchType searchType : searchTypes) {
final Optional<SearchTypeError> searchTypeError = validateSearchType(query, searchType, searchConfig);
if (searchTypeError.isPresent()) {
LOG.error("Invalid search type {} for elasticsearch backend, cannot generate query part. Skipping this search type.", searchType.type());
queryContext.addError(searchTypeError.get());
continue;
}
final SearchSourceBuilder searchTypeSourceBuilder = queryContext.searchSourceBuilder(searchType);
final Set<String> effectiveStreamIds = searchType.effectiveStreams().isEmpty() ? query.usedStreamIds() : searchType.effectiveStreams();
final BoolQueryBuilder searchTypeOverrides = QueryBuilders.boolQuery().must(searchTypeSourceBuilder.query()).must(Objects.requireNonNull(TimeRangeQueryFactory.create(query.effectiveTimeRange(searchType)), "Timerange for search type " + searchType.id() + " cannot be found in query or search type.")).must(QueryBuilders.termsQuery(Message.FIELD_STREAMS, effectiveStreamIds));
searchType.query().ifPresent(searchTypeBackendQuery -> {
final String searchTypeQueryString = this.queryStringDecorators.decorate(searchTypeBackendQuery.queryString(), job, query);
final QueryBuilder normalizedSearchTypeQuery = normalizeQueryString(searchTypeQueryString);
searchTypeOverrides.must(normalizedSearchTypeQuery);
});
searchTypeSourceBuilder.query(searchTypeOverrides);
final String type = searchType.type();
final Provider<ESSearchTypeHandler<? extends SearchType>> searchTypeHandler = elasticsearchSearchTypeHandlers.get(type);
if (searchTypeHandler == null) {
LOG.error("Unknown search type {} for elasticsearch backend, cannot generate query part. Skipping this search type.", type);
queryContext.addError(new SearchTypeError(query, searchType.id(), "Unknown search type '" + type + "' for elasticsearch backend, cannot generate query"));
continue;
}
searchTypeHandler.get().generateQueryPart(job, query, searchType, queryContext);
}
return queryContext;
}
Aggregations