public void TestMultiValPath()
        {
            IndexReader reader = IndexReader.Open(directory, true);
            BoboIndexReader boboReader = BoboIndexReader.GetInstance(reader, facetHandlers);

            BoboBrowser browser = new BoboBrowser(boboReader);
            BrowseRequest req = new BrowseRequest();

            BrowseSelection sel = new BrowseSelection(PathHandlerName);
            sel.AddValue("/a");
            var propMap = new Dictionary<String, String>();
            propMap.Put(PathFacetHandler.SEL_PROP_NAME_DEPTH, "0");
            propMap.Put(PathFacetHandler.SEL_PROP_NAME_STRICT, "false");
            sel.SetSelectionProperties(propMap);

            req.AddSelection(sel);

            FacetSpec fs = new FacetSpec();
            fs.MinHitCount = (1);
            req.SetFacetSpec(PathHandlerName, fs);

            BrowseResult res = browser.Browse(req);
            Assert.AreEqual(res.NumHits, 1);
            IFacetAccessible fa = res.GetFacetAccessor(PathHandlerName);
            IEnumerable<BrowseFacet> facets = fa.GetFacets();
            Console.WriteLine(facets);
            Assert.AreEqual(1, facets.Count());
            BrowseFacet facet = facets.Get(0);
            Assert.AreEqual(2, facet.FacetValueHitCount);
        }
Example #2
0
 public virtual BrowseResult Browse(BrowseRequest req)
 {
     BoboBrowser browser = null;
     try
     {
         browser = new BoboBrowser(_boboReader);
         return browser.Browse(req);
     }
     catch (Exception e)
     {
         logger.Error(e.Message, e);
         return new BrowseResult();
     }
     finally
     {
         if (browser != null)
         {
             try
             {
                 browser.Dispose();
             }
             catch (Exception e)
             {
                 logger.Error(e.Message);
             }
         }
     }
 }
Example #3
0
        public BrowseResult Browse(BrowseRequest browseRequest)
        {
            string indexDir = HostingEnvironment.MapPath(ConfigurationManager.AppSettings["LuceneIndexDirectory"]);

            // This is the equivalent code to what is specified in the "/LuceneIndex/bobo.spring" file. You could just as well
            // specify this configuration here and pass it to another overload of BoboIndexReader.GetInstance that accepts 
            // an IEnumerable<IFacetHandler> as an argument.

            //var facetHandlers = new List<IFacetHandler>();
            //facetHandlers.Add(new SimpleFacetHandler("color") { TermCountSize = BoboBrowse.Net.Facets.TermCountSize.Small });
            //facetHandlers.Add(new SimpleFacetHandler("category") { TermCountSize = BoboBrowse.Net.Facets.TermCountSize.Medium });
            //facetHandlers.Add(new PathFacetHandler("city") { Separator = "/" });
            //facetHandlers.Add(new PathFacetHandler("makemodel") { Separator = "/" });
            //facetHandlers.Add(new RangeFacetHandler("year", new PredefinedTermListFactory<int>("00000000000000000000"), new string[] { "[1993 TO 1994]", "[1995 TO 1996]", "[1997 TO 1998]", "[1999 TO 2000]", "[2001 TO 2002]" }));
            //facetHandlers.Add(new RangeFacetHandler("price", new PredefinedTermListFactory<float>("00000000000000000000"), new string[] { "[2001 TO 6700]", "[6800 TO 9900]", "[10000 TO 13100]", "[13200 TO 17300]", "[17400 TO 19500]" }));
            //facetHandlers.Add(new RangeFacetHandler("mileage", new PredefinedTermListFactory<int>("00000000000000000000"), new string[] { "[* TO 12500]", "[12501 TO 15000]", "[15001 TO 17500]", "[17501 TO *]" }));
            //facetHandlers.Add(new MultiValueFacetHandler("tags"));


            System.IO.DirectoryInfo idxDir = new System.IO.DirectoryInfo(indexDir);
            using (IndexReader reader = IndexReader.Open(FSDirectory.Open(idxDir), true))
            {
                using (BoboIndexReader boboReader = BoboIndexReader.GetInstance(reader))
                {
                    using (BoboBrowser browser = new BoboBrowser(boboReader))
                    {
                        return browser.Browse(browseRequest);
                    }
                }
            }
        }
 public virtual BrowseResult Browse(BrowseRequest req) // throws BrowseException
 {
     BrowseResult result = BrowseService_Fields.EMPTY_RESULT;
     if (req.Offset < 0)
     {
         throw new BrowseException("Invalid offset: " + req.Offset);
     }
     if (_reader != null)
     {
         BoboBrowser browser = new BoboBrowser(_reader);
         result = browser.Browse(req);
     }
     return result;
 }
