Search in sources :

Example 26 with BoolQueryBuilder

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

the class BsWebConfigToLabelCQ method bool.

public void bool(BoolCall<WebConfigToLabelCQ> boolLambda, ConditionOptionCall<BoolQueryBuilder> opLambda) {
    WebConfigToLabelCQ mustQuery = new WebConfigToLabelCQ();
    WebConfigToLabelCQ shouldQuery = new WebConfigToLabelCQ();
    WebConfigToLabelCQ mustNotQuery = new WebConfigToLabelCQ();
    WebConfigToLabelCQ filterQuery = new WebConfigToLabelCQ();
    boolLambda.callback(mustQuery, shouldQuery, mustNotQuery, filterQuery);
    if (mustQuery.hasQueries() || shouldQuery.hasQueries() || mustNotQuery.hasQueries() || filterQuery.hasQueries()) {
        BoolQueryBuilder builder = regBoolCQ(mustQuery.getQueryBuilderList(), shouldQuery.getQueryBuilderList(), mustNotQuery.getQueryBuilderList(), filterQuery.getQueryBuilderList());
        if (opLambda != null) {
            opLambda.callback(builder);
        }
    }
}
Also used : WebConfigToLabelCQ(org.codelibs.fess.es.config.cbean.cq.WebConfigToLabelCQ) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 27 with BoolQueryBuilder

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

the class BsWebConfigToRoleCQ method bool.

public void bool(BoolCall<WebConfigToRoleCQ> boolLambda, ConditionOptionCall<BoolQueryBuilder> opLambda) {
    WebConfigToRoleCQ mustQuery = new WebConfigToRoleCQ();
    WebConfigToRoleCQ shouldQuery = new WebConfigToRoleCQ();
    WebConfigToRoleCQ mustNotQuery = new WebConfigToRoleCQ();
    WebConfigToRoleCQ filterQuery = new WebConfigToRoleCQ();
    boolLambda.callback(mustQuery, shouldQuery, mustNotQuery, filterQuery);
    if (mustQuery.hasQueries() || shouldQuery.hasQueries() || mustNotQuery.hasQueries() || filterQuery.hasQueries()) {
        BoolQueryBuilder builder = regBoolCQ(mustQuery.getQueryBuilderList(), shouldQuery.getQueryBuilderList(), mustNotQuery.getQueryBuilderList(), filterQuery.getQueryBuilderList());
        if (opLambda != null) {
            opLambda.callback(builder);
        }
    }
}
Also used : WebConfigToRoleCQ(org.codelibs.fess.es.config.cbean.cq.WebConfigToRoleCQ) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 28 with BoolQueryBuilder

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

the class BsSearchFieldLogCQ method bool.

