Search in sources :

Example 6 with LindenQuery

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

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

the class TestLindenGroupSearch method groupSearchTest1.

@Test
public void groupSearchTest1() 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 += getScore(i, j) * rank();\n" + "      }\n" + "    }\n" + "    return sum;";
    LindenQuery query = new LindenFlexibleQueryBuilder().setQuery("lucene").addField("title").addModel("test", function).build();
    LindenSearchRequest request = new LindenSearchRequest().setQuery(query).setSource(false).setExplain(false);
    request.setGroupParam(new GroupParam("cat1").setGroupInnerLimit(2));
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(true, result.isSuccess());
    Assert.assertEquals(5, result.getTotalHits());
    Assert.assertEquals(3, result.getTotalGroups());
    Assert.assertEquals(3, result.getHitsSize());
    Assert.assertEquals(2, result.getHits().get(0).getGroupHitsSize());
    Assert.assertEquals(1, result.getHits().get(1).getGroupHitsSize());
    Assert.assertEquals(2, result.getHits().get(2).getGroupHitsSize());
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenFlexibleQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenFlexibleQueryBuilder) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) GroupParam(com.xiaomi.linden.thrift.common.GroupParam) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 8 with LindenQuery

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

the class BooleanQueryConstructor method construct.

@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws Exception {
    if (lindenQuery.isSetBooleanQuery()) {
        LindenBooleanQuery ldBoolQuery = lindenQuery.getBooleanQuery();
        BooleanQuery booleanQuery = new BooleanQuery(ldBoolQuery.isDisableCoord());
        for (LindenBooleanSubQuery subQuery : ldBoolQuery.getQueries()) {
            Query query = constructQuery(subQuery.getQuery(), config);
            switch(subQuery.getClause()) {
                case SHOULD:
                    booleanQuery.add(query, BooleanClause.Occur.SHOULD);
                    break;
                case MUST:
                    booleanQuery.add(query, BooleanClause.Occur.MUST);
                    break;
                case MUST_NOT:
                    booleanQuery.add(query, BooleanClause.Occur.MUST_NOT);
                    break;
                default:
                    throw new IOException("This should never happen, boolean clause is " + subQuery.getClause());
            }
        }
        return booleanQuery;
    }
    return null;
}
Also used : LindenBooleanQuery(com.xiaomi.linden.thrift.common.LindenBooleanQuery) LindenBooleanQuery(com.xiaomi.linden.thrift.common.LindenBooleanQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) LindenBooleanQuery(com.xiaomi.linden.thrift.common.LindenBooleanQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) LindenBooleanSubQuery(com.xiaomi.linden.thrift.common.LindenBooleanSubQuery) LindenBooleanSubQuery(com.xiaomi.linden.thrift.common.LindenBooleanSubQuery) IOException(java.io.IOException)

Example 9 with LindenQuery

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

the class FilteredQueryConstructor method construct.

@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws Exception {
    Query query = QueryConstructor.constructQuery(lindenQuery.getFilteredQuery().getLindenQuery(), config);
    Filter filter = FilterConstructor.constructFilter(lindenQuery.getFilteredQuery().getLindenFilter(), config);
    return new FilteredQuery(query, filter);
}
Also used : Query(org.apache.lucene.search.Query) FilteredQuery(org.apache.lucene.search.FilteredQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) Filter(org.apache.lucene.search.Filter) FilteredQuery(org.apache.lucene.search.FilteredQuery)

Example 10 with LindenQuery

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

the class LindenBooleanQueryBuilder method build.

@Override
public LindenQuery build() {
    LindenBooleanQuery booleanQuery = new LindenBooleanQuery();
    for (Map.Entry<LindenQuery, LindenBooleanClause> query : queries) {
        booleanQuery.addToQueries(new LindenBooleanSubQuery().setQuery(query.getKey()).setClause(query.getValue()));
    }
    booleanQuery.setDisableCoord(disableCoord);
    return new LindenQuery().setBooleanQuery(booleanQuery).setBoost(boost);
}
Also used : LindenBooleanQuery(com.xiaomi.linden.thrift.common.LindenBooleanQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanSubQuery(com.xiaomi.linden.thrift.common.LindenBooleanSubQuery) LindenBooleanClause(com.xiaomi.linden.thrift.common.LindenBooleanClause) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

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