use of com.xiaomi.linden.lucene.query.flexiblequery.FlexibleQuery 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.lucene.query.flexiblequery.FlexibleQuery in project linden by XiaoMi.
the class FlexibleQueryConstructor method construct.
@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws IOException {
if (lindenQuery.isSetFlexQuery()) {
LindenFlexibleQuery lindenFlexibleQuery = lindenQuery.getFlexQuery();
List<FlexibleQuery.FlexibleField> fields = Lists.newArrayList();
for (LindenSearchField field : lindenFlexibleQuery.getFields()) {
fields.add(new FlexibleQuery.FlexibleField(field.getName(), field.getBoost()));
}
List<FlexibleQuery.FlexibleField> globalFields = initGlobalFields(lindenFlexibleQuery.isGlobalIDF(), lindenFlexibleQuery.getGlobalFields(), lindenFlexibleQuery.getFields());
FlexibleQuery flexibleQuery = new FlexibleQuery(fields, globalFields, lindenFlexibleQuery.getQuery(), lindenFlexibleQuery.getModel(), config);
flexibleQuery.setFullMatch(lindenFlexibleQuery.isFullMatch());
flexibleQuery.setGlobalIDF(lindenFlexibleQuery.isGlobalIDF());
flexibleQuery.setMatchRatio(lindenFlexibleQuery.getMatchRatio());
return flexibleQuery;
}
return null;
}
Aggregations