public void bool(BoolCall<SearchFieldLogCQ> boolLambda, ConditionOptionCall<BoolQueryBuilder> opLambda) {
    SearchFieldLogCQ mustQuery = new SearchFieldLogCQ();
    SearchFieldLogCQ shouldQuery = new SearchFieldLogCQ();
    SearchFieldLogCQ mustNotQuery = new SearchFieldLogCQ();
    SearchFieldLogCQ filterQuery = new SearchFieldLogCQ();
    boolLambda.callback(mustQuery, shouldQuery, mustNotQuery, filterQuery);
    if (mustQuery.hasQueries() || shouldQuery.hasQueries() || mustNotQuery.hasQueries() || filterQuery.hasQueries()) {
        BoolQueryBuilder builder = regBoolCQ(mustQuery.getQueryBuilderList(), shouldQuery.getQueryBuilderList(), mustNotQuery.getQueryBuilderList(), filterQuery.getQueryBuilderList());
        if (opLambda != null) {
            opLambda.callback(builder);
        }
    }
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchFieldLogCQ(org.codelibs.fess.es.log.cbean.cq.SearchFieldLogCQ)

Example 29 with BoolQueryBuilder

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

the class QueryHelper method buildRoleQuery.

protected void buildRoleQuery(final QueryContext queryContext, final SearchRequestType searchRequestType) {
    if (roleQueryHelper != null && queryContext.roleQueryEnabled()) {
        final Set<String> roleSet = roleQueryHelper.build(searchRequestType);
        if (!roleSet.isEmpty()) {
            queryContext.addQuery(boolQuery -> {
                final BoolQueryBuilder roleQuery = QueryBuilders.boolQuery();
                roleSet.stream().forEach(name -> {
                    roleQuery.should(QueryBuilders.termQuery(fessConfig.getIndexFieldRole(), name));
                });
                boolQuery.filter(roleQuery);
            });
        }
    }
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 30 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project chili-core by codingchili.

the class ElasticMap method query.

@Override
public QueryBuilder<Value> query(String field) {
    return new AbstractQueryBuilder<Value>(this, field) {

        List<BoolQueryBuilder> statements = new ArrayList<>();

        BoolQueryBuilder builder = new BoolQueryBuilder();

        @Override
        public QueryBuilder<Value> and(String attribute) {
            setAttribute(attribute);
            return this;
        }

        @Override
        public QueryBuilder<Value> or(String attribute) {
            setAttribute(attribute);
            statements.add(builder);
            builder = new BoolQueryBuilder();
            return this;
        }

        @Override
        public QueryBuilder<Value> between(Long minimum, Long maximum) {
            builder.must(QueryBuilders.rangeQuery(attribute()).gte(minimum).lte(maximum));
            return this;
        }

        @Override
        public QueryBuilder<Value> like(String text) {
            text = Validator.toPlainText(text).toLowerCase();
            builder.must(QueryBuilders.wildcardQuery(attribute(), "*" + text + "*"));
            return this;
        }

        @Override
        public QueryBuilder<Value> startsWith(String text) {
            builder.must(QueryBuilders.matchPhrasePrefixQuery(attribute(), text));
            return this;
        }

        @Override
        public QueryBuilder<Value> in(Comparable... list) {
            BoolQueryBuilder bool = new BoolQueryBuilder().minimumShouldMatch(1);
            for (Comparable item : list) {
                bool.should(QueryBuilders.matchPhraseQuery(attribute(), item));
            }
            builder.must(bool);
            return this;
        }

        @Override
        public QueryBuilder<Value> equalTo(Comparable match) {
            builder.must(QueryBuilders.matchPhraseQuery(attribute(), match));
            return this;
        }

        @Override
        public QueryBuilder<Value> matches(String regex) {
            if (regex.contains("^") || regex.contains("$")) {
                // remove unsupported characters in ElasticSearch query.
                regex = regex.replaceAll("[\\^$]", "");
            }
            builder.must(QueryBuilders.regexpQuery(attribute(), regex.toLowerCase()).flags(RegexpFlag.ALL));
            return this;
        }

        @Override
        public void execute(Handler<AsyncResult<Collection<Value>>> handler) {
            if (!builder.equals(new BoolQueryBuilder())) {
                statements.add(builder);
            }
            BoolQueryBuilder query = new BoolQueryBuilder().minimumShouldMatch(1);
            for (BoolQueryBuilder statement : statements) {
                query.should(statement);
            }
            getRequestWithOptions().setQuery(query).execute(new ElasticHandler<>(response -> {
                handler.handle(result(listFrom(response.getHits().getHits())));
            }, exception -> handler.handle(error(exception))));
        }

        private SearchRequestBuilder getRequestWithOptions() {
            SearchRequestBuilder request = client.prepareSearch(context.database()).setTypes(context.collection());
            if (isOrdered) {
                switch(sortOrder) {
                    case ASCENDING:
                        request.addSort(getOrderByAttribute(), SortOrder.ASC);
                        break;
                    case DESCENDING:
                        request.addSort(getOrderByAttribute(), SortOrder.DESC);
                }
            }
            request.setSize(pageSize);
            request.setFrom(pageSize * page);
            return request;
        }
    };
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) TransportClient(org.elasticsearch.client.transport.TransportClient) RegexpFlag(org.elasticsearch.index.query.RegexpFlag) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ArrayList(java.util.ArrayList) Logger(com.codingchili.core.logging.Logger) InetAddress(java.net.InetAddress) StorageContext(com.codingchili.core.context.StorageContext) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) FutureHelper.result(com.codingchili.core.context.FutureHelper.result) AsyncResult(io.vertx.core.AsyncResult) Validator(com.codingchili.core.security.Validator) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) SearchHit(org.elasticsearch.search.SearchHit) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) Collection(java.util.Collection) com.codingchili.core.storage.exception(com.codingchili.core.storage.exception) IOException(java.io.IOException) FutureHelper.error(com.codingchili.core.context.FutureHelper.error) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) BytesReference(org.elasticsearch.common.bytes.BytesReference) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) Future(io.vertx.core.Future) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) TransportAddress(org.elasticsearch.common.transport.TransportAddress) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) RestStatus(org.elasticsearch.rest.RestStatus) SortOrder(org.elasticsearch.search.sort.SortOrder) Handler(io.vertx.core.Handler) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ActionListener(org.elasticsearch.action.ActionListener) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Handler(io.vertx.core.Handler) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)201 SearchResponse (org.elasticsearch.action.search.SearchResponse)73 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)58 SearchHit (org.elasticsearch.search.SearchHit)41 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)38 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)30 Map (java.util.Map)29 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 List (java.util.List)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)21 Test (org.junit.Test)18 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)16 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)16 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)14 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)13 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 LinkedList (java.util.LinkedList)11 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)11