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());
}
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());
}
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;
}
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);
}
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);
}
Aggregations