Search in sources :

Example 1 with SortingMergePolicyDecorator

use of com.xiaomi.linden.lucene.merge.SortingMergePolicyDecorator 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)

Aggregations

EarlyTerminationCollector (com.xiaomi.linden.lucene.collector.EarlyTerminationCollector)1 LindenDocsCollector (com.xiaomi.linden.lucene.collector.LindenDocsCollector)1 SortingMergePolicyDecorator (com.xiaomi.linden.lucene.merge.SortingMergePolicyDecorator)1 LindenFacet (com.xiaomi.linden.thrift.common.LindenFacet)1 LindenFacetDimAndPath (com.xiaomi.linden.thrift.common.LindenFacetDimAndPath)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 FastTaxonomyFacetCounts (org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts)1 SearcherTaxonomyManager (org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager)1 MergePolicy (org.apache.lucene.index.MergePolicy)1 Collector (org.apache.lucene.search.Collector)1 Filter (org.apache.lucene.search.Filter)1 FilteredQuery (org.apache.lucene.search.FilteredQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1 Sort (org.apache.lucene.search.Sort)1 TermQuery (org.apache.lucene.search.TermQuery)1