Example #5
0
        private static void DoBrowse(BoboBrowser browser)
        {
            String q = "java";
            QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "b", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT));
            Query query = parser.Parse(q);
            BrowseRequest br = new BrowseRequest();
            //br.setQuery(query);
            br.Offset = 0;
            br.Count = 0;

            BrowseSelection geoSel = new BrowseSelection("geo_region");
            geoSel.AddValue("5227");
            BrowseSelection industrySel = new BrowseSelection("industry_norm");
            industrySel.AddValue("1");

            //br.AddSelection(geoSel);
            br.AddSelection(industrySel);

            FacetSpec regionSpec = new FacetSpec();
            regionSpec.ExpandSelection = true;
            regionSpec.OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc;
            regionSpec.MaxCount = 5;

            FacetSpec industrySpec=new FacetSpec();
            industrySpec.ExpandSelection = true;
            industrySpec.OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc;
            industrySpec.MaxCount = 5;
        

            FacetSpec numEndorserSpec=new FacetSpec();
            numEndorserSpec.ExpandSelection = true;
    
		    br.SetFacetSpec("industry_norm", industrySpec);
            br.SetFacetSpec("geo_region", regionSpec);
            br.SetFacetSpec("num_endorsers_norm", numEndorserSpec);

		    long start = System.Environment.TickCount;
		    BrowseResult res = browser.Browse(br);
		    long end = System.Environment.TickCount;

            Console.WriteLine("result: " + res);
            Console.WriteLine("took: " + (end-start));
        }
Example #6
0
        public void TestSimpleBrowser()
        {
            var query = new TermQuery(new Term("name", "asp.net"));
            Console.WriteLine(string.Format("query: <{0}>", query.ToString()));
            var request = new BrowseRequest()
            {
                Count = 10,
                Offset = 0,
                Query = query,
                Sort = new Lucene.Net.Search.Sort(new SortField("price", SortField.DOUBLE, false)).GetSort()
            };

            var facetHandlers = new IFacetHandler[] { new SimpleFacetHandler("category") };
            var browser = new BoboBrowser(BoboIndexReader.GetInstance(IndexReader.Open(_indexDir, true), facetHandlers));
            var facetSpec = new FacetSpec() { OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc, MinHitCount = 1 };
            request.SetFacetSpec("category", facetSpec);

            var result = browser.Browse(request);
            Console.WriteLine(string.Format("total hits:{0}", result.NumHits));
            Console.WriteLine("===========================");
            foreach (var facet in result.FacetMap["category"].GetFacets())
            {
                var category = _categories.First(k => k.Value == int.Parse(facet.Value.ToString()));
                Console.WriteLine("{0}:({1})", category.Key, facet.FacetValueHitCount);
            }
            Console.WriteLine("===========================");
            for (var i = 0; i < result.Hits.Length; i++)
            {
                var doc = browser.Doc(result.Hits[i].DocId);
                var category = _categories.First(k => k.Value == int.Parse(doc.GetField("category").StringValue)).Key;
                Console.WriteLine(string.Format("{2} - {0} ${1} by {3}", doc.GetField("name").StringValue, doc.GetField("price").StringValue, category, doc.GetField("author").StringValue));
            }
        }
Example #7
0
        public void TestFacetSelectionFilter()
        {
            var query = new MatchAllDocsQuery();
            Console.WriteLine(string.Format("query: <{0}>", query.ToString()));
            var request = new BrowseRequest()
            {
                Count = 10,
                Offset = 0,
                Query = query
            };
            var authors = new string[] { "kathy", "sierra" };//kathy&sierra
            var sectionFilter = new BrowseSelection("author");
            sectionFilter.Values = authors;
            sectionFilter.SelectionOperation = BrowseSelection.ValueOperation.ValueOperationAnd;
            request.AddSelection(sectionFilter);

            var browser = new BoboBrowser(BoboIndexReader.GetInstance(IndexReader.Open(_indexDir, true), new IFacetHandler[] { new MultiValueFacetHandler("author") }));
            var result = browser.Browse(request);
            Console.WriteLine("===========================");
            for (var i = 0; i < result.Hits.Length; i++)
            {
                var doc = browser.Doc(result.Hits[i].DocId);
                var category = _categories.First(k => k.Value == int.Parse(doc.GetField("category").StringValue)).Key;
                Console.WriteLine(string.Format("{2} - {0} ${1} by {3}", doc.GetField("name").StringValue, doc.GetField("price").StringValue, category, doc.GetField("author").StringValue));
            }
        }
        public void TestNotValuesForSimpleFacetHandler()
        {
            BrowseRequest br = new BrowseRequest();
            br.Count = 20;
            br.Offset = 0;

            BrowseSelection colorSel = new BrowseSelection("color");
            colorSel.AddValue("red");
            br.AddSelection(colorSel);

            BrowseSelection idSel = new BrowseSelection("id");
            idSel.AddNotValue("0");
            br.AddSelection(idSel);

            BrowseResult result = null;
            BoboBrowser boboBrowser = null;
            int expectedHitNum = (_documentSize / 2) - 1;

            using (Directory ramIndexDir = CreateIndex())
            {
                using (IndexReader srcReader = IndexReader.Open(ramIndexDir, true))
                {
                    using (boboBrowser = new BoboBrowser(BoboIndexReader.GetInstance(srcReader, _facetHandlers, null)))
                    {
                        result = boboBrowser.Browse(br);

                        Assert.AreEqual(expectedHitNum, result.NumHits);

                        StringBuilder buffer = new StringBuilder();
                        BrowseHit[] hits = result.Hits;

                        for (int i = 0; i < hits.Length; ++i)
                        {
                            int expectedID = (i + 1) * 2;
                            Assert.AreEqual(expectedID, int.Parse(hits[i].GetField("id")));
                            if (i != 0)
                            {
                                buffer.Append('\n');
                            }
                            buffer.Append("id=" + hits[i].GetField("id") + "," + "color=" + hits[i].GetField("color"));
                        }
                        log.Info(buffer.ToString());
                    }
                }
            }
        }
Example #9
0
        public virtual BrowseResult Browse(BrowseRequest req)
        {
            if (_reader == null)
                return new BrowseResult();

            BrowseResult result = new BrowseResult();

            long start = System.Environment.TickCount;

            SortCollector collector = GetSortCollector(req.Sort, req.Query, req.Offset, req.Count, req.FetchStoredFields, req.TermVectorsToFetch, false, req.GroupBy, req.MaxPerGroup, req.CollectDocIdCache);
    
            IDictionary<string, IFacetAccessible> facetCollectors = new Dictionary<string, IFacetAccessible>();
            Browse(req, collector, facetCollectors);
            BrowseHit[] hits = null;

            try
            {
                hits = collector.TopDocs;
            }
            catch (Exception e)
            {
                logger.Error(e.Message, e);
                hits = new BrowseHit[0];
            }
    
            var q = req.Query;
            if (q == null)
            {
    	        q = new MatchAllDocsQuery();
            }
            if (req.ShowExplanation)
            {
    	        foreach (BrowseHit hit in hits)
                {
    		        try 
                    {
				        Explanation expl = Explain(q, hit.DocId);
				        hit.Explanation = expl;
			        } 
                    catch (Exception e) 
                    {
				        logger.Error(e.Message, e);
			        }
    	        }
            }
            result.Hits = hits;
            result.NumHits = collector.TotalHits;
            result.NumGroups = collector.TotalGroups;
            result.GroupAccessibles = collector.GroupAccessibles;
            result.SortCollector = collector;
            result.TotalDocs = _reader.NumDocs();
            result.AddAll(facetCollectors);
            long end = System.Environment.TickCount;
            result.Time = (end - start);
            return result;
        }
Example #10
0
 public virtual void Browse(
     BrowseRequest req,
     Collector collector,
     IDictionary<string, IFacetAccessible> facetMap,
     int start)
 {
     Weight w = null;
     try
     {
         var q = req.Query;
         if (q == null)
         {
             q = new MatchAllDocsQuery();
         }
         w = CreateWeight(q);
     }
     catch (Exception ioe)
     {
     throw new BrowseException(ioe.Message, ioe);
     }
     Browse(req, w, collector, facetMap, start);
 }
Example #11
0
        /// <summary>
        /// Generates a merged BrowseResult from the supplied <see cref="T:BrowseRequest"/> and a <see cref="T:Lucene.Net.Search.Weight"/>.
        /// The results are put into a Lucene.Net <see cref="T:Lucene.Net.Search.Collector"/> and a <see cref="T:System.Collections.Generic.IDictionary{System.String, IFacetAccessible}"/>.
        /// </summary>
        /// <param name="req"><see cref="T:BrowseRequest"/> for generating the facets.</param>
        /// <param name="weight">A <see cref="T:Lucene.Net.Search.Weight"/> instance to alter the score of the queries in a multiple index scenario.</param>
        /// <param name="hitCollector">A <see cref="T:Lucene.Net.Search.Collector"/> for the hits generated during a search.</param>
        /// <param name="facetMap">A dictionary of all of the facet collections (output).</param>
        /// <param name="start">The offset value for the document number.</param>
        public virtual void Browse(
            BrowseRequest req,
            Weight weight,
            Collector hitCollector,
            IDictionary <string, IFacetAccessible> facetMap,
            int start)
        {
            IBrowsable[] browsers = this.GetSubBrowsers();
            // index empty
            if (browsers == null || browsers.Length == 0)
            {
                return;
            }
            int[] starts = GetStarts();

            var mergedMap = new Dictionary <string, IList <IFacetAccessible> >();

            try
            {
                var facetColMap = new Dictionary <string, IFacetAccessible>();
                for (int i = 0; i < browsers.Length; i++)
                {
                    try
                    {
                        browsers[i].Browse(req, weight, hitCollector, facetColMap, (start + starts[i]));
                    }
                    finally
                    {
                        foreach (var entry in facetColMap)
                        {
                            string           name          = entry.Key;
                            IFacetAccessible facetAccessor = entry.Value;
                            var list = mergedMap.Get(name);
                            if (list == null)
                            {
                                list = new List <IFacetAccessible>(browsers.Length);
                                mergedMap.Put(name, list);
                            }
                            list.Add(facetAccessor);
                        }
                        facetColMap.Clear();
                    }
                }
            }
            finally
            {
                if (req.MapReduceWrapper != null)
                {
                    req.MapReduceWrapper.FinalizePartition();
                }
                foreach (var entry in mergedMap)
                {
                    string        name    = entry.Key;
                    IFacetHandler handler = GetFacetHandler(name);
                    try
                    {
                        IList <IFacetAccessible> subList = entry.Value;
                        if (subList != null)
                        {
                            IFacetAccessible merged = handler.Merge(req.GetFacetSpec(name), subList);
                            facetMap.Put(name, merged);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(e.Message, e);
                    }
                }
            }
        }
 private static bool IsNoQueryNoFilter(BrowseRequest req)
 {
     Lucene.Net.Search.Query q = req.Query;
     Filter filter = req.Filter;
     return ((q == null || q is MatchAllDocsQuery || q is FastMatchAllDocsQuery) && filter == null);
 }
        ///<summary>browses the index.</summary>
        ///<param name="req">browse request </param>
        ///<returns> browse result </returns>
        public virtual BrowseResult Browse(BrowseRequest req) // throws BrowseException
        {
            if (reader == null)
            {
                return new BrowseResult();
            }

            BrowseResult result = new BrowseResult();

            long start = System.Environment.TickCount;

            int offset = req.Offset;
            int count = req.Count;

            if (offset < 0 || count <= 0)
            {
                throw new ArgumentException("the offset must be >= 0 and count must be > 0: " + offset + "/" + count);
            }
            TopDocsSortedHitCollector myHC = GetSortedHitCollector(req.Sort, offset, count, req.FetchStoredFields);
            Dictionary<string, IFacetAccessible> facetCollectors = new Dictionary<string, IFacetAccessible>();

            Browse(req, myHC, facetCollectors);
            BrowseHit[] hits = null;

            try
            {
                hits = myHC.GetTopDocs();
            }
            catch (System.IO.IOException e)
            {
                logger.Error(e.Message, e);
                hits = new BrowseHit[0];
            }
            result.Hits = hits;
            result.NumHits = myHC.GetTotalHits();
            result.TotalDocs = reader.NumDocs();
            result.AddAll(facetCollectors);
            long end = System.Environment.TickCount;
            result.Time = end - start;
            return result;
        }
        ///<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);
            }
        }
        public void TestIndexReload()
        {
            try
            {
                RAMDirectory idxDir = new RAMDirectory();
                Document[] docs = BoboTestCase.BuildData();
                BoboIndexReader.WorkArea workArea = new BoboIndexReader.WorkArea();
                BrowseRequest req;
                BrowseSelection sel;
                BoboBrowser browser;
                BrowseResult result;

                IndexWriter writer = new IndexWriter(idxDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.UNLIMITED);
                writer.Close();

                int dup = 0;
                for (int j = 0; j < 50; j++)
                {
                    IndexReader idxReader = IndexReader.Open(idxDir, true);
                    BoboIndexReader reader = BoboIndexReader.GetInstance(idxReader, _fconf, workArea);

                    req = new BrowseRequest();
                    req.Offset = 0;
                    req.Count = 10;
                    sel = new BrowseSelection("color");
                    sel.AddValue("red");
                    req.AddSelection(sel);
                    browser = new BoboBrowser(reader);
                    result = browser.Browse(req);

                    Assert.AreEqual(3 * dup, result.NumHits);

                    req = new BrowseRequest();
                    req.Offset = 0;
                    req.Count = 10;
                    sel = new BrowseSelection("tag");
                    sel.AddValue("dog");
                    req.AddSelection(sel);
                    browser = new BoboBrowser(reader);
                    result = browser.Browse(req);

                    Assert.AreEqual(2 * dup, result.NumHits);

                    req = new BrowseRequest();
                    req.Offset = 0;
                    req.Count = 10;
                    sel = new BrowseSelection("tag");
                    sel.AddValue("funny");
                    req.AddSelection(sel);
                    browser = new BoboBrowser(reader);
                    result = browser.Browse(req);

                    Assert.AreEqual(3 * dup, result.NumHits);

                    writer = new IndexWriter(idxDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT), false, IndexWriter.MaxFieldLength.UNLIMITED);
                    for (int k = 0; k <= j; k++)
                    {
                        for (int i = 0; i < docs.Length; i++)
                        {
                            writer.AddDocument(docs[i]);
                        }
                        dup++;
                    }
                    writer.Close();
                }
                idxDir.Close();
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }
Example #16
0
 /// <summary>
 /// Generates a merged BrowseResult from the supplied <see cref="T:BrowseRequest"/>.
 /// The results are put into a Lucene.Net <see cref="T:Lucene.Net.Search.Collector"/> and a <see cref="T:System.Collections.Generic.IDictionary{System.String, IFacetAccessible}"/>.
 /// </summary>
 /// <param name="req"><see cref="T:BrowseRequest"/> for generating the facets.</param>
 /// <param name="hitCollector">A <see cref="T:Lucene.Net.Search.Collector"/> for the hits generated during a search.</param>
 /// <param name="facetMap">A dictionary of all of the facet collections (output).</param>
 public virtual void Browse(BrowseRequest req, Collector hitCollector, IDictionary <string, IFacetAccessible> facetMap)
 {
     Browse(req, hitCollector, facetMap, 0);
 }
Example #17
0
        public void TestMultiValueFacetHandler()
        {
            var query = new MatchAllDocsQuery();
            Console.WriteLine(string.Format("query: <{0}>", query.ToString()));
            var request = new BrowseRequest()
            {
                Count = 100,
                Offset = 0,
                Query = query,
                Sort = new Sort(new SortField("path", SortField.STRING, false)).GetSort()
            };

            var faceHandlers = new FacetHandler[] { new MultiValueFacetHandler("path") };
            var browser = new BoboBrowser(BoboIndexReader.GetInstance(IndexReader.Open(_indexDir, true), faceHandlers));
            var factSpec = new FacetSpec() { OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc };
            request.SetFacetSpec("path", factSpec);

            var result = browser.Browse(request);
            Console.WriteLine(string.Format("total hits:{0}", result.NumHits));
            Console.WriteLine("===========================");
            foreach (var facet in result.FacetMap["path"].GetFacets())
            {
                Console.WriteLine(facet.ToString());               
            }
            Console.WriteLine("===========================");
            for (var i = 0; i < result.Hits.Length; i++)
            {
                var doc = browser.Doc(result.Hits[i].DocId);
                var category = _categories.First(k => k.Value == int.Parse(doc.GetField("category").StringValue)).Key;
                Console.WriteLine(string.Format("{2} - {0} ${1} by {3}", doc.GetField("name").StringValue, doc.GetField("price").StringValue, category, doc.GetField("author").StringValue));
            }
        }
