Search in sources :

Example 21 with LindenQuery

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

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

the class BQLCompilerAnalyzer method exitDelete_stmt.

@Override
public void exitDelete_stmt(BQLParser.Delete_stmtContext ctx) {
    deleteRequest = new LindenDeleteRequest();
    if (ctx.dw != null) {
        LindenFilter filter = filterProperty.get(ctx.dw);
        if (filter == null) {
            throw new ParseCancellationException(new SemanticException(ctx, "Filter parse failed"));
        }
        LindenQuery query = LindenQueryBuilder.buildMatchAllQuery();
        query = LindenQueryBuilder.buildFilteredQuery(query, filter);
        deleteRequest.setQuery(query);
    } else {
        deleteRequest.setQuery(LindenQueryBuilder.buildMatchAllQuery());
    }
    if (ctx.route_param != null) {
        deleteRequest.setRouteParam((SearchRouteParam) valProperty.get(ctx.route_param));
    }
    if (ctx.indexes != null) {
        deleteRequest.setIndexNames((List<String>) valProperty.get(ctx.indexes));
    }
}
Also used : LindenDeleteRequest(com.xiaomi.linden.thrift.common.LindenDeleteRequest) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException)

Example 23 with LindenQuery

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

Example 24 with LindenQuery

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

the class TestBQL method testQueryPredicate.

@Test
public void testQueryPredicate() {
    String bql = "select * from linden where query is 'title:a' and id = 233";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertTrue(lindenRequest.getFilter().isSetBooleanFilter());
    Assert.assertEquals(2, lindenRequest.getFilter().getBooleanFilter().getFilters().size());
    Assert.assertTrue(lindenRequest.getFilter().getBooleanFilter().getFilters().get(0).getFilter().isSetQueryFilter());
    bql = "select * from linden by query is 'abc' and query is 'def' where id = 1 or id = 3";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertTrue(lindenRequest.getQuery().isSetBooleanQuery());
    Assert.assertEquals(2, lindenRequest.getQuery().getBooleanQuery().getQueriesSize());
    Assert.assertTrue(lindenRequest.getFilter().isSetBooleanFilter());
    Assert.assertEquals(2, lindenRequest.getFilter().getBooleanFilter().getFilters().size());
    bql = "select * from linden by query is 'title:abc bcd' disableCoord OP(AND)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQueryStringQueryBuilder builder = new LindenQueryStringQueryBuilder();
    builder.setQuery("title:abc bcd").setOperator(Operator.AND);
    builder.setDisableCoord(true);
    LindenQuery expected = builder.build();
    Assert.assertEquals(expected, lindenRequest.getQuery());
}
Also used : LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) LindenQueryStringQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenQueryStringQueryBuilder) Test(org.junit.Test)

Example 25 with LindenQuery

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

the class TestLindenCore method flexibleQueryTest.

@Test
public void flexibleQueryTest() throws IOException {
    String function = "    float sum = 0;\n" + "    for (int i = 0; i < getFieldLength(); ++i) {\n" + "      for (int j = 0; j < getTermLength(); ++j) {\n" + "        sum += 100 * getScore(i, j) * rank();\n" + "      }\n" + "    }\n" + "    return sum;";
    LindenQuery flexQuery = new LindenFlexibleQueryBuilder().setQuery("lucene").addField("title").addModel("test", function).build();
    LindenResult result = lindenCore.search(new LindenSearchRequest().setQuery(flexQuery));
    Assert.assertEquals(4, result.getTotalHits());
    flexQuery = new LindenFlexibleQueryBuilder().setQuery("lucene 1").addField("title").addModel("test", function).setFullMatch(true).build();
    result = lindenCore.search(new LindenSearchRequest().setQuery(flexQuery));
    Assert.assertEquals(1, result.getTotalHits());
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenFlexibleQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenFlexibleQueryBuilder) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

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