use of com.xiaomi.linden.thrift.common.LindenResult in project linden by XiaoMi.
the class TestLindenAggregation method aggregationSearchTest.
@Test
public void aggregationSearchTest() throws IOException {
String bql = "SELECT * FROM linden aggregate by intValue({*, *}, {*, -200], {-200, 0}, [0, 10}), " + "dValue.double({*, -5.0}, [-21.79, -0.9}, [-21.80, -0.9}, {1000, *], {0, 11.0])";
LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
LindenResult result = lindenCore.search(request);
Assert.assertEquals("[AggregationResult(field:intValue, labelValues:[LindenLabelAndValue(label:{*,*}, value:15), " + "LindenLabelAndValue(label:{*,-200], value:0), LindenLabelAndValue(label:{-200,0}, value:4), " + "LindenLabelAndValue(label:[0,10}, value:5)]), " + "AggregationResult(field:dValue, labelValues:[LindenLabelAndValue(label:{*,-5.0}, value:2), " + "LindenLabelAndValue(label:[-21.79,-0.9}, value:2), LindenLabelAndValue(label:[-21.80,-0.9}, value:3), " + "LindenLabelAndValue(label:{1000,*], value:0), LindenLabelAndValue(label:{0,11.0], value:6)])]", result.getAggregationResults().toString());
}
use of com.xiaomi.linden.thrift.common.LindenResult in project linden by XiaoMi.
the class TestLindenCore method testQueryString.
@Test
public void testQueryString() throws IOException {
String bql = "select * from linden by query is \"title:lucene\"";
LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
LindenResult result = lindenCore.search(request);
Assert.assertEquals(4, result.getTotalHits());
// phrase test
bql = "select * from linden by query is 'title:\"lucene 1\"'";
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(1, result.getTotalHits());
bql = "select * from linden by query is 'title:(lucene 1)' OP(AND)";
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(1, result.getTotalHits());
bql = "select * from linden by query is 'cat1:{* TO 3}'";
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(2, result.getTotalHits());
bql = "select * from linden by query is 'cat1:[3 TO 3]'";
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(1, result.getTotalHits());
bql = "select * from linden by query is 'cat1:[3 TO 3]'";
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(1, result.getTotalHits());
}
use of com.xiaomi.linden.thrift.common.LindenResult in project linden by XiaoMi.
the class TestLindenCore method testBoosts.
@Test
public void testBoosts() throws Exception {
String bql = "select * from linden by title = \"lucene\" and field1 = \"aaa\" boost by 2";
LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
Query query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof BooleanQuery);
Assert.assertEquals(2f, query.getBoost(), DELTA5);
bql = "select * from linden by title = \"lucene\" boost by 3";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof TermQuery);
Assert.assertEquals(3f, query.getBoost(), DELTA5);
bql = "select * from linden by Query is 'title:lucene' subBoost by 4 or tagstr = 'MI4'";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof BooleanQuery);
BooleanQuery booleanQuery = (BooleanQuery) query;
Assert.assertEquals(4f, booleanQuery.clauses().get(0).getQuery().getBoost(), DELTA5);
Assert.assertEquals(1f, booleanQuery.clauses().get(1).getQuery().getBoost(), DELTA5);
request = bqlCompiler.compile(bql).getSearchRequest();
LindenResult result = lindenCore.search(request);
Assert.assertEquals(5, result.getHitsSize());
Assert.assertEquals("1", result.getHits().get(0).getId());
bql = "select * from linden by Query is 'title:lucene' subBoost by 4 or tagstr = 'MI4' subBoost by 2";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof BooleanQuery);
booleanQuery = (BooleanQuery) query;
Assert.assertEquals(4f, booleanQuery.clauses().get(0).getQuery().getBoost(), DELTA5);
Assert.assertEquals(2f, booleanQuery.clauses().get(1).getQuery().getBoost(), DELTA5);
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(5, result.getHitsSize());
Assert.assertEquals("6", result.getHits().get(0).getId());
bql = "select * from linden by rank > 4 subBoost by 4 or tagstr = 'MI4'";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof BooleanQuery);
booleanQuery = (BooleanQuery) query;
Assert.assertEquals(4f, booleanQuery.clauses().get(0).getQuery().getBoost(), DELTA5);
Assert.assertEquals(1f, booleanQuery.clauses().get(1).getQuery().getBoost(), DELTA5);
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(4, result.getHitsSize());
Assert.assertEquals("2", result.getHits().get(0).getId());
bql = "select * from linden by rank > 4 subBoost by 4 or tagstr = 'MI4' subBoost by 2.0";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof BooleanQuery);
booleanQuery = (BooleanQuery) query;
Assert.assertEquals(4f, booleanQuery.clauses().get(0).getQuery().getBoost(), DELTA5);
Assert.assertEquals(2f, booleanQuery.clauses().get(1).getQuery().getBoost(), DELTA5);
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(4, result.getHitsSize());
Assert.assertEquals("6", result.getHits().get(0).getId());
// this boolean query case, does not support term query boost
bql = "select * from linden by title = \"lucene\" and field1 = \"aaa\" boost by 2" + "|select * from linden by title = \"lucene\" boost by 3";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof DisjunctionMaxQuery);
DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query;
Assert.assertEquals(2f, disjunctionMaxQuery.getDisjuncts().get(0).getBoost(), DELTA5);
Assert.assertEquals(3f, disjunctionMaxQuery.getDisjuncts().get(1).getBoost(), DELTA5);
Assert.assertEquals("(((+title:lucene +field1:aaa)^2.0) | title:lucene^3.0)~0.1", query.toString());
bql = "select * from linden by query is \"title:(lucene test)^2\" where rank > 1 boost by 2";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof BooleanQuery);
Assert.assertEquals("(title:lucene title:test)^4.0", query.toString());
bql = "select * from linden by flexible_query is \"lucene test\" in (title)\n" + "using model test(Float m = 10)\n" + "begin\n" + " return score();\n" + "end where rank > 1 source explain boost by 2";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertEquals("FlexibleQuery([title]:[lucene,test])^2.0", query.toString());
bql = "select * from linden by query is \"title:lucene^2\" where rank > 1 order by rank, field1 desc source boost by 2" + "|select * from linden by query is \"title:test^3 field1:aaa\" where rank > 1 order by rank, field1 desc source boost by 0.03";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof DisjunctionMaxQuery);
disjunctionMaxQuery = (DisjunctionMaxQuery) query;
// subQuery 1 is filterQuery which contains a termQuery
Assert.assertTrue(disjunctionMaxQuery.getDisjuncts().get(0) instanceof FilteredQuery);
FilteredQuery filteredQuery = (FilteredQuery) disjunctionMaxQuery.getDisjuncts().get(0);
Assert.assertTrue(filteredQuery.getQuery() instanceof TermQuery);
Assert.assertEquals(4f, filteredQuery.getQuery().getBoost(), DELTA5);
// subQuery 2 is filterQuery which contains a booleanQuery
Assert.assertTrue(disjunctionMaxQuery.getDisjuncts().get(1) instanceof FilteredQuery);
filteredQuery = (FilteredQuery) disjunctionMaxQuery.getDisjuncts().get(1);
Assert.assertTrue(filteredQuery.getQuery() instanceof BooleanQuery);
Assert.assertEquals(0.03f, filteredQuery.getQuery().getBoost(), DELTA5);
Assert.assertEquals("(filtered(title:lucene^4.0)->rank:{1.0 TO *} | filtered((title:test^3.0 field1:aaa)^0.03)->rank:{1.0 TO *})~0.1", query.toString());
// test FlexibleQuery
bql = "select * from linden by flexible_query is \"lucene\" in (title)\n" + "using model test(Float m = 10)\n" + "begin\n" + " float sum = 0f;\n" + " for (int i = 0; i < getFieldLength(); ++i) {\n" + " for (int j = 0; j < getTermLength(); ++j) {\n" + " sum += 10 * getScore(i, j);\n" + " addTermExpl(i, j, 10, getExpl(\"[%.2f]\", sum));\n" + " }\n" + " addFieldExpl(i, 20, getExpl(\"[%.2f]\", sum * m));\n" + " }\n" + " return sum * m;\n" + "end where rank > 1 source explain";
bql = bql + "|select * from linden by flexible_query is \"test^3\" in (field1)\n" + "using model test(Float m = 10)\n" + "begin\n" + " float sum = 0f;\n" + " for (int i = 0; i < getFieldLength(); ++i) {\n" + " for (int j = 0; j < getTermLength(); ++j) {\n" + " sum += 10 * getScore(i, j);\n" + " addTermExpl(i, j, 10, getExpl(\"[%.2f]\", sum));\n" + " }\n" + " addFieldExpl(i, 20, getExpl(\"[%.2f]\", sum * m));\n" + " }\n" + " return sum * m;\n" + "end where rank > 1 source explain boost by 0.5";
request = bqlCompiler.compile(bql).getSearchRequest();
query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
Assert.assertTrue(query instanceof DisjunctionMaxQuery);
disjunctionMaxQuery = (DisjunctionMaxQuery) query;
// subQuery 1 is a FilteredQuery which contains a FlexibleQuery
Assert.assertTrue(disjunctionMaxQuery.getDisjuncts().get(0) instanceof FilteredQuery);
filteredQuery = (FilteredQuery) disjunctionMaxQuery.getDisjuncts().get(0);
Assert.assertTrue(filteredQuery.getQuery() instanceof FlexibleQuery);
Assert.assertEquals(1f, filteredQuery.getQuery().getBoost(), DELTA5);
// subQuery 2 is a FilteredQuery which contains a FlexibleQuery
Assert.assertTrue(disjunctionMaxQuery.getDisjuncts().get(1) instanceof FilteredQuery);
filteredQuery = (FilteredQuery) disjunctionMaxQuery.getDisjuncts().get(1);
Assert.assertTrue(filteredQuery.getQuery() instanceof FlexibleQuery);
Assert.assertEquals(0.5f, filteredQuery.getQuery().getBoost(), DELTA5);
Assert.assertEquals("(filtered(FlexibleQuery([title]:[lucene]))->rank:{1.0 TO *} | filtered(FlexibleQuery([field1]:[test^3.0])^0.5)->rank:{1.0 TO *})~0.1", query.toString());
}
use of com.xiaomi.linden.thrift.common.LindenResult in project linden by XiaoMi.
the class TestLindenCore method deleteTest.
@Test
public void deleteTest() throws IOException {
String bql = "delete from linden where id = '1'";
LindenDeleteRequest request = bqlCompiler.compile(bql).getDeleteRequest();
lindenCore.delete(request);
lindenCore.refresh();
bql = "select * from linden where id = '1'";
LindenSearchRequest searchRequest = bqlCompiler.compile(bql).getSearchRequest();
LindenResult result = lindenCore.search(searchRequest);
Assert.assertEquals(0, result.getTotalHits());
bql = "select * from linden";
searchRequest = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(searchRequest);
Assert.assertEquals(5, result.getTotalHits());
bql = "select * from linden where id >= '1' and id <='3'";
searchRequest = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(searchRequest);
Assert.assertEquals(2, result.getTotalHits());
bql = "delete from linden where id >= '1' and id <='3'";
LindenDeleteRequest deleteRequest = bqlCompiler.compile(bql).getDeleteRequest();
lindenCore.delete(deleteRequest);
lindenCore.refresh();
bql = "select * from linden";
searchRequest = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(searchRequest);
Assert.assertEquals(3, result.getTotalHits());
bql = "delete from linden";
request = bqlCompiler.compile(bql).getDeleteRequest();
lindenCore.delete(request);
lindenCore.refresh();
bql = "select * from linden";
searchRequest = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(searchRequest);
Assert.assertEquals(0, result.getTotalHits());
}
use of com.xiaomi.linden.thrift.common.LindenResult in project linden by XiaoMi.
the class TestLindenCore method bqlSortTest.
@Test
public void bqlSortTest() throws IOException {
String bql = "select * from linden by query is \"title:lucene\" order by rank, field1 desc source";
LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
LindenResult result = lindenCore.search(request);
Assert.assertEquals(4, result.getTotalHits());
Assert.assertEquals("4", result.getHits().get(0).getId());
Assert.assertEquals("10.3", result.getHits().get(0).getFields().get("rank"));
Assert.assertEquals("ddd", result.getHits().get(0).getFields().get("field1"));
Assert.assertEquals("3", result.getHits().get(1).getId());
Assert.assertEquals("4.5", result.getHits().get(1).getFields().get("rank"));
Assert.assertEquals("ccc", result.getHits().get(1).getFields().get("field1"));
bql = "select * from linden by query is \"title:lucene\" order by rank, field1 asc source";
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals(4, result.getTotalHits());
Assert.assertEquals("4", result.getHits().get(0).getId());
Assert.assertEquals("10.3", result.getHits().get(0).getFields().get("rank"));
Assert.assertEquals("ddd", result.getHits().get(0).getFields().get("field1"));
Assert.assertEquals("2", result.getHits().get(1).getId());
Assert.assertEquals("4.5", result.getHits().get(1).getFields().get("rank"));
Assert.assertEquals("bbb", result.getHits().get(1).getFields().get("field1"));
bql = "select * from linden by query is \"title:lucene\" order by rank asc source";
request = bqlCompiler.compile(bql).getSearchRequest();
result = lindenCore.search(request);
Assert.assertEquals("1", result.getHits().get(0).getId());
Assert.assertEquals("1.2", result.getHits().get(0).getFields().get("rank"));
}
Aggregations