Search in sources :

Example 61 with LindenSearchRequest

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

Example 62 with LindenSearchRequest

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

the class TestBQL method testFacetBrowsing.

@Test
public void testFacetBrowsing() {
    String bql = "SELECT title, rank FROM linden browse by color";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals("title", lindenRequest.getSourceFields().get(0));
    Assert.assertEquals("rank", lindenRequest.getSourceFields().get(1));
    LindenFacet facetRequest = new LindenFacet();
    facetRequest.addToFacetParams(new LindenFacetParam().setFacetDimAndPath(new LindenFacetDimAndPath().setDim("color")));
    Assert.assertEquals(facetRequest, lindenRequest.getFacet());
    bql = "SELECT * FROM linden browse by color(5)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    facetRequest = new LindenFacet();
    facetRequest.addToFacetParams(new LindenFacetParam().setTopN(5).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("color")));
    Assert.assertEquals(facetRequest, lindenRequest.getFacet());
    bql = "SELECT * FROM linden browse by color(6, \"red\")";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    facetRequest = new LindenFacet();
    facetRequest.addToFacetParams(new LindenFacetParam().setTopN(6).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("color").setPath("red")));
    Assert.assertEquals(facetRequest, lindenRequest.getFacet());
    bql = "SELECT * FROM linden browse by color(6, 'light/gray/white')";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    facetRequest = new LindenFacet();
    facetRequest.addToFacetParams(new LindenFacetParam().setTopN(6).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("color").setPath("light/gray/white")));
    Assert.assertEquals(facetRequest, lindenRequest.getFacet());
    bql = "SELECT * FROM linden browse by color(6, 'light/gray/white'), color(5, 'red'),  shape(10, 'rectangle')";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    facetRequest = new LindenFacet();
    facetRequest.addToFacetParams(new LindenFacetParam().setTopN(6).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("color").setPath("light/gray/white")));
    facetRequest.addToFacetParams(new LindenFacetParam().setTopN(5).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("color").setPath("red")));
    facetRequest.addToFacetParams(new LindenFacetParam().setTopN(10).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("shape").setPath("rectangle")));
    Assert.assertEquals(facetRequest, lindenRequest.getFacet());
}
Also used : LindenFacetDimAndPath(com.xiaomi.linden.thrift.common.LindenFacetDimAndPath) LindenFacetParam(com.xiaomi.linden.thrift.common.LindenFacetParam) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) LindenFacet(com.xiaomi.linden.thrift.common.LindenFacet) Test(org.junit.Test)

Example 63 with LindenSearchRequest

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

Example 64 with LindenSearchRequest

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

the class TestLindenCore method testScoreModelPlugin.

@Test
public void testScoreModelPlugin() throws IOException {
    String bql = "select * from linden where title='lucene'" + " using score model plugin com.xiaomi.linden.core.plugin.TestScoreModelStrategy" + "(Long param1 = $long1, Map<String, Double> dict = {'中文': 1.0, 'b': 2.0});";
    LindenSearchRequest searchRequest = bqlCompiler.compile(bql).getSearchRequest();
    LindenResult result = lindenCore.search(searchRequest);
    Assert.assertEquals(4, result.getTotalHits());
    Assert.assertEquals(14.3, result.getHits().get(0).getScore(), 0.01);
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 65 with LindenSearchRequest

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

the class TestLindenCore method likeTest.

@Test
public void likeTest() throws Exception {
    String bql = "select * from linden by field1 like \"aa*\" source boost by 2";
    LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
    Query query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
    Assert.assertTrue(query instanceof WildcardQuery);
    Assert.assertEquals("field1:aa*^2.0", query.toString());
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(1, result.getTotalHits());
    bql = "select * from linden by field1 not like \"aaa*\" source";
    request = bqlCompiler.compile(bql).getSearchRequest();
    query = QueryConstructor.constructQuery(request.getQuery(), lindenConfig);
    Assert.assertTrue(query instanceof BooleanQuery);
    Assert.assertEquals("+*:* -field1:aaa*", query.toString());
    result = lindenCore.search(request);
    Assert.assertEquals(5, result.getTotalHits());
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) 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