Example #18
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);
            }
        }
Example #19
0
        /// <summary>
        /// Generates a merged BrowseResult from the supplied <see cref="T:BrowseRequest"/>.
        /// </summary>
        /// <param name="req"><see cref="T:BrowseRequest"/> for generating the facets.</param>
        /// <returns><see cref="T:BrowseResult"/> of the results corresponding to the <see cref="T:BrowseRequest"/>.</returns>
        public virtual BrowseResult Browse(BrowseRequest req)
        {
            BrowseResult result = new BrowseResult();

            // index empty
            if (m_subBrowsers == null || m_subBrowsers.Length == 0)
            {
                return(result);
            }
            long start  = System.Environment.TickCount;
            int  offset = req.Offset;
            int  count  = req.Count;

            if (offset < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException("both offset and count must be > 0: " + offset + "/" + count);
            }
            SortCollector collector = GetSortCollector(req.Sort, req.Query, offset, count,
                                                       req.FetchStoredFields, req.TermVectorsToFetch, req.GroupBy, req.MaxPerGroup,
#pragma warning disable 612, 618
                                                       req.CollectDocIdCache);

#pragma warning restore 612, 618

            var facetCollectors = new Dictionary <string, IFacetAccessible>();
            Browse(req, collector, facetCollectors, 0);

            if (req.MapReduceWrapper != null)
            {
                result.MapReduceResult = req.MapReduceWrapper.Result;
            }
            BrowseHit[] hits = null;
            try
            {
                hits = collector.TopDocs;
            }
            catch (Exception e)
            {
                logger.ErrorException(e.Message, e);
                result.AddError(e.Message);
                hits = new BrowseHit[0];
            }

            var q = req.Query;
            if (req.ShowExplanation)
            {
                foreach (BrowseHit hit in hits)
                {
                    try
                    {
                        int         doc        = hit.DocId;
                        int         idx        = ReaderIndex(doc);
                        int         deBasedDoc = doc - ReaderBase(idx);
                        Explanation expl       = m_subBrowsers[idx].Explain(q, deBasedDoc);
                        hit.SetExplanation(expl);
                    }
                    catch (Exception e)
                    {
                        logger.ErrorException(e.Message, e);
                        result.AddError(e.Message);
                    }
                }
            }

            result.Hits             = hits;
            result.NumHits          = collector.TotalHits;
            result.NumGroups        = collector.TotalGroups;
            result.GroupAccessibles = collector.GroupAccessibles;
            result.SortCollector    = collector;
            result.TotalDocs        = this.NumDocs;
            result.AddAll(facetCollectors);
            long end = System.Environment.TickCount;
            result.Time = (end - start);
            // set the transaction ID to trace transactions
            result.Tid = req.Tid;
            return(result);
        }
Example #20
0
 private bool IsNoQueryNoFilter(BrowseRequest req)
 {
     Lucene.Net.Search.Query q = req.Query;
     Filter filter = req.Filter;
     return ((q == null || q is MatchAllDocsQuery) && filter == null && !_reader.HasDeletions); 
 }
