Search in sources :

Example 6 with LindenBooleanFilterBuilder

use of com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitSearch_expr.

@Override
public void exitSearch_expr(BQLParser.Search_exprContext ctx) {
    if (inQueryWhere) {
        List<LindenQuery> lindenQueries = new ArrayList<>();
        for (BQLParser.Term_exprContext f : ctx.term_expr()) {
            LindenQuery query = queryProperty.get(f);
            if (query == null) {
                continue;
            }
            lindenQueries.add(query);
        }
        if (lindenQueries.size() == 0) {
            return;
        }
        if (lindenQueries.size() == 1) {
            queryProperty.put(ctx, lindenQueries.get(0));
            return;
        }
        boolean disableCoord = ctx.disable_coord == null ? false : (boolean) valProperty.get(ctx.disable_coord);
        LindenBooleanQueryBuilder booleanQueryBuilder = new LindenBooleanQueryBuilder();
        booleanQueryBuilder.setDisableCoord(disableCoord);
        if (ctx.boost_by != null && ctx.boost_by.numeric_value().PLACEHOLDER() == null) {
            booleanQueryBuilder.setBoost(Double.valueOf(ctx.boost_by.numeric_value().getText()));
        }
        for (int i = 0; i < lindenQueries.size(); ++i) {
            // term_op indicates first clause is must
            if (ctx.term_op != null && i == 0) {
                booleanQueryBuilder.addQuery(lindenQueries.get(i), LindenBooleanClause.MUST);
            } else {
                booleanQueryBuilder.addQuery(lindenQueries.get(i), LindenBooleanClause.SHOULD);
            }
        }
        LindenQuery lindenQuery = booleanQueryBuilder.build();
        queryProperty.put(ctx, lindenQuery);
    } else {
        List<LindenFilter> lindenFilters = new ArrayList<>();
        for (BQLParser.Term_exprContext f : ctx.term_expr()) {
            LindenFilter filter = filterProperty.get(f);
            if (filter == null) {
                continue;
            }
            lindenFilters.add(filter);
        }
        if (lindenFilters.size() == 0) {
            return;
        }
        if (lindenFilters.size() == 1) {
            filterProperty.put(ctx, lindenFilters.get(0));
            return;
        }
        LindenBooleanFilterBuilder booleanFilterBuilder = new LindenBooleanFilterBuilder();
        for (int i = 0; i < lindenFilters.size(); ++i) {
            // term_op indicates first clause is must
            if (ctx.term_op != null && i == 0) {
                booleanFilterBuilder.addFilter(lindenFilters.get(i), LindenBooleanClause.MUST);
            } else {
                booleanFilterBuilder.addFilter(lindenFilters.get(i), LindenBooleanClause.SHOULD);
            }
        }
        LindenFilter lindenFilter = booleanFilterBuilder.build();
        filterProperty.put(ctx, lindenFilter);
    }
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) ArrayList(java.util.ArrayList)

Example 7 with LindenBooleanFilterBuilder

use of com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitTerm_expr.

@Override
public void exitTerm_expr(BQLParser.Term_exprContext ctx) {
    if (inQueryWhere) {
        List<LindenQuery> lindenQueries = new ArrayList<>();
        for (BQLParser.Factor_exprContext f : ctx.factor_expr()) {
            LindenQuery query = queryProperty.get(f);
            if (query == null) {
                continue;
            }
            lindenQueries.add(query);
        }
        if (lindenQueries.size() == 0) {
            return;
        }
        if (lindenQueries.size() == 1) {
            queryProperty.put(ctx, lindenQueries.get(0));
            return;
        }
        boolean disableCoord = ctx.disable_coord == null ? false : (boolean) valProperty.get(ctx.disable_coord);
        LindenBooleanQueryBuilder booleanQueryBuilder = new LindenBooleanQueryBuilder();
        booleanQueryBuilder.setDisableCoord(disableCoord);
        if (ctx.boost_by != null && ctx.boost_by.numeric_value().PLACEHOLDER() == null) {
            booleanQueryBuilder.setBoost(Double.valueOf(ctx.boost_by.numeric_value().getText()));
        }
        for (int i = 0; i < lindenQueries.size(); ++i) {
            booleanQueryBuilder.addQuery(lindenQueries.get(i), LindenBooleanClause.MUST);
        }
        LindenQuery lindenQuery = booleanQueryBuilder.build();
        queryProperty.put(ctx, lindenQuery);
    } else {
        List<LindenFilter> lindenFilters = new ArrayList<>();
        for (BQLParser.Factor_exprContext f : ctx.factor_expr()) {
            LindenFilter filter = filterProperty.get(f);
            if (filter == null) {
                continue;
            }
            lindenFilters.add(filter);
        }
        if (lindenFilters.size() == 0) {
            return;
        }
        if (lindenFilters.size() == 1) {
            filterProperty.put(ctx, lindenFilters.get(0));
            return;
        }
        LindenBooleanFilterBuilder booleanFilterBuilder = new LindenBooleanFilterBuilder();
        for (int i = 0; i < lindenFilters.size(); ++i) {
            booleanFilterBuilder.addFilter(lindenFilters.get(i), LindenBooleanClause.MUST);
        }
        LindenFilter lindenFilter = booleanFilterBuilder.build();
        filterProperty.put(ctx, lindenFilter);
    }
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) ArrayList(java.util.ArrayList)

Aggregations

LindenBooleanFilterBuilder (com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder)7 LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)6 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)6 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)6 LindenType (com.xiaomi.linden.thrift.common.LindenType)3 AbstractMap (java.util.AbstractMap)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)3 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)2 Test (org.junit.Test)2 LindenBooleanClause (com.xiaomi.linden.thrift.common.LindenBooleanClause)1 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)1 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)1 LindenFacet (com.xiaomi.linden.thrift.common.LindenFacet)1 LindenFacetDimAndPath (com.xiaomi.linden.thrift.common.LindenFacetDimAndPath)1 LindenFacetParam (com.xiaomi.linden.thrift.common.LindenFacetParam)1 LindenTerm (com.xiaomi.linden.thrift.common.LindenTerm)1 LindenWildcardQuery (com.xiaomi.linden.thrift.common.LindenWildcardQuery)1