Search in sources :

Example 16 with LindenQuery

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

the class BQLCompilerAnalyzer method exitRange_predicate.

@Override
public void exitRange_predicate(BQLParser.Range_predicateContext ctx) {
    // ignore unassigned value
    if (ctx.val.PLACEHOLDER() != null) {
        return;
    }
    Object val = valProperty.get(ctx.val);
    String col = unescapeColumnName(ctx.column_name());
    Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
    LindenType type = fieldNameAndType.getValue();
    col = fieldNameAndType.getKey();
    if (!checkValueType(type, val)) {
        throw new ParseCancellationException("Value: " + val + " in RANGE predicate doesn't correspond to field type: " + type);
    }
    String strVal1;
    String strVal2;
    boolean isStartClosed = false;
    boolean isEndClosed = false;
    if (ctx.op.getText().charAt(0) == '>') {
        strVal1 = val.toString();
        strVal2 = null;
        if (">=".equals(ctx.op.getText())) {
            isStartClosed = true;
        }
    } else {
        strVal1 = null;
        strVal2 = val.toString();
        if ("<=".equals(ctx.op.getText())) {
            isEndClosed = true;
        }
    }
    if (inQueryWhere) {
        LindenQuery query = LindenRangeQueryBuilder.buildRangeQuery(col, type, strVal1, strVal2, isStartClosed, isEndClosed);
        queryProperty.put(ctx, query);
    } else {
        LindenFilter filter = LindenRangeFilterBuilder.buildRangeFilter(col, type, strVal1, strVal2, isStartClosed, isEndClosed);
        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 17 with LindenQuery

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

the class BQLCompilerAnalyzer method exitFlexible_query_predicate.

@Override
public void exitFlexible_query_predicate(BQLParser.Flexible_query_predicateContext ctx) {
    String orig = unescapeStringLiteral(ctx.STRING_LITERAL());
    LindenScoreModel lindenScoreModel = new LindenScoreModel().setName(ctx.IDENT().getText()).setFunc((String) valProperty.get(ctx.score_model())).setParams((List<LindenInputParam>) valProperty.get(ctx.formal_parameters()));
    if (ctx.OVERRIDE() != null) {
        lindenScoreModel.setOverride(true);
    }
    if (ctx.PLUGIN() != null) {
        lindenScoreModel.setPlugin(true);
    }
    LindenFlexibleQuery lindenFlexibleQuery = new LindenFlexibleQuery().setQuery(orig).setFields((List<LindenSearchField>) valProperty.get(ctx.flexible_fields()));
    lindenFlexibleQuery.setModel(lindenScoreModel);
    if (ctx.fm != null) {
        lindenFlexibleQuery.setFullMatch(true);
    } else if (ctx.mrt != null && ctx.mrt.PLACEHOLDER() == null) {
        double ratio = Double.valueOf(ctx.mrt.getText());
        lindenFlexibleQuery.setMatchRatio(ratio);
    }
    LindenQuery lindenQuery = new LindenQuery();
    if (inQueryWhere) {
        if (ctx.gi != null) {
            lindenFlexibleQuery.setGlobalIDF(true);
            if (ctx.gfd != null) {
                lindenFlexibleQuery.setGlobalFields((List<LindenSearchField>) valProperty.get(ctx.global_fields()));
            }
        }
        queryProperty.put(ctx, lindenQuery.setFlexQuery(lindenFlexibleQuery));
    } else {
        filterProperty.put(ctx, LindenQueryFilterBuilder.buildQueryFilter(lindenQuery.setFlexQuery(lindenFlexibleQuery)));
    }
}
Also used : LindenInputParam(com.xiaomi.linden.thrift.common.LindenInputParam) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenSearchField(com.xiaomi.linden.thrift.common.LindenSearchField) LindenFlexibleQuery(com.xiaomi.linden.thrift.common.LindenFlexibleQuery) LindenScoreModel(com.xiaomi.linden.thrift.common.LindenScoreModel)

Example 18 with LindenQuery

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

the class BQLCompilerAnalyzer method exitBetween_predicate.

@Override
public void exitBetween_predicate(BQLParser.Between_predicateContext ctx) {
    if (ctx.val1.PLACEHOLDER() != null || ctx.val2.PLACEHOLDER() != null) {
        return;
    }
    String val1 = valProperty.get(ctx.val1).toString();
    String val2 = valProperty.get(ctx.val2).toString();
    String col = unescapeColumnName(ctx.column_name());
    Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
    LindenType type = fieldNameAndType.getValue();
    col = fieldNameAndType.getKey();
    if (!validateValueString(type, val1)) {
        throw new ParseCancellationException("Filed value: " + val1 + " doesn't corresponds to field type: " + type);
    }
    if (!validateValueString(type, val2)) {
        throw new ParseCancellationException("Filed value: " + val2 + " doesn't corresponds to field type: " + type);
    }
    if (inQueryWhere) {
        LindenQuery query;
        if (ctx.not == null) {
            query = LindenRangeQueryBuilder.buildRangeQuery(col, type, val1, val2, true, true);
        } else {
            LindenQuery query1 = LindenRangeQueryBuilder.buildRangeQuery(col, type, null, val1, false, false);
            LindenQuery query2 = LindenRangeQueryBuilder.buildRangeQuery(col, type, val2, null, false, false);
            LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
            builder.addQuery(query1, LindenBooleanClause.SHOULD);
            builder.addQuery(query2, LindenBooleanClause.SHOULD);
            query = builder.build();
        }
        queryProperty.put(ctx, query);
    } else {
        LindenFilter filter;
        if (ctx.not == null) {
            filter = LindenRangeFilterBuilder.buildRangeFilter(col, type, val1, val2, true, true);
        } else {
            LindenFilter filter1 = LindenRangeFilterBuilder.buildRangeFilter(col, type, null, val1, false, false);
            LindenFilter filter2 = LindenRangeFilterBuilder.buildRangeFilter(col, type, val2, null, false, false);
            LindenBooleanFilterBuilder builder = new LindenBooleanFilterBuilder();
            builder.addFilter(filter1, LindenBooleanClause.SHOULD);
            builder.addFilter(filter2, LindenBooleanClause.SHOULD);
            filter = builder.build();
        }
        if (filter != null) {
            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) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) LindenType(com.xiaomi.linden.thrift.common.LindenType) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 19 with LindenQuery

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

the class BQLCompilerAnalyzer method exitIn_predicate.

@Override
public void exitIn_predicate(BQLParser.In_predicateContext ctx) {
    String col = unescapeColumnName(ctx.column_name());
    Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
    LindenType type = fieldNameAndType.getValue();
    col = fieldNameAndType.getKey();
    List<Object> values = (List<Object>) valProperty.get(ctx.value_list());
    List<Object> excludes = (List<Object>) valProperty.get(ctx.except_clause());
    List<Map.Entry<String, LindenBooleanClause>> subClauses = new ArrayList<>();
    LindenBooleanClause clause = LindenBooleanClause.SHOULD;
    if (ctx.not != null) {
        clause = LindenBooleanClause.MUST_NOT;
    }
    for (int i = 0; values != null && i < values.size(); ++i) {
        if (values.get(i) == null) {
            continue;
        }
        subClauses.add(new AbstractMap.SimpleEntry<>(values.get(i).toString(), clause));
    }
    clause = LindenBooleanClause.MUST_NOT;
    if (ctx.not != null) {
        clause = LindenBooleanClause.SHOULD;
    }
    for (int i = 0; excludes != null && i < excludes.size(); ++i) {
        if (excludes.get(i) == null) {
            continue;
        }
        subClauses.add(new AbstractMap.SimpleEntry<>(excludes.get(i).toString(), clause));
    }
    if (subClauses.size() == 0) {
        return;
    }
    if (inQueryWhere) {
        LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
        for (Map.Entry<String, LindenBooleanClause> clauseEntry : subClauses) {
            LindenQuery query;
            switch(type) {
                case STRING:
                case FACET:
                    query = LindenQueryBuilder.buildTermQuery(col, clauseEntry.getKey());
                    break;
                case LONG:
                case INTEGER:
                case FLOAT:
                case DOUBLE:
                    query = LindenRangeQueryBuilder.buildRangeQuery(col, type, clauseEntry.getKey(), clauseEntry.getKey(), true, true);
                    break;
                default:
                    throw new ParseCancellationException("IN predicate doesn't support this type " + type);
            }
            builder.addQuery(query, clauseEntry.getValue());
        }
        queryProperty.put(ctx, builder.build());
    } else {
        LindenBooleanFilterBuilder builder = new LindenBooleanFilterBuilder();
        for (Map.Entry<String, LindenBooleanClause> clauseEntry : subClauses) {
            LindenFilter filter;
            switch(type) {
                case STRING:
                case FACET:
                    filter = LindenTermFilterBuilder.buildTermFilter(col, clauseEntry.getKey());
                    break;
                case LONG:
                case INTEGER:
                case FLOAT:
                case DOUBLE:
                    filter = LindenRangeFilterBuilder.buildRangeFilter(col, type, clauseEntry.getKey(), clauseEntry.getKey(), true, true);
                    break;
                default:
                    throw new ParseCancellationException("IN predicate doesn't support this type " + type);
            }
            builder.addFilter(filter, clauseEntry.getValue());
        }
        filterProperty.put(ctx, builder.build());
    }
}
Also used : LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) ArrayList(java.util.ArrayList) LindenType(com.xiaomi.linden.thrift.common.LindenType) AbstractMap(java.util.AbstractMap) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) List(java.util.List) ArrayList(java.util.ArrayList) LindenBooleanClause(com.xiaomi.linden.thrift.common.LindenBooleanClause) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 20 with LindenQuery

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

the class BQLCompilerAnalyzer method exitQuery_predicate.

@Override
public void exitQuery_predicate(BQLParser.Query_predicateContext ctx) {
    String orig = unescapeStringLiteral(ctx.STRING_LITERAL());
    LindenQueryStringQueryBuilder builder = new LindenQueryStringQueryBuilder().setQuery(orig);
    if (ctx.disable_coord != null) {
        builder.setDisableCoord((Boolean) valProperty.get(ctx.disable_coord));
    }
    if (ctx.AND() != null) {
        builder.setOperator(Operator.AND);
    }
    LindenQuery stringQuery = builder.build();
    if (inQueryWhere) {
        queryProperty.put(ctx, stringQuery);
    } else {
        LindenFilter filter = LindenQueryFilterBuilder.buildQueryFilter(stringQuery);
        filterProperty.put(ctx, filter);
    }
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenQueryStringQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenQueryStringQueryBuilder)

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