Search in sources :

Example 1 with LindenBooleanQuery

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

the class BooleanQueryConstructor method construct.

@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws Exception {
    if (lindenQuery.isSetBooleanQuery()) {
        LindenBooleanQuery ldBoolQuery = lindenQuery.getBooleanQuery();
        BooleanQuery booleanQuery = new BooleanQuery(ldBoolQuery.isDisableCoord());
        for (LindenBooleanSubQuery subQuery : ldBoolQuery.getQueries()) {
            Query query = constructQuery(subQuery.getQuery(), config);
            switch(subQuery.getClause()) {
                case SHOULD:
                    booleanQuery.add(query, BooleanClause.Occur.SHOULD);
                    break;
                case MUST:
                    booleanQuery.add(query, BooleanClause.Occur.MUST);
                    break;
                case MUST_NOT:
                    booleanQuery.add(query, BooleanClause.Occur.MUST_NOT);
                    break;
                default:
                    throw new IOException("This should never happen, boolean clause is " + subQuery.getClause());
            }
        }
        return booleanQuery;
    }
    return null;
}
Also used : LindenBooleanQuery(com.xiaomi.linden.thrift.common.LindenBooleanQuery) LindenBooleanQuery(com.xiaomi.linden.thrift.common.LindenBooleanQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) LindenBooleanQuery(com.xiaomi.linden.thrift.common.LindenBooleanQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) LindenBooleanSubQuery(com.xiaomi.linden.thrift.common.LindenBooleanSubQuery) LindenBooleanSubQuery(com.xiaomi.linden.thrift.common.LindenBooleanSubQuery) IOException(java.io.IOException)

Example 2 with LindenBooleanQuery

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

the class LindenBooleanQueryBuilder method build.

@Override
public LindenQuery build() {
    LindenBooleanQuery booleanQuery = new LindenBooleanQuery();
    for (Map.Entry<LindenQuery, LindenBooleanClause> query : queries) {
        booleanQuery.addToQueries(new LindenBooleanSubQuery().setQuery(query.getKey()).setClause(query.getValue()));
    }
    booleanQuery.setDisableCoord(disableCoord);
    return new LindenQuery().setBooleanQuery(booleanQuery).setBoost(boost);
}
Also used : LindenBooleanQuery(com.xiaomi.linden.thrift.common.LindenBooleanQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanSubQuery(com.xiaomi.linden.thrift.common.LindenBooleanSubQuery) LindenBooleanClause(com.xiaomi.linden.thrift.common.LindenBooleanClause) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 3 with LindenBooleanQuery

use of com.xiaomi.linden.thrift.common.LindenBooleanQuery 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)

Aggregations

LindenBooleanQuery (com.xiaomi.linden.thrift.common.LindenBooleanQuery)3 LindenBooleanSubQuery (com.xiaomi.linden.thrift.common.LindenBooleanSubQuery)2 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)2 LindenBooleanClause (com.xiaomi.linden.thrift.common.LindenBooleanClause)1 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)1 LindenDeleteRequest (com.xiaomi.linden.thrift.common.LindenDeleteRequest)1 LindenRange (com.xiaomi.linden.thrift.common.LindenRange)1 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)1 LindenTerm (com.xiaomi.linden.thrift.common.LindenTerm)1 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 Query (org.apache.lucene.search.Query)1 Test (org.junit.Test)1