org.graylog2.search.SearchQuery Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.search;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.mongojack.DBQuery;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class SearchQuery {
private final Multimap queryMap;
private final Set disallowedKeys;
private final String queryString;
public SearchQuery(String queryString) {
this(queryString, HashMultimap.create(), Collections.emptySet());
}
public SearchQuery(String queryString, Multimap queryMap, Set disallowedKeys) {
this.queryString = queryString;
this.queryMap = queryMap;
this.disallowedKeys = disallowedKeys;
}
public Multimap getQueryMap() {
return queryMap;
}
public DBQuery.Query toDBQuery() {
if (queryMap.isEmpty()) {
return DBQuery.empty();
}
final List dbQueries = new ArrayList<>();
for (Map.Entry> entry : queryMap.asMap().entrySet()) {
final List queries = new ArrayList<>();
final List include = selectValues(entry.getValue(), value -> !value.isNegate());
final List exclude = selectValues(entry.getValue(), SearchQueryParser.FieldValue::isNegate);
if (!include.isEmpty()) {
queries.add(DBQuery.or(toQuery(entry.getKey(), include)));
}
if (!exclude.isEmpty()) {
queries.add(DBQuery.nor(toQuery(entry.getKey(), exclude)));
}
dbQueries.add(DBQuery.and(queries.toArray(new DBQuery.Query[0])));
}
return DBQuery.and(dbQueries.toArray(new DBQuery.Query[0]));
}
private DBQuery.Query[] toQuery(String field, List values) {
return values.stream()
.map(value -> value.getOperator().buildQuery(field, value.getValue()))
.collect(Collectors.toList())
.toArray(new DBQuery.Query[0]);
}
public Bson toBson() {
if (queryMap.isEmpty()) {
return new Document();
}
final List dbQueries = toBsonFilterList();
return Filters.and(dbQueries);
}
public List toBsonFilterList() {
if (queryMap.isEmpty()) {
return List.of();
}
final List dbQueries = new ArrayList<>();
for (Map.Entry> entry : queryMap.asMap().entrySet()) {
final List queries = new ArrayList<>();
final List include = selectValues(entry.getValue(), value -> !value.isNegate());
final List exclude = selectValues(entry.getValue(), SearchQueryParser.FieldValue::isNegate);
if (!include.isEmpty()) {
queries.add(Filters.or(toBson(entry.getKey(), include)));
}
if (!exclude.isEmpty()) {
queries.add(Filters.nor(toBson(entry.getKey(), exclude)));
}
dbQueries.add(Filters.and(queries));
}
return dbQueries;
}
private List toBson(String field, List values) {
return values.stream()
.map(value -> value.getOperator().buildBson(field, value.getValue()))
.toList();
}
private List selectValues(Collection values,
Function callback) {
return values.stream()
.filter(callback::apply)
.toList();
}
public String getQueryString() {
return queryString;
}
public Set getDisallowedKeys() {
return disallowedKeys;
}
public boolean hasDisallowedKeys() {
return !disallowedKeys.isEmpty();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy