Exemplo n.º 1
0
        public virtual void Browse(
            BrowseRequest req,
            Weight weight,
            Collector collector,
            IDictionary<string, IFacetAccessible> facetMap,
            int start)
        {

            if (_reader == null)
                return;

    
            //      initialize all RuntimeFacetHandlers with data supplied by user at run-time.
            _runtimeFacetHandlers = new List<IRuntimeFacetHandler>(_runtimeFacetHandlerFactoryMap.Count());

            IEnumerable<string> runtimeFacetNames = _runtimeFacetHandlerFactoryMap.Keys;
            foreach (string facetName in runtimeFacetNames)
            {
                var sfacetHandler = this.GetFacetHandler(facetName);
                if (sfacetHandler!=null)
                {
                    logger.Warn("attempting to reset facetHandler: " + sfacetHandler);
                    continue;
                }
                IRuntimeFacetHandlerFactory factory = (IRuntimeFacetHandlerFactory)_runtimeFacetHandlerFactoryMap.Get(facetName);
      
                try
                {

                    FacetHandlerInitializerParam data = req.GetFacetHandlerData(facetName);
                    if (data == null)
                        data = FacetHandlerInitializerParam.EMPTY_PARAM;
                    if (data != FacetHandlerInitializerParam.EMPTY_PARAM || !factory.IsLoadLazily)
                    {
                        IRuntimeFacetHandler facetHandler = factory.Get(data);
                        if (facetHandler != null)
                        {
                            _runtimeFacetHandlers.Add(facetHandler); // add to a list so we close them after search
                            this.SetFacetHandler(facetHandler);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new BrowseException("error trying to set FacetHandler : " + facetName + ":" + e.Message, e);
                }
            }
            // done initialize all RuntimeFacetHandlers with data supplied by user at run-time.

            IEnumerable<string> fields = FacetNames;

            List<Filter> preFilterList = new List<Filter>();
            List<FacetHitCollector> facetHitCollectorList = new List<FacetHitCollector>();
    
            Filter baseFilter = req.Filter;
            if (baseFilter != null)
            {
                preFilterList.Add(baseFilter);
            }

            int selCount = req.SelectionCount;
            bool isNoQueryNoFilter = IsNoQueryNoFilter(req);

            bool isDefaultSearch = isNoQueryNoFilter && selCount == 0;
            try
            {
      
                foreach (string name in fields)
                {
                    BrowseSelection sel = req.GetSelection(name);
                    FacetSpec ospec = req.GetFacetSpec(name);

                    var handler = GetFacetHandler(name);
        
                    if (handler == null)
                    {
        	            logger.Error("facet handler: " + name + " is not defined, ignored.");
        	            continue;
                    }
        
                    FacetHitCollector facetHitCollector = null;

                    RandomAccessFilter filter = null;
                    if (sel != null)
                    {
                        filter = handler.BuildFilter(sel);
                    }

                    if (ospec == null)
                    {
                        if (filter != null)
                        {
                            preFilterList.Add(filter);
                        }
                    }
                    else
                    {
                        /*FacetSpec fspec = new FacetSpec(); // OrderValueAsc,
                        fspec.setMaxCount(0);
                        fspec.setMinHitCount(1);
          
                        fspec.setExpandSelection(ospec.isExpandSelection());*/
                        FacetSpec fspec = ospec;

                        facetHitCollector = new FacetHitCollector();
                        facetHitCollector.facetHandler = handler;
          
                        if (isDefaultSearch)
                        {
        	                facetHitCollector._collectAllSource = handler.GetFacetCountCollectorSource(sel, fspec);
                        }
                        else
                        {
                            facetHitCollector._facetCountCollectorSource = handler.GetFacetCountCollectorSource(sel, fspec);            
                            if (ospec.ExpandSelection)
                            {
                                if (isNoQueryNoFilter && sel != null && selCount == 1)
                                {
            	                    facetHitCollector._collectAllSource = handler.GetFacetCountCollectorSource(sel, fspec);
                                    if (filter != null)
                                    {
                                        preFilterList.Add(filter);
                                    }
                                }
                                else
                                {
                                    if (filter != null)
                                    {
                	                    facetHitCollector._filter = filter;
                                    }
                                }
                            }
                            else
                            {
                                if (filter != null)
                                {
                                    preFilterList.Add(filter);
                                }
                            }
                        }
                    }
                    if (facetHitCollector != null)
                    {
                        facetHitCollectorList.Add(facetHitCollector);
                    }
                }

                Filter finalFilter = null;
                if (preFilterList.Count > 0)
                {
                    if (preFilterList.Count == 1)
                    {
                        finalFilter = preFilterList.First();
                    }
                    else
                    {
                        finalFilter = new AndFilter(preFilterList);
                    }
                }

                this.FacetHitCollectorList = facetHitCollectorList;

                try
                {
                    if (weight == null)
                    {
                        var q = req.Query;
                        if (q == null)
                        {
                            q = new MatchAllDocsQuery();
                        }
                        weight = CreateWeight(q);
                    }
                    Search(weight, finalFilter, collector, start, req.MapReduceWrapper);
                }
                finally
                {
                    foreach (FacetHitCollector facetCollector in facetHitCollectorList)
                    {
                        string name = facetCollector.facetHandler.Name;
                        List<IFacetCountCollector> resultcollector = null;
                        resultcollector = facetCollector._countCollectorList;
                        if (resultcollector == null || resultcollector.Count == 0)
                        {
        	                resultcollector = facetCollector._collectAllCollectorList;
                        }
                        if (resultcollector != null)
                        {
        	                FacetSpec fspec = req.GetFacetSpec(name);
        	                Debug.Assert(fspec != null);
                            if(resultcollector.Count == 1)
                            {
                                facetMap.Put(name, resultcollector[0]);             
                            }
                            else
                            {
                                List<IFacetAccessible> finalList = new List<IFacetAccessible>(resultcollector.Count);
                                foreach (IFacetCountCollector fc in resultcollector)
                                {
                                    finalList.Add((IFacetAccessible)fc);
                                }
        	                    CombinedFacetAccessible combinedCollector = new CombinedFacetAccessible(fspec, finalList);
                                facetMap.Put(name, combinedCollector);
        	                }
                        }
                    }
                }
            }
            catch (Exception ioe)
            {
                throw new BrowseException(ioe.Message, ioe);
            }
        }
Exemplo n.º 2
0
        ///<summary>browses the index.</summary>
        ///<param name="req">browse request </param>
        ///<param name="collector">collector for the hits </param>
        ///<param name="facetMap">map to gather facet data </param>
        public virtual void Browse(BrowseRequest req, Collector collector, Dictionary<string, IFacetAccessible> facetMap) //  throws BrowseException
        {
            if (reader == null)
                return;

            IEnumerable<string> fields = GetFacetNames();

            LinkedList<Filter> preFilterList = new LinkedList<Filter>();
            List<FacetHitCollector> facetHitCollectorList = new List<FacetHitCollector>();
            List<IFacetCountCollector> countAllCollectorList = new List<IFacetCountCollector>();

            Filter baseFilter = req.Filter;
            if (baseFilter != null)
            {
                preFilterList.AddLast(baseFilter);
            }

            int selCount = req.SelectionCount;
            bool isNoQueryNoFilter = IsNoQueryNoFilter(req);

            bool isDefaultSearch = isNoQueryNoFilter && selCount == 0;
            try
            {

                foreach (string name in fields)
                {
                    FacetSpec ospec = req.GetFacetSpec(name);

                    FacetHandler handler = GetFacetHandler(name);

                    if (handler == null)
                    {
                        logger.Warn("facet handler: " + name + " is not defined, ignored.");
                        continue;
                    }

                    FacetHitCollector facetHitCollector = null;

                    RandomAccessFilter filter = null;
                    BrowseSelection sel = req.GetSelection(name);
                    if (sel != null)
                    {
                        filter = handler.BuildFilter(sel);
                    }

                    if (ospec == null)
                    {
                        if (filter != null)
                        {
                            preFilterList.AddLast(filter);
                        }
                    }
                    else
                    {
                        if (isDefaultSearch)
                        {
                            countAllCollectorList.Add(handler.GetFacetCountCollector(sel, ospec));
                        }
                        else
                        {
                            facetHitCollector = new FacetHitCollector();
                            facetHitCollector.FacetCountCollector = handler.GetFacetCountCollector(sel, ospec);
                            facetHitCollector.FacetHandler = handler;
                            if (ospec.ExpandSelection)
                            {
                                if (isNoQueryNoFilter && sel != null && selCount == 1)
                                {
                                    facetHitCollector = null; // don't post collect
                                    countAllCollectorList.Add(handler.GetFacetCountCollector(sel, ospec));
                                    if (filter != null)
                                    {
                                        preFilterList.AddLast(filter);
                                    }
                                }
                                else
                                {
                                    if (filter != null)
                                    {
                                        RandomAccessDocIdSet docset = filter.GetRandomAccessDocIdSet(reader);
                                        facetHitCollector.PostDocIDSetIterator = docset.Iterator();
                                        facetHitCollector.DocIdSet = docset;
                                    }
                                }
                            }
                            else
                            {
                                if (filter != null)
                                {
                                    preFilterList.AddLast(filter);
                                }
                            }
                        }
                    }
                    if (facetHitCollector != null)
                    {
                        facetHitCollectorList.Add(facetHitCollector);
                    }
                }

                Filter finalFilter = null;
                if (preFilterList.Count > 0)
                {
                    if (preFilterList.Count == 1)
                    {
                        finalFilter = preFilterList.First.Value;
                    }
                    else
                    {
                        finalFilter = new AndFilter(preFilterList);
                    }
                }

                SetFacetHitCollectorList(facetHitCollectorList);

                Lucene.Net.Search.Query q = req.Query;
                if (q == null || q is MatchAllDocsQuery)
                {
                    q = reader.GetFastMatchAllDocsQuery();
                }

                try
                {
                    Search(q, finalFilter, collector);
                }
                finally
                {
                    foreach (FacetHitCollector facetCollector in facetHitCollectorList)
                    {
                        string name = facetCollector.FacetCountCollector.Name;
                        facetMap.Add(name, facetCollector.FacetCountCollector);
                    }
                    foreach (IFacetCountCollector facetCountCollector in countAllCollectorList)
                    {
                        facetCountCollector.CollectAll();
                        facetMap.Add(facetCountCollector.Name, facetCountCollector);
                    }
                }
            }
            catch (System.IO.IOException ioe)
            {
                throw new BrowseException(ioe.Message, ioe);
            }
        }