Search in sources :

Example 16 with LindenSearchRequest

use of com.xiaomi.linden.thrift.common.LindenSearchRequest 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());
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 17 with LindenSearchRequest

use of com.xiaomi.linden.thrift.common.LindenSearchRequest 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());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) LindenResult(com.xiaomi.linden.thrift.common.LindenResult) Query(org.apache.lucene.search.Query) FlexibleQuery(com.xiaomi.linden.lucene.query.flexiblequery.FlexibleQuery) FilteredQuery(org.apache.lucene.search.FilteredQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) FlexibleQuery(com.xiaomi.linden.lucene.query.flexiblequery.FlexibleQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) FilteredQuery(org.apache.lucene.search.FilteredQuery) Test(org.junit.Test)

Example 18 with LindenSearchRequest

use of com.xiaomi.linden.thrift.common.LindenSearchRequest 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());
}
Also used : LindenDeleteRequest(com.xiaomi.linden.thrift.common.LindenDeleteRequest) LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 19 with LindenSearchRequest

use of com.xiaomi.linden.thrift.common.LindenSearchRequest 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"));
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 20 with LindenSearchRequest

use of com.xiaomi.linden.thrift.common.LindenSearchRequest in project linden by XiaoMi.

the class TestLindenCore method lindenBQLScoreModelTest.

@Test
public void lindenBQLScoreModelTest() throws IOException {
    String bql = "select * from linden by query is \"title:lucene\" " + "using score model test(long aa = 100000000000000, String title = ['a', 'b'], long a = 10, Double b = [1, 2, 3, 4]) " + "begin " + "    float sum = 0;\n" + "    for (Double m : b) {\n" + "      sum += m;\n" + "    }" + "writeExplanation(\"rank:%.2f, a:%d\", rank(), a); " + "return sum + rank() * 10 * a; " + "end explain";
    LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(4, result.getTotalHits());
    Assert.assertEquals(1040f, result.getHits().get(0).getScore(), 0.1f);
    Assert.assertTrue(result.getHits().get(0).getExplanation().toString().contains("value:1040.0, description:rank:10.30, a:10"));
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Aggregations

LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)95 Test (org.junit.Test)90 LindenResult (com.xiaomi.linden.thrift.common.LindenResult)69 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)13 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)9 LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)5 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)5 IOException (java.io.IOException)5 JSONObject (com.alibaba.fastjson.JSONObject)4 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)4 LindenDeleteRequest (com.xiaomi.linden.thrift.common.LindenDeleteRequest)4 LindenScoreModel (com.xiaomi.linden.thrift.common.LindenScoreModel)4 Stopwatch (com.google.common.base.Stopwatch)3 MultiLindenCoreImpl (com.xiaomi.linden.core.search.MultiLindenCoreImpl)3 LindenFlexibleQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenFlexibleQueryBuilder)3 LindenFacet (com.xiaomi.linden.thrift.common.LindenFacet)3 LindenFacetDimAndPath (com.xiaomi.linden.thrift.common.LindenFacetDimAndPath)3 LindenFacetParam (com.xiaomi.linden.thrift.common.LindenFacetParam)3 LindenRequest (com.xiaomi.linden.thrift.common.LindenRequest)3 LindenTerm (com.xiaomi.linden.thrift.common.LindenTerm)3