Search in sources :

Example 1 with LindenFilter

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

the class TestBQL method testSpatialFilter.

@Test
public void testSpatialFilter() throws InterruptedException {
    String bql = "select * from linden where distance(116.7, 35.3) in 10";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter spatialFilter = LindenSpatialFilterBuilder.buildSpatialParam(35.3, 116.7, 10);
    Assert.assertEquals(spatialFilter, lindenRequest.getFilter());
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 2 with LindenFilter

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

the class TestBQL method testRangePred.

@Test
public void testRangePred() throws Exception {
    String bql = "SELECT * FROM linden WHERE id > 1999";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter expectedFilter = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "1999", null, false, false);
    Assert.assertEquals(expectedFilter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden  WHERE id <= 2000";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    expectedFilter = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, null, "2000", false, true);
    Assert.assertEquals(expectedFilter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE id <= $e";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(null, lindenRequest.getFilter());
    bql = "SELECT * FROM linden by id <= 2000";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, null, "2000", false, true);
    Assert.assertEquals(query, lindenRequest.getQuery());
    bql = "SELECT * FROM linden by id <= 2000 and id > 1000 subBoost by 0.5";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query1 = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, null, "2000", false, true);
    LindenQuery query2 = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "1000", null, false, false);
    query2.setBoost(0.5);
    LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
    builder.addQuery(query1, LindenBooleanClause.MUST);
    builder.addQuery(query2, LindenBooleanClause.MUST);
    Assert.assertEquals(builder.build(), lindenRequest.getQuery());
    bql = "SELECT * FROM linden WHERE color > 're''d'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    expectedFilter = LindenRangeFilterBuilder.buildRangeFilter("color", LindenType.FACET, "re'd", null, false, false);
    Assert.assertEquals(expectedFilter, lindenRequest.getFilter());
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 3 with LindenFilter

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

the class TestBQL method testEscapedColumnName.

@Test
public void testEscapedColumnName() {
    String bql = "select * from linden where `select` = \"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("select", "qq"), booleanFilter.getFilters().get(0).getFilter().getTermFilter().getTerm());
    bql = "SELECT * FROM linden WHERE `source` <> 'red'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    booleanFilter = new LindenBooleanFilter();
    LindenFilter termFilter = LindenTermFilterBuilder.buildTermFilter("source", "red");
    booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(termFilter).setClause(LindenBooleanClause.MUST_NOT));
    Assert.assertEquals(booleanFilter, lindenRequest.getFilter().getBooleanFilter());
    bql = "select * from linden where `explain` in (1, 2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenBooleanFilterBuilder builder = new LindenBooleanFilterBuilder();
    builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "1"), LindenBooleanClause.SHOULD);
    builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "2"), LindenBooleanClause.SHOULD);
    builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "3"), LindenBooleanClause.SHOULD);
    LindenFilter expected = builder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE `route` BETWEEN 'black' AND \"yellow\"";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter filter = LindenRangeFilterBuilder.buildRangeFilter("route", LindenType.STRING, "black", "yellow", true, true);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT `delete`, `score` FROM linden where title = 'sed' route by 0, 1, 2";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(2, lindenRequest.getSourceFieldsSize());
    Assert.assertEquals("delete", lindenRequest.getSourceFields().get(0));
    Assert.assertEquals("score", lindenRequest.getSourceFields().get(1));
    bql = "SELECT * FROM linden QUERY `FROM` LIKE 'sed*'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query = new LindenQuery();
    query.setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("FROM"));
    Assert.assertEquals(query, lindenRequest.getQuery());
    bql = "SELECT * FROM linden browse by `group`(5)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFacet facetRequest = new LindenFacet();
    facetRequest.addToFacetParams(new LindenFacetParam().setTopN(5).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("group")));
    Assert.assertEquals(facetRequest, lindenRequest.getFacet());
    bql = "SELECT title, rank FROM linden drill down `group`('big')";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    facetRequest = new LindenFacet();
    facetRequest.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("group").setPath("big"));
    Assert.assertEquals(facetRequest, lindenRequest.getFacet());
}
Also used : LindenFacetDimAndPath(com.xiaomi.linden.thrift.common.LindenFacetDimAndPath) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenWildcardQuery(com.xiaomi.linden.thrift.common.LindenWildcardQuery) LindenBooleanFilter(com.xiaomi.linden.thrift.common.LindenBooleanFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenFacetParam(com.xiaomi.linden.thrift.common.LindenFacetParam) LindenBooleanSubFilter(com.xiaomi.linden.thrift.common.LindenBooleanSubFilter) LindenTerm(com.xiaomi.linden.thrift.common.LindenTerm) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) LindenFacet(com.xiaomi.linden.thrift.common.LindenFacet) Test(org.junit.Test)

