Exemplo n.º 1
0
            ///<summary>This method validates the doc against any multi-select enabled fields. </summary>
            ///<param name="docid"> </param>
            ///<returns>true if all fields matched </returns>
            public override sealed bool Validate(int docid)
            {
                FacetHitCollector.CurrentPointers miss = null;

                for (int i = 0; i < _numPostFilters; i++)
                {
                    FacetHitCollector.CurrentPointers cur = _collectors[i]._currentPointers;
                    int sid = cur.Doc;

                    if (sid < docid)
                    {
                        sid     = cur.PostDocIDSetIterator.Advance(docid);
                        cur.Doc = sid;
                        if (sid == DocIdSetIterator.NO_MORE_DOCS)
                        {
                            // move this to front so that the call can find the failure faster
                            FacetHitCollector tmp = _collectors[0];
                            _collectors[0] = _collectors[i];
                            _collectors[i] = tmp;
                        }
                    }

                    if (sid > docid) //mismatch
                    {
                        if (miss != null)
                        {
                            // failed because we already have a mismatch
                            _nextTarget = (miss.Doc < cur.Doc ? miss.Doc : cur.Doc);
                            return(false);
                        }
                        miss = cur;
                    }
                }

                _nextTarget = docid + 1;

                if (miss != null)
                {
                    miss.FacetCountCollector.Collect(docid);
                    return(false);
                }
                else
                {
                    foreach (IFacetCountCollector collector in _countCollectors)
                    {
                        collector.Collect(docid);
                    }
                    return(true);
                }
            }
Exemplo n.º 2
0
        protected virtual FacetValidator CreateFacetValidator()
        {
            FacetHitCollector[]         collectors      = new FacetHitCollector[_facetCollectors.Count()];
            FacetCountCollectorSource[] countCollectors = new FacetCountCollectorSource[collectors.Length];
            int numPostFilters;
            int i = 0;
            int j = collectors.Length;

            foreach (FacetHitCollector facetCollector in _facetCollectors)
            {
                if (facetCollector._filter != null)
                {
                    collectors[i]      = facetCollector;
                    countCollectors[i] = facetCollector._facetCountCollectorSource;
                    i++;
                }
                else
                {
                    j--;
                    collectors[j]      = facetCollector;
                    countCollectors[j] = facetCollector._facetCountCollectorSource;
                }
            }
            numPostFilters = i;

            if (numPostFilters == 0)
            {
                return(new NoNeedFacetValidator(collectors));
            }
            else if (numPostFilters == 1)
            {
                return(new OnePostFilterFacetValidator(collectors));
            }
            else
            {
                return(new DefaultFacetValidator(collectors, numPostFilters));
            }
        }
        private int DoTestSingle()
        {
            // TODO: Lucene.Net 3.0.3 doesn't allow null to be passed for the BoboIndexReader
            // so an alternative (possibly a mocked index reader) needs to be passed in.

            var bbs2 = new BoboSearcher2(null);

            var facetHitCollectors = new List<FacetHitCollector>();
            var o = new FacetHitCollector();
            o.PostDocIDSetIterator = new MyIterator();
            var radis = new MyRandomAccessDocIdSet();
            o.DocIdSet = radis;
            o.FacetCountCollector = new MyFacetCountCollector();

            facetHitCollectors.Add(o);
            bbs2.SetFacetHitCollectorList(facetHitCollectors);
            var weight = new MyWeight();
            var results = TopScoreDocCollector.Create(100, true);
            bbs2.Search(weight, null, results);
            return results.TotalHits;
        }
Exemplo n.º 4
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.º 5
0
 public OnePostFilterFacetValidator(FacetHitCollector[] collectors)
     : base(collectors, 1)
 {
     _firsttime = _collectors[0];
 }
Exemplo n.º 6
0
 protected FacetValidator(FacetHitCollector[] collectors, IFacetCountCollector[] countCollectors, int numPostFilters)
 {
     Collectors = collectors;
     CountCollectors = countCollectors;
     NumPostFilters = numPostFilters;
 }
