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