Search in sources :

Example 11 with LindenQuery

use of com.xiaomi.linden.thrift.common.LindenQuery in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitPredicate.

@Override
public void exitPredicate(BQLParser.PredicateContext ctx) {
    if (inQueryWhere) {
        LindenQuery query = queryProperty.get(ctx.getChild(0));
        if (query == null) {
            return;
        }
        if (ctx.boost_by != null && ctx.boost_by.numeric_value().PLACEHOLDER() == null) {
            query.setBoost(Double.valueOf(ctx.boost_by.numeric_value().getText()));
        }
        queryProperty.put(ctx, query);
    } else {
        filterProperty.put(ctx, filterProperty.get(ctx.getChild(0)));
    }
}
Also used : LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery)

Example 12 with LindenQuery

use of com.xiaomi.linden.thrift.common.LindenQuery in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitNull_predicate.

@Override
public void exitNull_predicate(BQLParser.Null_predicateContext ctx) {
    String col = unescapeColumnName(ctx.column_name());
    Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
    col = fieldNameAndType.getKey();
    LindenFilter filter;
    if (ctx.NOT() != null) {
        filter = LindenNotNullFieldFilterBuilder.buildNotNullFieldFilterBuilder(col, false);
    } else {
        filter = LindenNotNullFieldFilterBuilder.buildNotNullFieldFilterBuilder(col, true);
    }
    if (inQueryWhere) {
        LindenQuery query = LindenQueryBuilder.buildFilteredQuery(LindenQueryBuilder.buildMatchAllQuery(), filter);
        queryProperty.put(ctx, query);
    } else {
        filterProperty.put(ctx, filter);
    }
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenType(com.xiaomi.linden.thrift.common.LindenType) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 13 with LindenQuery

use of com.xiaomi.linden.thrift.common.LindenQuery in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitEqual_predicate.

@Override
public void exitEqual_predicate(BQLParser.Equal_predicateContext ctx) {
    if (ctx.value().PLACEHOLDER() != null) {
        return;
    }
    String value = valProperty.get(ctx.value()).toString();
    String col = unescapeColumnName(ctx.column_name());
    Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
    LindenType type = fieldNameAndType.getValue();
    col = fieldNameAndType.getKey();
    if (!validateValueString(type, value)) {
        throw new ParseCancellationException("Filed value: " + value + " doesn't corresponds to field type: " + type);
    }
    if (inQueryWhere) {
        LindenQuery query;
        switch(type) {
            case STRING:
            case FACET:
                query = LindenQueryBuilder.buildTermQuery(col, value);
                break;
            case INTEGER:
            case LONG:
            case DOUBLE:
            case FLOAT:
                query = LindenRangeQueryBuilder.buildRangeQuery(col, type, value, value, true, true);
                break;
            default:
                throw new ParseCancellationException("EQUAL predicate doesn't support this type " + type);
        }
        queryProperty.put(ctx, query);
    } else {
        LindenFilter filter;
        switch(type) {
            case STRING:
            case FACET:
                filter = LindenTermFilterBuilder.buildTermFilter(col, value);
                break;
            case INTEGER:
            case LONG:
            case DOUBLE:
            case FLOAT:
                filter = LindenRangeFilterBuilder.buildRangeFilter(col, type, value, value, true, true);
                break;
            default:
                throw new ParseCancellationException("EQUAL predicate doesn't support this type " + type);
        }
        filterProperty.put(ctx, filter);
    }
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LindenType(com.xiaomi.linden.thrift.common.LindenType) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 14 with LindenQuery

use of com.xiaomi.linden.thrift.common.LindenQuery in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitSelect_stmt.

@Override
public void exitSelect_stmt(BQLParser.Select_stmtContext ctx) {
    if (ctx.order_by_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.order_by_clause(1), "ORDER BY clause can only appear once."));
    }
    if (ctx.limit_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.limit_clause(1), "LIMIT clause can only appear once."));
    }
    if (ctx.group_by_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.group_by_clause(1), "GROUP BY clause can only appear once."));
    }
    if (ctx.browse_by_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.browse_by_clause(1), "BROWSE BY clause can only appear once."));
    }
    if (ctx.drill_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.drill_clause(1), "DRILL clause can only appear once."));
    }
    if (ctx.source_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.source_clause(1), "SOURCE clause can only appear once."));
    }
    if (ctx.route_by_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.route_by_clause(1), "ROUTE BY clause can only appear once."));
    }
    if (ctx.score_model_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.score_model_clause(1), "USING SCORE MODEL clause can only appear once."));
    }
    LindenSearchRequest lindenRequest = new LindenSearchRequest();
    if (ctx.cols != null) {
        lindenRequest.setSourceFields((List<String>) valProperty.get(ctx.cols));
    }
    if (ctx.tables != null) {
        lindenRequest.setIndexNames((List<String>) valProperty.get(ctx.tables));
    }
    if (ctx.group_by != null) {
        GroupParam groupParam = (GroupParam) valProperty.get(ctx.group_by);
        if (groupParam != null) {
            lindenRequest.setGroupParam(groupParam);
        }
    }
    if (ctx.limit != null) {
        lindenRequest.setOffset(offsetProperty.get(ctx.limit));
        lindenRequest.setLength(countProperty.get(ctx.limit));
    }
    if (ctx.source != null && (Boolean) valProperty.get(ctx.source)) {
        lindenRequest.setSource(true);
    }
    if (ctx.explain != null && (Boolean) valProperty.get(ctx.explain)) {
        lindenRequest.setExplain(true);
    }
    LindenQuery query = null;
    if (ctx.q != null) {
        query = queryProperty.get(ctx.q);
    }
    if (query == null) {
        query = LindenQueryBuilder.buildMatchAllQuery();
    }
    lindenRequest.setQuery(query);
    if (ctx.w != null) {
        LindenFilter filter = filterProperty.get(ctx.w);
        lindenRequest.setFilter(filter);
    }
    if (ctx.scoring_model != null) {
        LindenScoreModel scoreModel = (LindenScoreModel) valProperty.get(ctx.scoring_model);
        lindenRequest.getQuery().setScoreModel(scoreModel);
    }
    if (ctx.boost_by != null && ctx.boost_by.numeric_value().PLACEHOLDER() == null) {
        lindenRequest.getQuery().setBoost(Double.valueOf(ctx.boost_by.numeric_value().getText()));
    }
    if (spatialParam != null) {
        lindenRequest.setSpatialParam(spatialParam);
    }
    if (ctx.order_by != null) {
        lindenRequest.setSort((LindenSort) valProperty.get(ctx.order_by));
    }
    if (ctx.snippet != null) {
        lindenRequest.setSnippetParam((SnippetParam) valProperty.get(ctx.snippet));
    }
    if (ctx.in_top != null) {
        lindenRequest.setEarlyParam((EarlyParam) valProperty.get(ctx.in_top));
    }
    if (ctx.route_param != null) {
        lindenRequest.setRouteParam((SearchRouteParam) valProperty.get(ctx.route_param));
    }
    if (facetRequest.isSetFacetParams() || facetRequest.isSetDrillDownDimAndPaths() || facetRequest.isSetAggregations()) {
        lindenRequest.setFacet(facetRequest);
    }
    lindenSearchRequestProperty.put(ctx, lindenRequest);
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) GroupParam(com.xiaomi.linden.thrift.common.GroupParam) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) LindenScoreModel(com.xiaomi.linden.thrift.common.LindenScoreModel)

