use of com.xiaomi.linden.thrift.common.LindenTerm in project linden by XiaoMi.
the class TestBQL method testScoreModel.
@Test
public void testScoreModel() {
String bql = "select * from linden where title = 'qq' " + "using score model test (Float link = 1)" + " begin" + " return score();" + " end";
LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertEquals(new LindenTerm("title", "qq"), lindenRequest.getFilter().getTermFilter().getTerm());
LindenScoreModel lindenScoreModel = new LindenScoreModel().setName("test").setFunc("return score();");
lindenScoreModel.addToParams(new LindenInputParam("link").setValue(new LindenValue().setDoubleValue(1)));
Assert.assertTrue(lindenRequest.getQuery().isSetMatchAllQuery());
Assert.assertEquals(lindenScoreModel, lindenRequest.getQuery().getScoreModel());
bql = "select * from linden where title = 'qq' " + "using score model override test (Float link = 1)" + " begin" + " return score();" + " end";
lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertEquals(new LindenTerm("title", "qq"), lindenRequest.getFilter().getTermFilter().getTerm());
lindenScoreModel = new LindenScoreModel().setName("test").setFunc("return score();").setOverride(true);
lindenScoreModel.addToParams(new LindenInputParam("link").setValue(new LindenValue().setDoubleValue(1)));
Assert.assertTrue(lindenRequest.getQuery().isSetMatchAllQuery());
Assert.assertEquals(lindenScoreModel, lindenRequest.getQuery().getScoreModel());
// list input params.
bql = "select * from linden where title = 'qq' " + "using score model test (Float link = [1, 2], Integer pos = [3, 4], Map<String,Double> kv = {\"a\":1, \"b\":2})" + " begin" + " return score();" + " end";
lindenRequest = compiler.compile(bql).getSearchRequest();
lindenScoreModel = new LindenScoreModel().setName("test").setFunc("return score();");
LindenInputParam inputParam = new LindenInputParam("link").setValue(new LindenValue());
inputParam.getValue().addToDoubleValues(1);
inputParam.getValue().addToDoubleValues(2);
lindenScoreModel.addToParams(inputParam);
LindenInputParam inputParam2 = new LindenInputParam("pos").setValue(new LindenValue());
inputParam2.getValue().addToLongValues(3);
inputParam2.getValue().addToLongValues(4);
lindenScoreModel.addToParams(inputParam2);
LindenInputParam inputParam3 = new LindenInputParam("kv").setValue(new LindenValue());
inputParam3.getValue().putToMapValue(new LindenValue().setStringValue("a"), new LindenValue().setDoubleValue(1));
inputParam3.getValue().putToMapValue(new LindenValue().setStringValue("b"), new LindenValue().setDoubleValue(2));
lindenScoreModel.addToParams(inputParam3);
Assert.assertEquals(lindenScoreModel, lindenRequest.getQuery().getScoreModel());
}
use of com.xiaomi.linden.thrift.common.LindenTerm in project linden by XiaoMi.
the class TestBQL method testEscapedColumnName.
@Test
public void testEscapedColumnName() {
String bql = "select * from linden where `select` = \"qq\" and id = 211 limit 10, 50";
LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertTrue(lindenRequest.getFilter().isSetBooleanFilter());
LindenBooleanFilter booleanFilter = lindenRequest.getFilter().getBooleanFilter();
Assert.assertEquals(2, booleanFilter.getFiltersSize());
Assert.assertEquals(LindenBooleanClause.MUST, booleanFilter.getFilters().get(0).getClause());
Assert.assertEquals(new LindenTerm("select", "qq"), booleanFilter.getFilters().get(0).getFilter().getTermFilter().getTerm());
bql = "SELECT * FROM linden WHERE `source` <> 'red'";
lindenRequest = compiler.compile(bql).getSearchRequest();
booleanFilter = new LindenBooleanFilter();
LindenFilter termFilter = LindenTermFilterBuilder.buildTermFilter("source", "red");
booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(termFilter).setClause(LindenBooleanClause.MUST_NOT));
Assert.assertEquals(booleanFilter, lindenRequest.getFilter().getBooleanFilter());
bql = "select * from linden where `explain` in (1, 2, 3)";
lindenRequest = compiler.compile(bql).getSearchRequest();
LindenBooleanFilterBuilder builder = new LindenBooleanFilterBuilder();
builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "1"), LindenBooleanClause.SHOULD);
builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "2"), LindenBooleanClause.SHOULD);
builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "3"), LindenBooleanClause.SHOULD);
LindenFilter expected = builder.build();
Assert.assertEquals(expected, lindenRequest.getFilter());
bql = "SELECT * FROM linden WHERE `route` BETWEEN 'black' AND \"yellow\"";
lindenRequest = compiler.compile(bql).getSearchRequest();
LindenFilter filter = LindenRangeFilterBuilder.buildRangeFilter("route", LindenType.STRING, "black", "yellow", true, true);
Assert.assertEquals(filter, lindenRequest.getFilter());
bql = "SELECT `delete`, `score` FROM linden where title = 'sed' route by 0, 1, 2";
lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertEquals(2, lindenRequest.getSourceFieldsSize());
Assert.assertEquals("delete", lindenRequest.getSourceFields().get(0));
Assert.assertEquals("score", lindenRequest.getSourceFields().get(1));
bql = "SELECT * FROM linden QUERY `FROM` LIKE 'sed*'";
lindenRequest = compiler.compile(bql).getSearchRequest();
LindenQuery query = new LindenQuery();
query.setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("FROM"));
Assert.assertEquals(query, lindenRequest.getQuery());
bql = "SELECT * FROM linden browse by `group`(5)";
lindenRequest = compiler.compile(bql).getSearchRequest();
LindenFacet facetRequest = new LindenFacet();
facetRequest.addToFacetParams(new LindenFacetParam().setTopN(5).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("group")));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
bql = "SELECT title, rank FROM linden drill down `group`('big')";
lindenRequest = compiler.compile(bql).getSearchRequest();
facetRequest = new LindenFacet();
facetRequest.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("group").setPath("big"));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
}
use of com.xiaomi.linden.thrift.common.LindenTerm in project linden by XiaoMi.
the class TestBQL method testDelete.
@Test
public void testDelete() {
String bql = "delete from linden where title = 'sed'";
LindenDeleteRequest lindenRequest = compiler.compile(bql).getDeleteRequest();
LindenTermFilter termFilter = new LindenTermFilter().setTerm(new LindenTerm().setField("title").setValue("sed"));
Assert.assertEquals(termFilter, lindenRequest.getQuery().getFilteredQuery().getLindenFilter().getTermFilter());
bql = "delete from linden";
lindenRequest = compiler.compile(bql).getDeleteRequest();
Assert.assertTrue(lindenRequest.getQuery().isSetMatchAllQuery());
bql = "delete from tag_1,tag_2";
lindenRequest = compiler.compile(bql).getDeleteRequest();
Assert.assertEquals(lindenRequest.getIndexNames().size(), 2);
Assert.assertEquals(lindenRequest.getIndexNames().get(0), "tag_1");
Assert.assertEquals(lindenRequest.getIndexNames().get(1), "tag_2");
bql = "delete from linden where id = '123' route by 1, 2, 3";
lindenRequest = compiler.compile(bql).getDeleteRequest();
Assert.assertTrue(lindenRequest.getQuery().isSetFilteredQuery());
Assert.assertEquals(3, lindenRequest.getRouteParam().getShardParamsSize());
Assert.assertEquals(1, lindenRequest.getRouteParam().getShardParams().get(0).getShardId());
Assert.assertEquals(2, lindenRequest.getRouteParam().getShardParams().get(1).getShardId());
}
use of com.xiaomi.linden.thrift.common.LindenTerm in project linden by XiaoMi.
the class TestBQL method testEqualPredicate.
@Test
public void testEqualPredicate() {
String bql = "select * from linden where title = \"qq\" and id = 211 limit 10, 50";
LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertTrue(lindenRequest.getFilter().isSetBooleanFilter());
LindenBooleanFilter booleanFilter = lindenRequest.getFilter().getBooleanFilter();
Assert.assertEquals(2, booleanFilter.getFiltersSize());
Assert.assertEquals(LindenBooleanClause.MUST, booleanFilter.getFilters().get(0).getClause());
Assert.assertEquals(new LindenTerm("title", "qq"), booleanFilter.getFilters().get(0).getFilter().getTermFilter().getTerm());
Assert.assertEquals(LindenBooleanClause.MUST, booleanFilter.getFilters().get(1).getClause());
Assert.assertEquals(new LindenRange("id", LindenType.LONG, true, true).setStartValue("211").setEndValue("211"), booleanFilter.getFilters().get(1).getFilter().getRangeFilter().getRange());
Assert.assertEquals(10, lindenRequest.getOffset());
Assert.assertEquals(50, lindenRequest.getLength());
bql = "delete from default where title = \"q\"\"q\" and id = 211";
LindenDeleteRequest deleteRequest = compiler.compile(bql).getDeleteRequest();
Assert.assertTrue(deleteRequest.getQuery().isSetFilteredQuery());
Assert.assertTrue(deleteRequest.getQuery().getFilteredQuery().isSetLindenFilter());
Assert.assertTrue(deleteRequest.getQuery().getFilteredQuery().getLindenFilter().isSetBooleanFilter());
booleanFilter = deleteRequest.getQuery().getFilteredQuery().getLindenFilter().getBooleanFilter();
Assert.assertEquals(2, booleanFilter.getFiltersSize());
Assert.assertEquals(LindenBooleanClause.MUST, booleanFilter.getFilters().get(0).getClause());
Assert.assertEquals(new LindenTerm("title", "q\"q"), booleanFilter.getFilters().get(0).getFilter().getTermFilter().getTerm());
Assert.assertEquals(LindenBooleanClause.MUST, booleanFilter.getFilters().get(1).getClause());
Assert.assertEquals(new LindenRange("id", LindenType.LONG, true, true).setStartValue("211").setEndValue("211"), booleanFilter.getFilters().get(1).getFilter().getRangeFilter().getRange());
bql = "delete from default where title = $a and id = 211";
deleteRequest = compiler.compile(bql).getDeleteRequest();
Assert.assertTrue(deleteRequest.getQuery().isSetFilteredQuery());
Assert.assertTrue(deleteRequest.getQuery().getFilteredQuery().isSetLindenFilter());
Assert.assertTrue(deleteRequest.getQuery().getFilteredQuery().getLindenFilter().isSetRangeFilter());
bql = "select * from linden where dynmaicTitle.STRING = \"qq\" and dynamicId.int = 211 limit 10, 50";
lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertTrue(lindenRequest.getFilter().isSetBooleanFilter());
booleanFilter = lindenRequest.getFilter().getBooleanFilter();
Assert.assertEquals(2, booleanFilter.getFiltersSize());
Assert.assertEquals(LindenBooleanClause.MUST, booleanFilter.getFilters().get(0).getClause());
Assert.assertEquals(new LindenTerm("dynmaicTitle", "qq"), booleanFilter.getFilters().get(0).getFilter().getTermFilter().getTerm());
Assert.assertEquals(LindenBooleanClause.MUST, booleanFilter.getFilters().get(1).getClause());
Assert.assertEquals(new LindenRange("dynamicId", LindenType.INTEGER, true, true).setStartValue("211").setEndValue("211"), booleanFilter.getFilters().get(1).getFilter().getRangeFilter().getRange());
Assert.assertEquals(10, lindenRequest.getOffset());
Assert.assertEquals(50, lindenRequest.getLength());
bql = "select * from linden by title = \"qq\" subBoost by 0.5 and id = 211 andDisableCoord limit 10, 50";
lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertFalse(lindenRequest.isSetFilter());
Assert.assertTrue(lindenRequest.getQuery().isSetBooleanQuery());
LindenBooleanQuery query = lindenRequest.getQuery().getBooleanQuery();
Assert.assertTrue(query.isDisableCoord());
Assert.assertEquals(2, query.getQueriesSize());
Assert.assertEquals(LindenBooleanClause.MUST, query.getQueries().get(0).clause);
Assert.assertEquals(new LindenTerm("title", "qq"), query.getQueries().get(0).getQuery().getTermQuery().getTerm());
Assert.assertEquals(0.5, query.getQueries().get(0).getQuery().getBoost(), 0.001);
Assert.assertEquals(LindenBooleanClause.MUST, query.getQueries().get(1).clause);
Assert.assertEquals(new LindenRange("id", LindenType.LONG, true, true).setStartValue("211").setEndValue("211"), query.getQueries().get(1).getQuery().getRangeQuery().getRange());
Assert.assertEquals(10, lindenRequest.getOffset());
Assert.assertEquals(50, lindenRequest.getLength());
}
Aggregations