Search in sources :

Example 1 with LindenFacet

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());
}
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 2 with LindenFacet

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());
}
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 3 with LindenFacet

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);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FastTaxonomyFacetCounts(org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts) Query(org.apache.lucene.search.Query) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) FilteredQuery(org.apache.lucene.search.FilteredQuery) TermQuery(org.apache.lucene.search.TermQuery) Facets(org.apache.lucene.facet.Facets) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) TimeLimitingCollector(org.apache.lucene.search.TimeLimitingCollector) FilteredQuery(org.apache.lucene.search.FilteredQuery) SearcherTaxonomyManager(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager) LindenDocsCollector(com.xiaomi.linden.lucene.collector.LindenDocsCollector) TopDocs(org.apache.lucene.search.TopDocs) SortingMergePolicyDecorator(com.xiaomi.linden.lucene.merge.SortingMergePolicyDecorator) MergePolicy(org.apache.lucene.index.MergePolicy) EarlyTerminationCollector(com.xiaomi.linden.lucene.collector.EarlyTerminationCollector) TopFieldCollector(org.apache.lucene.search.TopFieldCollector) TimeLimitingCollector(org.apache.lucene.search.TimeLimitingCollector) LindenDocsCollector(com.xiaomi.linden.lucene.collector.LindenDocsCollector) FacetsCollector(org.apache.lucene.facet.FacetsCollector) Collector(org.apache.lucene.search.Collector) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) EarlyTerminationCollector(com.xiaomi.linden.lucene.collector.EarlyTerminationCollector) TopDocsCollector(org.apache.lucene.search.TopDocsCollector) Sort(org.apache.lucene.search.Sort) TopDocsCollector(org.apache.lucene.search.TopDocsCollector) IOException(java.io.IOException) IOException(java.io.IOException) LindenFacet(com.xiaomi.linden.thrift.common.LindenFacet) FacetsCollector(org.apache.lucene.facet.FacetsCollector) LindenFacetDimAndPath(com.xiaomi.linden.thrift.common.LindenFacetDimAndPath) Filter(org.apache.lucene.search.Filter) GroupingSearch(org.apache.lucene.search.grouping.GroupingSearch) DrillSideways(org.apache.lucene.facet.DrillSideways)

Example 4 with LindenFacet

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());
}
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)

Aggregations

LindenFacet (com.xiaomi.linden.thrift.common.LindenFacet)4 LindenFacetDimAndPath (com.xiaomi.linden.thrift.common.LindenFacetDimAndPath)4 LindenFacetParam (com.xiaomi.linden.thrift.common.LindenFacetParam)3 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)3 Test (org.junit.Test)3 EarlyTerminationCollector (com.xiaomi.linden.lucene.collector.EarlyTerminationCollector)1 LindenDocsCollector (com.xiaomi.linden.lucene.collector.LindenDocsCollector)1 SortingMergePolicyDecorator (com.xiaomi.linden.lucene.merge.SortingMergePolicyDecorator)1 LindenBooleanFilterBuilder (com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder)1 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)1 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)1 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)1 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)1 LindenTerm (com.xiaomi.linden.thrift.common.LindenTerm)1 LindenWildcardQuery (com.xiaomi.linden.thrift.common.LindenWildcardQuery)1 IOException (java.io.IOException)1 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)1 DrillSideways (org.apache.lucene.facet.DrillSideways)1 Facets (org.apache.lucene.facet.Facets)1 FacetsCollector (org.apache.lucene.facet.FacetsCollector)1