Example 15 with LindenQuery

use of com.xiaomi.linden.thrift.common.LindenQuery in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitLike_predicate.

@Override
public void exitLike_predicate(BQLParser.Like_predicateContext ctx) {
    if (ctx.PLACEHOLDER() != null) {
        return;
    }
    String col = unescapeColumnName(ctx.column_name());
    Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
    LindenType type = fieldNameAndType.getValue();
    if (type != LindenType.STRING && type != LindenType.FACET) {
        throw new ParseCancellationException(new SemanticException(ctx.column_name(), "Non-string type column \"" + col + "\" can not be used in LIKE predicates."));
    }
    col = fieldNameAndType.getKey();
    String likeString = unescapeStringLiteral(ctx.STRING_LITERAL());
    LindenWildcardQuery wildcardQuery = new LindenWildcardQuery().setField(col).setQuery(likeString);
    if (inQueryWhere) {
        if (ctx.NOT() != null) {
            LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
            builder.addQuery(LindenRangeQueryBuilder.buildMatchAllQuery(), LindenBooleanClause.MUST);
            builder.addQuery(new LindenQuery().setWildcardQuery(wildcardQuery), LindenBooleanClause.MUST_NOT);
            queryProperty.put(ctx, builder.build());
        } else {
            queryProperty.put(ctx, new LindenQuery().setWildcardQuery(wildcardQuery));
        }
    } else {
        LindenFilter filter = new LindenFilter().setQueryFilter(new LindenQueryFilter().setQuery(new LindenQuery().setWildcardQuery(wildcardQuery)));
        if (ctx.NOT() != null) {
            LindenBooleanFilter booleanFilter = new LindenBooleanFilter();
            booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter).setClause(LindenBooleanClause.MUST_NOT));
            filter = new LindenFilter().setBooleanFilter(booleanFilter);
        }
        filterProperty.put(ctx, filter);
    }
}
Also used : LindenQueryFilter(com.xiaomi.linden.thrift.common.LindenQueryFilter) LindenWildcardQuery(com.xiaomi.linden.thrift.common.LindenWildcardQuery) LindenBooleanFilter(com.xiaomi.linden.thrift.common.LindenBooleanFilter) LindenType(com.xiaomi.linden.thrift.common.LindenType) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LindenBooleanSubFilter(com.xiaomi.linden.thrift.common.LindenBooleanSubFilter) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Aggregations

LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)29 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)17 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)11 Test (org.junit.Test)10 LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)9 LindenType (com.xiaomi.linden.thrift.common.LindenType)7 AbstractMap (java.util.AbstractMap)7 Map (java.util.Map)7 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)7 LindenBooleanFilterBuilder (com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder)6 HashMap (java.util.HashMap)6 Query (org.apache.lucene.search.Query)5 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)4 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)4 LindenFlexibleQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenFlexibleQueryBuilder)3 LindenResult (com.xiaomi.linden.thrift.common.LindenResult)3 LindenWildcardQuery (com.xiaomi.linden.thrift.common.LindenWildcardQuery)3 ArrayList (java.util.ArrayList)3 LindenQueryStringQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenQueryStringQueryBuilder)2 GroupParam (com.xiaomi.linden.thrift.common.GroupParam)2