Exemplo n.º 7
0
 public DefaultFacetValidator(FacetHitCollector[] collectors, IFacetCountCollector[] countCollectors, int numPostFilters)
     : base(collectors, countCollectors, numPostFilters)
 {
 }
Exemplo n.º 8
0
        protected FacetValidator CreateFacetValidator()
        {
            FacetHitCollector[] collectors = new FacetHitCollector[facetCollectors.Count];
            IFacetCountCollector[] countCollectors = new IFacetCountCollector[collectors.Length];
            int numPostFilters;
            int i = 0;
            int j = collectors.Length;

            foreach (FacetHitCollector facetCollector in facetCollectors)
            {
                if (facetCollector.PostDocIDSetIterator != null)
                {
                    facetCollector.More = facetCollector.PostDocIDSetIterator.NextDoc()!=DocIdSetIterator.NO_MORE_DOCS;
                    facetCollector.Doc = (facetCollector.More ? facetCollector.PostDocIDSetIterator.DocID() : int.MaxValue);
                    collectors[i] = facetCollector;
                    countCollectors[i] = facetCollector.FacetCountCollector;
                    i++;
                }
                else
                {
                    j--;
                    collectors[j] = facetCollector;
                    countCollectors[j] = facetCollector.FacetCountCollector;
                }
            }
            numPostFilters = i;

            if (numPostFilters == 0)
            {
                return new NoNeedFacetValidator(collectors, countCollectors, numPostFilters);
            }
            else if (numPostFilters == 1)
            {
                return new OnePostFilterFacetValidator(collectors, countCollectors, numPostFilters);
            }
            else
            {
                return new DefaultFacetValidator(collectors, countCollectors, numPostFilters);
            }
        }
Exemplo n.º 9
0
 internal NoNeedFacetValidator(FacetHitCollector[] collectors, IFacetCountCollector[] countCollectors, int numPostFilters)
     : base(collectors, countCollectors, numPostFilters)
 {
 }
Exemplo n.º 10
0
 internal OnePostFilterFacetValidator(FacetHitCollector[] collectors, IFacetCountCollector[] countCollectors, int numPostFilters)
     : base(collectors, countCollectors, numPostFilters)
 {
     firsttime = Collectors[0];
 }
Exemplo n.º 11
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);
            }
        }
Exemplo n.º 12
0
 ///    
 ///	 <summary> * This method validates the doc against any multi-select enabled fields. </summary>
 ///	 * <param name="docid"> </param>
 ///	 * <returns> false if not all a match on all fields. Facet count may still be collected. </returns>
 ///	 
 private static bool validateAndIncrement(int docid, FacetHitCollector[] facetCollectors, bool doValidate)
 {
     if (doValidate)
     {
         int misses = 0;
         int _marker = -1;
         for (int i = 0; i < facetCollectors.Length; ++i)
         {
             FacetHitCollector facetCollector = facetCollectors[i];
             if (facetCollector.PostDocIDSetIterator == null)
             {
                 continue;
             }
             if (facetCollector.PostDocIDSetIterator.Advance(docid)!=DocIdSetIterator.NO_MORE_DOCS)
             {
                 if (facetCollector.PostDocIDSetIterator.DocID() != docid)
                 {
                     misses++;
                     if (misses > 1)
                     {
                         return false;
                     }
                     else
                     {
                         _marker = i;
                     }
                 }
             }
             else
             {
                 misses++;
                 if (misses > 1)
                 {
                     return false;
                 }
                 else
                 {
                     _marker = i;
                 }
             }
         }
         if (misses == 1)
         {
             for (int i = 0; i < facetCollectors.Length; ++i)
             {
                 FacetHitCollector facetCollector = facetCollectors[i];
                 if (_marker == i)
                 {
                     facetCollector.FacetCountCollector.Collect(docid);
                 }
             }
         }
         else
         {
             foreach (FacetHitCollector facetCollector in facetCollectors)
             {
                 facetCollector.FacetCountCollector.Collect(docid);
             }
         }
         return misses == 0;
     }
     else
     {
         foreach (FacetHitCollector facetCollector in facetCollectors)
         {
             facetCollector.FacetCountCollector.Collect(docid);
         }
         return true;
     }
 }