Example 4 with LindenFilter

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

the class TestBQL method testLikePredicate.

@Test
public void testLikePredicate() throws Exception {
    String bql = "SELECT * FROM linden WHERE title LIKE 's?d*'";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter filter = new LindenFilter().setQueryFilter(new LindenQueryFilter().setQuery(new LindenQuery().setWildcardQuery(new LindenWildcardQuery().setQuery("s?d*").setField("title"))));
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title NOT LIKE 'bl*'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query = new LindenQuery();
    query.setWildcardQuery(new LindenWildcardQuery().setQuery("bl*").setField("title"));
    filter = new LindenFilter().setQueryFilter(new LindenQueryFilter().setQuery(query));
    LindenBooleanFilter booleanFilter = new LindenBooleanFilter();
    booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter).setClause(LindenBooleanClause.MUST_NOT));
    Assert.assertEquals(booleanFilter, lindenRequest.getFilter().getBooleanFilter());
    bql = "SELECT * FROM linden WHERE title NOT LIKE $title";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(null, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title LIKE $title";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(null, lindenRequest.getFilter());
    bql = "SELECT * FROM linden BY title LIKE 'sed*'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    query = new LindenQuery();
    query.setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("title"));
    Assert.assertEquals(query, lindenRequest.getQuery());
    bql = "SELECT * FROM linden BY title NOT LIKE  'sed*'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
    builder.addQuery(LindenRangeQueryBuilder.buildMatchAllQuery(), LindenBooleanClause.MUST);
    builder.addQuery(new LindenQuery().setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("title")), LindenBooleanClause.MUST_NOT);
    Assert.assertEquals(builder.build(), lindenRequest.getQuery());
    bql = "SELECT * FROM linden BY title LIKE 'sed*' subBoost by 3";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    query = new LindenQuery();
    query.setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("title"));
    query.setBoost(3);
    Assert.assertEquals(query, lindenRequest.getQuery());
}
Also used : LindenQueryFilter(com.xiaomi.linden.thrift.common.LindenQueryFilter) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenWildcardQuery(com.xiaomi.linden.thrift.common.LindenWildcardQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanFilter(com.xiaomi.linden.thrift.common.LindenBooleanFilter) LindenBooleanSubFilter(com.xiaomi.linden.thrift.common.LindenBooleanSubFilter) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 5 with LindenFilter

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

the class TestBQL method testNullPredicate.

@Test
public void testNullPredicate() throws Exception {
    String bql = "SELECT * FROM linden  WHERE id IS NOT NULL";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter filter = LindenNotNullFieldFilterBuilder.buildNotNullFieldFilterBuilder("id", false);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title IS NULL";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filter = LindenNotNullFieldFilterBuilder.buildNotNullFieldFilterBuilder("title", true);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden by title IS NULL";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filter = LindenNotNullFieldFilterBuilder.buildNotNullFieldFilterBuilder("title", true);
    LindenQuery query = LindenQueryBuilder.buildFilteredQuery(LindenQueryBuilder.buildMatchAllQuery(), filter);
    Assert.assertEquals(query, lindenRequest.getQuery());
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Aggregations

LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)22 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)17 LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)10 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)9 Test (org.junit.Test)8 LindenType (com.xiaomi.linden.thrift.common.LindenType)7 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)7 LindenBooleanFilterBuilder (com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder)6 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)6 AbstractMap (java.util.AbstractMap)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)5 LindenWildcardQuery (com.xiaomi.linden.thrift.common.LindenWildcardQuery)3 ArrayList (java.util.ArrayList)3 LindenQueryFilter (com.xiaomi.linden.thrift.common.LindenQueryFilter)2 LindenQueryStringQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenQueryStringQueryBuilder)1 GroupParam (com.xiaomi.linden.thrift.common.GroupParam)1 LindenBooleanClause (com.xiaomi.linden.thrift.common.LindenBooleanClause)1 LindenDeleteRequest (com.xiaomi.linden.thrift.common.LindenDeleteRequest)1