use of com.xiaomi.linden.thrift.common.LindenFacetParam in project linden by XiaoMi.
the class TestBQL method testFacetDrilling.
@Test
public void testFacetDrilling() {
String bql = "SELECT title, rank FROM linden drill down 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.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("color"));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
bql = "SELECT * FROM linden browse by color(5) drill sideways shape('rectangle')";
lindenRequest = compiler.compile(bql).getSearchRequest();
facetRequest = new LindenFacet();
facetRequest.setFacetDrillingType(FacetDrillingType.DRILLSIDEWAYS);
facetRequest.addToFacetParams(new LindenFacetParam().setTopN(5).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("color")));
facetRequest.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("shape").setPath("rectangle"));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
bql = "SELECT * FROM linden drill down color(\"red\")";
lindenRequest = compiler.compile(bql).getSearchRequest();
facetRequest = new LindenFacet();
facetRequest.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("color").setPath("red"));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
bql = "SELECT * FROM linden drill sideways color('light/gray/white') ";
lindenRequest = compiler.compile(bql).getSearchRequest();
facetRequest = new LindenFacet();
facetRequest.setFacetDrillingType(FacetDrillingType.DRILLSIDEWAYS);
facetRequest.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("color").setPath("light/gray/white"));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
bql = "SELECT * FROM linden browse by color(6, 'light/gray/white') drill down color('red'), shape('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.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("color").setPath("red"));
facetRequest.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("shape").setPath("rectangle"));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
}
use of com.xiaomi.linden.thrift.common.LindenFacetParam 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());
}
use of com.xiaomi.linden.thrift.common.LindenFacetParam in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitFacet_spec.
@Override
public void exitFacet_spec(BQLParser.Facet_specContext ctx) {
String col = unescapeColumnName(ctx.column_name());
Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
LindenType type = fieldNameAndType.getValue();
if (type != LindenType.FACET) {
throw new ParseCancellationException(new SemanticException(ctx.column_name(), "Non-facet type column \"" + col + "\" can not be used in browse predicates."));
}
col = fieldNameAndType.getKey();
LindenFacetParam facetParam = new LindenFacetParam();
LindenFacetDimAndPath facetDimAndPath = new LindenFacetDimAndPath();
facetDimAndPath.setDim(col);
if (ctx.n1 != null) {
facetParam.setTopN(Integer.parseInt(ctx.n1.getText()));
}
if (ctx.path != null) {
String path = unescapeStringLiteral(ctx.STRING_LITERAL());
facetDimAndPath.setPath(path);
}
facetParam.setFacetDimAndPath(facetDimAndPath);
facetRequest.addToFacetParams(facetParam);
}
use of com.xiaomi.linden.thrift.common.LindenFacetParam in project linden by XiaoMi.
the class LindenResultParser method parseFacets.
private void parseFacets(LindenResult result, Facets facets, FacetsCollector facetsCollector) throws IOException {
// Set facets
if (request.isSetFacet()) {
if (request.getFacet().isSetFacetParams() && facets != null) {
List<LindenFacetParam> facetParams = request.getFacet().getFacetParams();
for (LindenFacetParam facetParam : facetParams) {
LindenFacetResult lindenFacetResult = new LindenFacetResult();
lindenFacetResult.setDim(facetParam.facetDimAndPath.dim);
lindenFacetResult.setPath(facetParam.facetDimAndPath.path);
FacetResult facetResult;
if (facetParam.facetDimAndPath.path != null) {
facetResult = facets.getTopChildren(facetParam.topN, facetParam.facetDimAndPath.dim, facetParam.facetDimAndPath.path.split("/"));
} else {
facetResult = facets.getTopChildren(facetParam.topN, facetParam.facetDimAndPath.dim);
}
if (facetResult != null) {
lindenFacetResult.setValue(facetResult.value.intValue());
lindenFacetResult.setChildCount(facetResult.childCount);
int sumValue = 0;
for (int j = 0; j < facetResult.labelValues.length; ++j) {
LindenLabelAndValue labelAndValue = new LindenLabelAndValue();
labelAndValue.setLabel(facetResult.labelValues[j].label);
int value = facetResult.labelValues[j].value.intValue();
labelAndValue.setValue(value);
sumValue += value;
lindenFacetResult.addToLabelValues(labelAndValue);
}
if (sumValue > lindenFacetResult.getValue() || facetResult.labelValues.length < facetParam.topN) {
lindenFacetResult.setValue(sumValue);
}
}
result.addToFacetResults(lindenFacetResult);
}
} else if (request.getFacet().isSetAggregations() && facetsCollector != null) {
List<Aggregation> aggregations = request.getFacet().getAggregations();
for (int i = 0; i < aggregations.size(); ++i) {
Aggregation aggregation = aggregations.get(i);
String fieldName = aggregation.getField();
LindenType type = aggregation.getType();
AggregationResult aggregationResult = new AggregationResult();
aggregationResult.setField(fieldName);
Facets aggFacets;
if (type == LindenType.INTEGER || type == LindenType.LONG) {
LongRange[] ranges = new LongRange[aggregation.getBucketsSize()];
for (int j = 0; j < ranges.length; ++j) {
Bucket bucket = aggregation.getBuckets().get(j);
String label = generateBucketLabel(bucket);
long minValue = bucket.getStartValue().equals("*") ? Long.MIN_VALUE : Long.parseLong(bucket.getStartValue());
long maxValue = bucket.getEndValue().equals("*") ? Long.MAX_VALUE : Long.parseLong(bucket.getEndValue());
ranges[j] = new LongRange(label, minValue, bucket.isStartClosed(), maxValue, bucket.isEndClosed());
}
aggFacets = new LongRangeFacetCounts(fieldName, facetsCollector, ranges);
} else if (type == LindenType.DOUBLE) {
DoubleRange[] ranges = new DoubleRange[aggregation.getBucketsSize()];
for (int j = 0; j < ranges.length; ++j) {
Bucket bucket = aggregation.getBuckets().get(j);
String label = generateBucketLabel(bucket);
double minValue = bucket.getStartValue().equals("*") ? -Double.MAX_VALUE : Double.parseDouble(bucket.getStartValue());
double maxValue = bucket.getEndValue().equals("*") ? Double.MAX_VALUE : Double.parseDouble(bucket.getEndValue());
ranges[j] = new DoubleRange(label, minValue, bucket.isStartClosed(), maxValue, bucket.isEndClosed());
}
aggFacets = new DoubleRangeFacetCounts(fieldName, facetsCollector, ranges);
} else {
throw new IOException(type + " type is not supported in aggregation");
}
FacetResult facetResult = aggFacets.getTopChildren(aggregation.getBucketsSize(), fieldName);
for (int j = 0; j < facetResult.labelValues.length; ++j) {
LindenLabelAndValue labelAndValue = new LindenLabelAndValue();
labelAndValue.setLabel(facetResult.labelValues[j].label);
labelAndValue.setValue(facetResult.labelValues[j].value.intValue());
aggregationResult.addToLabelValues(labelAndValue);
}
result.addToAggregationResults(aggregationResult);
}
}
}
}
use of com.xiaomi.linden.thrift.common.LindenFacetParam 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());
}
Aggregations