Java Code Examples for org.elasticsearch.index.query.BoolQueryBuilder#hasClauses()

The following examples show how to use org.elasticsearch.index.query.BoolQueryBuilder#hasClauses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryHelper.java    From fess with Apache License 2.0 7 votes vote down vote up
protected QueryBuilder convertBooleanQuery(final QueryContext context, final BooleanQuery booleanQuery, final float boost) {
    final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    for (final BooleanClause clause : booleanQuery.clauses()) {
        final QueryBuilder queryBuilder = convertQuery(context, clause.getQuery(), boost);
        if (queryBuilder != null) {
            switch (clause.getOccur()) {
            case MUST:
                boolQuery.must(queryBuilder);
                break;
            case SHOULD:
                boolQuery.should(queryBuilder);
                break;
            case MUST_NOT:
                boolQuery.mustNot(queryBuilder);
                break;
            default:
                break;
            }
        }
    }
    if (boolQuery.hasClauses()) {
        return boolQuery;
    }
    return null;
}
 
Example 2
Source File: ElasticsearchQueryBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
public static QueryBuilder buildSearchQuery(ConnectorSession session, TupleDomain<ElasticsearchColumnHandle> constraint, Optional<String> query)
{
    BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
    if (constraint.getDomains().isPresent()) {
        for (Map.Entry<ElasticsearchColumnHandle, Domain> entry : constraint.getDomains().get().entrySet()) {
            ElasticsearchColumnHandle column = entry.getKey();
            Domain domain = entry.getValue();

            checkArgument(!domain.isNone(), "Unexpected NONE domain for %s", column.getName());
            if (!domain.isAll()) {
                queryBuilder.filter(new BoolQueryBuilder().must(buildPredicate(session, column.getName(), domain, column.getType())));
            }
        }
    }
    query.map(QueryStringQueryBuilder::new)
            .ifPresent(queryBuilder::must);

    if (queryBuilder.hasClauses()) {
        return queryBuilder;
    }
    return new MatchAllQueryBuilder();
}
 
Example 3
Source File: EsQueryBuilder.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 构建查询
 * 
 * @return
 */
public QueryBuilder makeQueryBuilder(SearchRequestBuilder srb) {

    // 拼接查询层次
    BoolQueryBuilder boolQ = QueryBuilders.boolQuery();
    Map<SearchCondition, ConditionType> searchTypeMap = this.request.getSearchConditions();
    ConditionType conditionType;
    BoolQueryBuilder subBoolQ;
    for (SearchCondition searchCondition : searchTypeMap.keySet()) {
        conditionType = searchTypeMap.get(searchCondition);
        subBoolQ = queryBuilder(searchCondition, conditionType);
        mergeBuilder(boolQ, subBoolQ, conditionType);
    }
    // 没有条件直接返回
    if (!boolQ.hasClauses()) {
        return null;
    }
    // 得分脚本
    if (request.getSafeScoreScript().size() < 1) {
        srb.setQuery(boolQ);
        return boolQ;
    }
    String boost_mode = request.getSafeExtend().get(Constants.BOOST_MODE_KEY);
    if (StringUtils.isBlank(boost_mode)) {
        boost_mode = Constants.boost_mode;
    }
    FunctionScoreQueryBuilder functionScoreQ = QueryBuilders.functionScoreQuery(boolQ);
    functionScoreQ.boostMode(boost_mode);
    for (ScoreScript scoreScript : request.getSafeScoreScript()) {
        ScriptScoreFunctionBuilder builder = ScoreFunctionBuilders.scriptFunction(scoreScript
                .getScript());
        if (scoreScript.getScriptParams() != null && scoreScript.getScriptParams().size() > 0) {
            builder.params(scoreScript.getScriptParams());
        }
        functionScoreQ.add(builder);
    }
    srb.setQuery(functionScoreQ);
    return functionScoreQ;
}
 
Example 4
Source File: QueryContext.java    From fess with Apache License 2.0 5 votes vote down vote up
public void addQuery(final Consumer<BoolQueryBuilder> boolQuery) {
    BoolQueryBuilder builder;
    if (queryBuilder instanceof MatchAllQueryBuilder) {
        builder = QueryBuilders.boolQuery();
    } else {
        builder = QueryBuilders.boolQuery().must(queryBuilder);
    }
    boolQuery.accept(builder);
    if (builder.hasClauses()) {
        queryBuilder = builder;
    }
}