Search in sources :

Example 6 with LindenFilter

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

the class TestBQL method testBetweenPred.

@Test
public void testBetweenPred() throws Exception {
    String bql = "SELECT * FROM linden WHERE id BETWEEN 2000 AND 2001";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter filter = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2000", "2001", true, true);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title BETWEEN 'black' AND \"yellow\"";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filter = LindenRangeFilterBuilder.buildRangeFilter("title", LindenType.STRING, "black", "yellow", true, true);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE id NOT BETWEEN 2000 AND 2002";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter filter1 = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, null, "2000", false, false);
    LindenFilter filter2 = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2002", null, false, false);
    LindenBooleanFilter booleanFilter = new LindenBooleanFilter();
    booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter1).setClause(LindenBooleanClause.SHOULD));
    booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter2).setClause(LindenBooleanClause.SHOULD));
    LindenFilter expectedFilter = new LindenFilter().setBooleanFilter(booleanFilter);
    Assert.assertEquals(expectedFilter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title BETWEEN $a AND $b";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(null, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title BETWEEN 'black' AND \"yellow\"";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filter = LindenRangeFilterBuilder.buildRangeFilter("title", LindenType.STRING, "black", "yellow", true, true);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden by id NOT BETWEEN 2000 AND 2002";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query1 = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, null, "2000", false, false);
    LindenQuery query2 = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "2002", null, false, false);
    LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
    builder.addQuery(query1, LindenBooleanClause.SHOULD);
    builder.addQuery(query2, LindenBooleanClause.SHOULD);
    Assert.assertEquals(builder.build(), lindenRequest.getQuery());
    bql = "SELECT * FROM linden by title BETWEEN 'black' AND \"yel\"\"low\"";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query = LindenRangeQueryBuilder.buildRangeQuery("title", LindenType.STRING, "black", "yel\"low", true, true);
    Assert.assertEquals(query, lindenRequest.getQuery());
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenBooleanFilter(com.xiaomi.linden.thrift.common.LindenBooleanFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanSubFilter(com.xiaomi.linden.thrift.common.LindenBooleanSubFilter) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 7 with LindenFilter

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

the class TestBQL method testInPredicate.

@Test
public void testInPredicate() {
    String bql = "select * from linden where id in (1, 2, 3)";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenBooleanFilterBuilder filterBuilder = new LindenBooleanFilterBuilder();
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.SHOULD);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.SHOULD);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.SHOULD);
    LindenFilter expected = filterBuilder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "select * from linden where id not in (1, 2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filterBuilder = new LindenBooleanFilterBuilder();
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.MUST_NOT);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.MUST_NOT);
    expected = filterBuilder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "select * from linden where id in (1) except (2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filterBuilder = new LindenBooleanFilterBuilder();
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.SHOULD);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.MUST_NOT);
    expected = filterBuilder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "select * from linden where dy.long in (1) except (2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filterBuilder = new LindenBooleanFilterBuilder();
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("dy", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.SHOULD);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("dy", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("dy", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.MUST_NOT);
    expected = filterBuilder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "select * from linden by id in (1) except (2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenBooleanQueryBuilder queryBuilder = new LindenBooleanQueryBuilder();
    queryBuilder.addQuery(LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.SHOULD);
    queryBuilder.addQuery(LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    queryBuilder.addQuery(LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.MUST_NOT);
    LindenQuery query = queryBuilder.build();
    Assert.assertEquals(query, lindenRequest.getQuery());
    bql = "select * from linden by id in ($a) except (2, $b)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    queryBuilder = new LindenBooleanQueryBuilder();
    queryBuilder.addQuery(LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    query = queryBuilder.build();
    Assert.assertEquals(query, lindenRequest.getQuery());
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 8 with LindenFilter

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

the class BooleanFilterConstructor method construct.

@Override
protected Filter construct(LindenFilter lindenFilter, LindenConfig config) throws Exception {
    List<LindenBooleanSubFilter> booleanSubFilterList = lindenFilter.getBooleanFilter().getFilters();
    BooleanFilter booleanFilter = new BooleanFilter();
    for (LindenBooleanSubFilter booleanSubFilter : booleanSubFilterList) {
        LindenFilter subFilter = booleanSubFilter.getFilter();
        switch(booleanSubFilter.clause) {
            case MUST:
                booleanFilter.add(FilterConstructor.constructFilter(subFilter, config), BooleanClause.Occur.MUST);
                continue;
            case SHOULD:
                booleanFilter.add(FilterConstructor.constructFilter(subFilter, config), BooleanClause.Occur.SHOULD);
                continue;
            case MUST_NOT:
                booleanFilter.add(FilterConstructor.constructFilter(subFilter, config), BooleanClause.Occur.MUST_NOT);
        }
    }
    return booleanFilter;
}
Also used : BooleanFilter(org.apache.lucene.queries.BooleanFilter) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenBooleanSubFilter(com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)

Example 9 with LindenFilter

use of com.xiaomi.linden.thrift.common.LindenFilter 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 10 with LindenFilter

use of com.xiaomi.linden.thrift.common.LindenFilter 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)

Aggregations

LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)22 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)17 LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)10 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)9 Test (org.junit.Test)8 LindenType (com.xiaomi.linden.thrift.common.LindenType)7 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)7 LindenBooleanFilterBuilder (com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder)6 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)6 AbstractMap (java.util.AbstractMap)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)5 LindenWildcardQuery (com.xiaomi.linden.thrift.common.LindenWildcardQuery)3 ArrayList (java.util.ArrayList)3 LindenQueryFilter (com.xiaomi.linden.thrift.common.LindenQueryFilter)2 LindenQueryStringQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenQueryStringQueryBuilder)1 GroupParam (com.xiaomi.linden.thrift.common.GroupParam)1 LindenBooleanClause (com.xiaomi.linden.thrift.common.LindenBooleanClause)1 LindenDeleteRequest (com.xiaomi.linden.thrift.common.LindenDeleteRequest)1