Example #21
0
        /// <summary>
        /// Generates a merged BrowseResult from the supplied <see cref="T:BrowseRequest"/>.
        /// The results are put into a Lucene.Net <see cref="T:Lucene.Net.Search.Collector"/> and a <see cref="T:System.Collections.Generic.IDictionary{System.String, IFacetAccessible}"/>.
        /// </summary>
        /// <param name="req"><see cref="T:BrowseRequest"/> for generating the facets.</param>
        /// <param name="hitCollector">A <see cref="T:Lucene.Net.Search.Collector"/> for the hits generated during a search.</param>
        /// <param name="facetMap">A dictionary of all of the facet collections (output).</param>
        /// <param name="start">The offset value for the document number.</param>
        public virtual void Browse(
            BrowseRequest req,
            ICollector hitCollector,
            IDictionary <string, IFacetAccessible> facetMap,
            int start)
        {
            // index empty
            if (m_subBrowsers == null || m_subBrowsers.Length == 0)
            {
                return;
            }

            try
            {
                var q = req.Query;
                MatchAllDocsQuery matchAllDocsQuery = new MatchAllDocsQuery();
                if (q == null)
                {
                    q = matchAllDocsQuery;
                }
                else if (!(q is MatchAllDocsQuery))
                {
                    //MatchAllQuery is needed to filter out the deleted docids, that reside in ZoieSegmentReader and are not visible on Bobo level
                    matchAllDocsQuery.Boost = 0f;
                    q = QueriesSupport.CombineAnd(matchAllDocsQuery, q);
                }
                req.Query = q;
            }
            catch (Exception ioe)
            {
                throw new BrowseException(ioe.Message, ioe);
            }

            var mergedMap = new Dictionary <string, IList <IFacetAccessible> >();

            try
            {
                var facetColMap = new Dictionary <string, IFacetAccessible>();
                for (int i = 0; i < m_subBrowsers.Length; i++)
                {
                    try
                    {
                        m_subBrowsers[i].Browse(req, hitCollector, facetColMap, (start + ReaderBase(i)));
                    }
                    finally
                    {
                        foreach (var entry in facetColMap)
                        {
                            string           name          = entry.Key;
                            IFacetAccessible facetAccessor = entry.Value;
                            var list = mergedMap.Get(name);
                            if (list == null)
                            {
                                list = new List <IFacetAccessible>(m_subBrowsers.Length);
                                mergedMap.Put(name, list);
                            }
                            list.Add(facetAccessor);
                        }
                        facetColMap.Clear();
                    }
                }
            }
            finally
            {
                if (req.MapReduceWrapper != null)
                {
                    req.MapReduceWrapper.FinalizePartition();
                }
                foreach (var entry in mergedMap)
                {
                    string        name    = entry.Key;
                    IFacetHandler handler = GetFacetHandler(name);
                    try
                    {
                        IList <IFacetAccessible> subList = entry.Value;
                        if (subList != null)
                        {
                            IFacetAccessible merged = handler.Merge(req.GetFacetSpec(name), subList);
                            facetMap.Put(name, merged);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.ErrorException(e.Message, e);
                    }
                }
            }
        }
        public void TestNotValuesForRangeFacetHandler()
        {
            Console.WriteLine("TestNotValuesForRangeFacetHandler");
            BrowseResult result = null;
            BoboBrowser boboBrowser=null;

            using (Directory ramIndexDir = CreateIndexTwo())
            {

                using (IndexReader srcReader = IndexReader.Open(ramIndexDir, true))
                {

                    using (boboBrowser = new BoboBrowser(BoboIndexReader.GetInstance(srcReader, _facetHandlers, null)))
                    {

                        BrowseRequest br = new BrowseRequest();
                        br.Count = (20);
                        br.Offset = (0);

                        if (_idRanges == null)
                        {
                            log.Error("_idRanges cannot be null in order to test NOT on RangeFacetHandler");
                        }
                        BrowseSelection idSel = new BrowseSelection("idRange");
                        //int rangeIndex = 2; // Not used
                        idSel.AddNotValue(_idRanges[0]);
                        int expectedHitNum = 1;
                        br.AddSelection(idSel);
                        BooleanQuery q = new BooleanQuery();
                        q.Add(NumericRangeQuery.NewIntRange("NUM", 10, 10, true, true), Occur.MUST_NOT);
                        q.Add(new MatchAllDocsQuery(), Occur.MUST);
                        br.Query = q;

                        result = boboBrowser.Browse(br);

                        Assert.AreEqual(expectedHitNum, result.NumHits);
                        for (int i = 0; i < result.NumHits; i++)
                        {
                            Console.WriteLine(result.Hits[i]);
                        }
                    }
                }
            }
        }
Example #23
0
        public void TestWithInterleavedCommitsUsingBobo()
        {
            string text = "text";

            Document doc1 = new Document();

            doc1.Add(new TextField(text, "Foo1", Field.Store.YES));
            m_indexWriter.AddDocument(doc1);
            m_indexWriter.Commit();

            Document doc2 = new Document();

            doc2.Add(new TextField(text, "Foo2", Field.Store.YES));
            m_indexWriter.AddDocument(doc2);
            m_indexWriter.Commit();

            Document doc3 = new Document();

            doc3.Add(new TextField(text, "Foo3", Field.Store.YES));
            m_indexWriter.AddDocument(doc3);
            m_indexWriter.Commit();

            List <IFacetHandler> handlerList = new List <IFacetHandler>();


            DirectoryReader reader = BoboMultiReader.Open(m_indexWriter, true);

            BoboMultiReader boboMultiReader = BoboMultiReader.GetInstance(reader,
                                                                          handlerList);

            BrowseRequest br = new BrowseRequest();

            br.Count  = (10);
            br.Offset = (0);

            QueryParser parser = new QueryParser(LuceneVersion.LUCENE_48, "text",
                                                 new StandardAnalyzer(LuceneVersion.LUCENE_48));

            Lucene.Net.Search.Query q = parser.Parse("Foo*");
            br.Query = (q);

            BoboBrowser  browser = new BoboBrowser(boboMultiReader);
            BrowseResult result  = browser.Browse(br);

            int totalHits = result.NumHits;

            BrowseHit[] hits = result.Hits;

            Assert.AreEqual(3, totalHits, "should be 3 hits");
            Assert.AreEqual(0, hits[0].DocId, "should be doc 0");
            Assert.AreEqual(1, hits[1].DocId, "should be doc 1"); // <-- This is
            // where the
            // test fails,
            // because all
            // three browser
            // hits are
            // returned with
            // doc id 0
            Assert.AreEqual(2, hits[2].DocId, "should be doc 2");

            result.Dispose();
        }
Example #24
0
        public void TestAutoComplete()
        {
            var query = new MatchAllDocsQuery();
            Console.WriteLine(string.Format("query: <{0}>", query.ToString()));
            var request = new BrowseRequest()
            {
                Count = 10,
                Offset = 0,
                Query = query
            };

            //var prefix = "java"; // NightOwl888: Prefix is no longer a feature
            //Console.WriteLine(string.Format("prefix:{0}", prefix));
            //Console.WriteLine("=============================");

            var faceHandlers = new IFacetHandler[] { new SimpleFacetHandler("name") };
            var browser = new BoboBrowser(BoboIndexReader.GetInstance(IndexReader.Open(_indexDir, true), faceHandlers));
            var factSpec = new FacetSpec() { OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc };
            request.SetFacetSpec("name", factSpec);

            var result = browser.Browse(request);

            foreach (var facet in result.FacetMap["name"].GetFacets())
            {
                Console.WriteLine(facet.ToString());
            }
        }
        public void TestFacetNameForSimpleFacetHandler()
        {
            BrowseRequest br = new BrowseRequest();
            br.Count = 20;
            br.Offset = 0;

            BrowseSelection colorSel = new BrowseSelection("mycolor");
            colorSel.AddValue("yellow");
            br.AddSelection(colorSel);

            BrowseSelection makeSel = new BrowseSelection("make");
            makeSel.AddValue("rav4");
            br.AddSelection(makeSel);

            FacetSpec spec = new FacetSpec();
            spec.ExpandSelection = true;
            spec.OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc;
            spec.MaxCount = 15;

            br.SetFacetSpec("mycolor", spec);
            br.SetFacetSpec("id", spec);
            br.SetFacetSpec("make", spec);

            int expectedHitNum = 3;

            Directory ramIndexDir = CreateIndex();
            using (IndexReader srcReader = IndexReader.Open(ramIndexDir, true))
            {
                using (BoboIndexReader boboReader = BoboIndexReader.GetInstance(srcReader, _facetHandlers, null))
                {
                    using (BoboBrowser boboBrowser = new BoboBrowser(boboReader))
                    {
                        using (BrowseResult result = boboBrowser.Browse(br))
                        {

                            Assert.AreEqual(expectedHitNum, result.NumHits);
                        }
                    }
                }
            }
        }
Example #26
0
        public void TestRangeFacetHandler()
        {
            var query = new MatchAllDocsQuery();
            Console.WriteLine(string.Format("query: <{0}>", query.ToString()));

            var testRangeFacetHandlers = new List<IFacetHandler>();
            //testRangeFacetHandlers.Add(new RangeFacetHandler("year", true));//auto range // NightOwl888 - Auto range is no longer a feature.
            testRangeFacetHandlers.Add(new RangeFacetHandler("year", new List<string>(new string[] { "[* TO 2000]", "[2000 TO 2005]", "[2006 TO 2010]", "[2011 TO *]" })));
            //testRangeFacetHandlers.Add(new RangeFacetHandler("price", "price", new NumberFieldFactory(), true)); // NightOwl888 - Auto range is no longer a feature.

            for (var i = 0; i < testRangeFacetHandlers.Count; i++)
            {
                var request = new BrowseRequest()
                {
                    Count = 100,
                    Offset = 0,
                    Query = query,
                    Sort = new Lucene.Net.Search.Sort(new SortField("price", SortField.DOUBLE, false)).GetSort()
                };

                var sectionFilter = new BrowseSelection("category");
                sectionFilter.NotValues = new string[] { "5" };
                sectionFilter.SelectionOperation = BrowseSelection.ValueOperation.ValueOperationAnd;
                request.AddSelection(sectionFilter);

                var faceHandler = testRangeFacetHandlers[i];
                var faceHandlers = new IFacetHandler[] { faceHandler, new SimpleFacetHandler("category") };
                var browser = new BoboBrowser(BoboIndexReader.GetInstance(IndexReader.Open(_indexDir, true), faceHandlers));
                var factSpec = new FacetSpec() { OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc };
                request.SetFacetSpec(faceHandler.Name, factSpec);
                var result = browser.Browse(request);
                Console.WriteLine(string.Format("total hits:{0}", result.NumHits));
                foreach (var facet in result.FacetMap[faceHandler.Name].GetFacets())
                {
                    Console.WriteLine(facet.ToString());
                }
                Console.WriteLine("");
            }
        }
        /// <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>
        /// <param name="start"></param>
        public virtual void Browse(
            BrowseRequest req,
            ICollector collector,
            IDictionary <string, IFacetAccessible> facetMap,
            int start)
        {
            if (m_reader == null)
            {
                return;
            }


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

            IEnumerable <string> runtimeFacetNames = m_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)m_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)
                        {
                            m_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.SetFacetHitCollectorList(facetHitCollectorList);

                try
                {
                    var    query  = req.Query;
                    Weight weight = CreateNormalizedWeight(query);
                    Search(weight, finalFilter, collector, start, req.MapReduceWrapper);
                }
                finally
                {
                    foreach (FacetHitCollector facetCollector in facetHitCollectorList)
                    {
                        string name = facetCollector.FacetHandler.Name;
                        IList <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);
            }
        }
