use of com.xiaomi.linden.thrift.common.LindenFacet 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.LindenFacet 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.LindenFacet in project linden by XiaoMi.
the class LindenCoreImpl method search.
public LindenResult search(LindenSearchRequest request) throws IOException {
SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomy = lindenNRTSearcherManager.acquire();
try {
IndexSearcher indexSearcher = searcherAndTaxonomy.searcher;
Filter filter = FilterConstructor.constructFilter(request.getFilter(), config);
Sort sort = SortConstructor.constructSort(request, indexSearcher, config);
indexSearcher.setSimilarity(config.getSearchSimilarityInstance());
Query query = QueryConstructor.constructQuery(request.getQuery(), config);
if (filter != null) {
query = new FilteredQuery(query, filter);
}
int from = request.getOffset();
int size = request.getLength();
LindenResultParser resultParser = new LindenResultParser(config, request, indexSearcher, snippetGenerator, query, filter, sort);
// very common search, no group, no facet, no early termination, no search time limit
if (!request.isSetGroupParam() && !request.isSetFacet() && !request.isSetEarlyParam() && config.getSearchTimeLimit() <= 0) {
TopDocs docs;
if (sort != null) {
docs = indexSearcher.search(query, from + size, sort);
} else {
docs = indexSearcher.search(query, from + size);
}
return resultParser.parse(docs, null, null, null);
}
// group param will suppress facet, group, early termination and search time limit parameters
if (request.isSetGroupParam()) {
String groupField = request.getGroupParam().getGroupField();
GroupingSearch groupingSearch = new GroupingSearch(groupField);
groupingSearch.setGroupDocsLimit(request.getGroupParam().getGroupInnerLimit());
if (sort != null) {
groupingSearch.setGroupSort(sort);
groupingSearch.setSortWithinGroup(sort);
groupingSearch.setFillSortFields(true);
}
groupingSearch.setCachingInMB(8.0, true);
groupingSearch.setAllGroups(true);
TopGroups<TopDocs> topGroupedDocs = groupingSearch.search(indexSearcher, query, 0, from + size);
return resultParser.parse(null, topGroupedDocs, null, null);
}
TopDocsCollector topDocsCollector;
if (sort != null) {
topDocsCollector = TopFieldCollector.create(sort, from + size, null, true, false, false, false);
} else {
topDocsCollector = TopScoreDocCollector.create(from + size, false);
}
LindenDocsCollector lindenDocsCollector;
if (request.isSetEarlyParam()) {
MergePolicy mergePolicy = indexWriter.getConfig().getMergePolicy();
Sort mergePolicySort = null;
if (mergePolicy instanceof SortingMergePolicyDecorator) {
mergePolicySort = ((SortingMergePolicyDecorator) mergePolicy).getSort();
}
EarlyTerminationCollector earlyTerminationCollector = new EarlyTerminationCollector(topDocsCollector, mergePolicySort, request.getEarlyParam().getMaxNum());
lindenDocsCollector = new LindenDocsCollector(earlyTerminationCollector);
} else {
lindenDocsCollector = new LindenDocsCollector(topDocsCollector);
}
Collector collector = lindenDocsCollector;
if (config.getSearchTimeLimit() > 0) {
collector = new TimeLimitingCollector(lindenDocsCollector, TimeLimitingCollector.getGlobalCounter(), config.getSearchTimeLimit());
}
// no facet param
if (!request.isSetFacet()) {
indexSearcher.search(query, collector);
return resultParser.parse(lindenDocsCollector.topDocs(), null, null, null);
}
// facet search
LindenFacet facetRequest = request.getFacet();
FacetsCollector facetsCollector = new FacetsCollector();
lindenDocsCollector.wrap(facetsCollector);
Facets facets = null;
if (facetRequest.isSetDrillDownDimAndPaths()) {
// drillDown or drillSideways
DrillDownQuery drillDownQuery = new DrillDownQuery(facetsConfig, query);
List<LindenFacetDimAndPath> drillDownDimAndPaths = facetRequest.getDrillDownDimAndPaths();
for (int i = 0; i < drillDownDimAndPaths.size(); ++i) {
String fieldName = drillDownDimAndPaths.get(i).dim;
if (drillDownDimAndPaths.get(i).path != null) {
drillDownQuery.add(fieldName, drillDownDimAndPaths.get(i).path.split("/"));
} else {
drillDownQuery.add(fieldName);
}
}
// drillSideways
if (facetRequest.getFacetDrillingType() == FacetDrillingType.DRILLSIDEWAYS) {
DrillSideways dillSideways = new DrillSideways(indexSearcher, facetsConfig, searcherAndTaxonomy.taxonomyReader);
DrillSideways.DrillSidewaysResult drillSidewaysResult = dillSideways.search(drillDownQuery, collector);
facets = drillSidewaysResult.facets;
} else {
// drillDown
indexSearcher.search(drillDownQuery, collector);
facets = new FastTaxonomyFacetCounts(searcherAndTaxonomy.taxonomyReader, facetsConfig, facetsCollector);
}
} else {
indexSearcher.search(query, collector);
// Simple facet browsing
if (facetRequest.isSetFacetParams()) {
facets = new FastTaxonomyFacetCounts(searcherAndTaxonomy.taxonomyReader, facetsConfig, facetsCollector);
}
}
return resultParser.parse(lindenDocsCollector.topDocs(), null, facets, facetsCollector);
} catch (Exception e) {
throw new IOException(Throwables.getStackTraceAsString(e));
} finally {
lindenNRTSearcherManager.release(searcherAndTaxonomy);
}
}
use of com.xiaomi.linden.thrift.common.LindenFacet 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