Example #28
0
        public void TestNumberRangeWithInt()
        {
            // Field.
            string field = "number";

            // Lucene index.
            using (IndexReader reader = IndexReader.Open(this._indexDir, true))
            {
                // Bobo reader.
                using (BoboIndexReader boboReader = BoboIndexReader.GetInstance(reader, this._facetHandlers))
                {
                    // Request.
                    BrowseRequest browseRequest = new BrowseRequest();
                    browseRequest.Count = 10;
                    browseRequest.Offset = 0;
                    browseRequest.FetchStoredFields = true;

                    // Selection.
                    BrowseSelection sel = new BrowseSelection(field);
                    browseRequest.AddSelection(sel);

                    // Query.
                    MatchAllDocsQuery query = new MatchAllDocsQuery();

                    // Output.
                    FacetSpec spec = new FacetSpec();
                    spec.OrderBy = FacetSpec.FacetSortSpec.OrderValueAsc;
                    spec.MaxCount = 10;
                    browseRequest.SetFacetSpec(field, spec);

                    // Browse.
                    IBrowsable browser = new BoboBrowser(boboReader);
                    using (BrowseResult result = browser.Browse(browseRequest))
                    {
                        // Results.
                        int totalHits = result.NumHits;
                        BrowseHit[] hits = result.Hits;
                        IDictionary<String, IFacetAccessible> facetMap = result.FacetMap;
                        IFacetAccessible facets = facetMap[field];
                        List<BrowseFacet> facetVals = facets.GetFacets().ToList();

                        // Check.
                        Assert.AreEqual(10, totalHits);
                        Assert.AreEqual(5, facetVals.Count());
                        Assert.AreEqual("[0000000000 TO 0000001000](3)", facetVals[0].ToString());
                        Assert.AreEqual("[0000001000 TO 0000010000](3)", facetVals[1].ToString());
                        Assert.AreEqual("[0000010000 TO 0000100000](2)", facetVals[2].ToString());
                        Assert.AreEqual("[0000100000 TO 0001000000](3)", facetVals[3].ToString());
                        Assert.AreEqual("[0001000000 TO *](3)", facetVals[4].ToString());
                    }
                }
            }
        }
 /// <summary>
 /// Browses the index
 /// </summary>
 /// <param name="req">Browse request</param>
 /// <returns>Browse result</returns>
 public virtual BrowseResult Browse(BrowseRequest req)
 {
     throw new NotSupportedException();
 }
        public void TestFacetNameForSimpleFacetHandler()
        {
            BrowseRequest br = new BrowseRequest();
            br.Count = 20;
            br.Offset = 0;

            BrowseSelection colorSel = new BrowseSelection("mycolor");
            colorSel.AddValue("yellow");
            br.AddSelection(colorSel);

            BrowseSelection makeSel = new BrowseSelection("make");
            makeSel.AddValue("rav4");
            br.AddSelection(makeSel);

            FacetSpec spec = new FacetSpec();
            spec.ExpandSelection = true;
            spec.OrderBy = FacetSpec.FacetSortSpec.OrderHitsDesc;
            spec.MaxCount = 15;

            br.SetFacetSpec("mycolor", spec);
            br.SetFacetSpec("id", spec);
            br.SetFacetSpec("make", spec);

            BrowseResult result = null;
            BoboBrowser boboBrowser = null;
            int expectedHitNum = 3;
            try
            {
                Directory ramIndexDir = CreateIndex();
                IndexReader srcReader = IndexReader.Open(ramIndexDir, true);
                boboBrowser = new BoboBrowser(BoboIndexReader.GetInstance(srcReader, _facetHandlers, null));
                result = boboBrowser.Browse(br);

                Assert.AreEqual(expectedHitNum, result.NumHits);
            }
            catch (BrowseException e)
            {
                Assert.Fail(e.Message);
            }
            catch (System.IO.IOException ioe)
            {
                Assert.Fail(ioe.Message);
            }
            finally
            {
                if (boboBrowser != null)
                {
                    try
                    {
                        boboBrowser.Close();
                    }
                    catch (System.IO.IOException e)
                    {
                        Assert.Fail(e.Message);
                    }
                }
            }
        }
Example #31
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,
     IDictionary<string, IFacetAccessible> facetMap)
 {
     Browse(req, collector, facetMap, 0);
 }
 public virtual BrowseResult Browse(BrowseRequest req) // throws BrowseException
 {
     return BrowseServiceFactory.CreateBrowseService(reader).Browse(req);
 }
        public BrowseRequest ConvertBrowseRequest(BoboRequest boboRequest)
        {
            Query query = this.queryBuilder.ParseQuery(boboRequest.Query, boboRequest.Df);
            Sort sort = this.queryBuilder.ParseSort(boboRequest.Sort);

            var browseRequest = new BrowseRequest();
            browseRequest.Offset = boboRequest.Start;
            browseRequest.Count = boboRequest.Rows;
            browseRequest.Query = query;

            if (sort != null)
            {
                SortField[] sortFields = sort.GetSort();
                if (sortFields != null && sortFields.Length > 0)
                {
                    browseRequest.Sort = sortFields;
                }
            }

            var selMap = new Dictionary<string, BrowseSelection>();
            foreach (var selection in boboRequest.Selections)
            {
                BrowseSelection sel = selMap.ContainsKey(selection.Name) ? selMap[selection.Name] : null;
                if (sel == null)
                {
                    sel = new BrowseSelection(selection.Name);
                    selMap.Add(selection.Name, sel);
                }
                foreach (var val in selection.Values)
                {
                    sel.AddValue(val);
                }

                sel.SelectionOperation = selection.SelectionOperation;

                sel.SetSelectionProperty("depth", selection.Depth.ToString());
                sel.SetSelectionProperty("strict", selection.Strict.ToString().ToLower());
            }
            if (selMap.Count > 0)
            {
                var sels = selMap.Values;
                foreach (var sel in sels)
                {
                    browseRequest.AddSelection(sel);
                }
            }

            if (boboRequest.Facet == true)
            {
                foreach (var facet in boboRequest.Facets)
                {
                    FacetSpec fspec = new FacetSpec();
                    browseRequest.SetFacetSpec(facet.Name, fspec);

                    fspec.MinHitCount = facet.MinCount == int.MinValue ? 0 : facet.MinCount;
                    fspec.MaxCount = facet.Limit == int.MinValue ? 100 : facet.Limit;
                    fspec.ExpandSelection = facet.Expand;
                    fspec.OrderBy = ParseFacetSort(facet.Sort, FacetSpec.FacetSortSpec.OrderHitsDesc);
                }
            }

            return browseRequest;
        }