/// <summary>
        /// *****************************************************
        /// * SAMPLE USAGE                                      *
        /// *****************************************************
        /// </summary>
        void HowToUse(string searchString)
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse(searchString);

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "source", "category" });

            SimpleFacetedSearch.Hits hits = sfs.Search(query, 10);

            long totalHits = hits.TotalHitCount;

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                long hitCountPerGroup = hpg.HitCount;
                SimpleFacetedSearch.FacetName facetName = hpg.Name;
                for (int i = 0; i < facetName.Length; i++)
                {
                    string part = facetName[i];
                }
                foreach (Document doc in hpg.Documents)
                {
                    string text = doc.GetField("text").StringValue;
                    System.Diagnostics.Debug.WriteLine(">>" + facetName + ": " + text);
                }
            }
        }
        public void Test5()
        {
            Query query = new MatchAllDocsQuery();

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "category");

            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(7, hits.TotalHitCount);
        }
        public void Test3()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("block*");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "lang", "source", "category" });

            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(6, hits.HitsPerFacet.Length);
            int nohit = 0;

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                //Test for [System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary.]
                var x = hits[hpg.Name];
                var y = hits[hpg.Name.ToString()];

                if (hpg.Name[0] == "us" && hpg.Name[1] == "CCN" && hpg.Name[2] == "politics")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "en" && hpg.Name[1] == "BCC" && hpg.Name[2] == "tech")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "us" && hpg.Name[1] == "CCN" && hpg.Name[2] == "sport")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "en" && hpg.Name[1] == "CCN" && hpg.Name[2] == "tech")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                {
                    nohit++;
                    Assert.AreEqual(0, hpg.HitCount);
                }
            }
            Assert.AreEqual(2, nohit);
            Assert.AreEqual(4, hits.TotalHitCount);

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                foreach (Document doc in hpg.Documents)
                {
                    string text = doc.GetField("text").StringValue;
                    Assert.IsTrue(text.Contains("block"));
                }
            }
        }
        public void Test6()
        {
            Query query = new MatchAllDocsQuery();

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "nosuchfield");

            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(0, hits.TotalHitCount);
            Assert.AreEqual(0, hits.HitsPerFacet.Length);
        }
        public void Test4()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("xxxxxxxxxxxxx");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "category");

            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(4, hits.HitsPerFacet.Length);
            Assert.AreEqual(0, hits.HitsPerFacet[0].HitCount);
            Assert.AreEqual(0, hits.HitsPerFacet[1].HitCount);
            Assert.AreEqual(0, hits.HitsPerFacet[2].HitCount);
        }
        public void Test7()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("a");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "category");

            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(0, hits.TotalHitCount, "Unexpected TotalHitCount");
            foreach (var x in hits.HitsPerFacet.Where(h => h.HitCount > 0))
            {
                Assert.Fail("There must be no hit");
            }
        }
        public void Test2()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("block*");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "category");

            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(4, hits.HitsPerFacet.Length);

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                if (hpg.Name[0] == "politics")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "tech")
                {
                    Assert.AreEqual(2, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "sport")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                {
                    Assert.AreEqual(0, hpg.HitCount);
                }
            }

            Assert.AreEqual(4, hits.TotalHitCount);

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                foreach (Document doc in hpg.Documents)
                {
                    string text = doc.GetField("text").StringValue;
                    Assert.IsTrue(text.Contains("block"));
                }
            }
        }
        public void TestMultiThreadedAccess()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("block*");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "lang", "source", "category" });

            _errorCount = 0;

            Thread[] t = new Thread[20];
            for (int i = 0; i < t.Length; i++)
            {
                t[i] = new Thread(MultiThreadedAccessThread);
                t[i].Start(sfs);
            }
            for (int i = 0; i < t.Length; i++)
            {
                t[i].Join();
            }

            Assert.AreEqual(0, _errorCount);
        }
        public HomeController()
        {
            string path = HostingEnvironment.MapPath("~/App_Data/LuceneIndex");
            var fsd = FSDirectory.Open(new DirectoryInfo(path));
            _indexReader = IndexReader.Open(fsd, true);
            _indexSearcher = new IndexSearcher(fsd, true);
            _luceneVersion = Version.LUCENE_29;

            _searchFields = new string[]
                                {
                                    LuceneIndexFieldMap.FirstNameField,
                                    LuceneIndexFieldMap.CityField,
                                    LuceneIndexFieldMap.TimeZoneField
                                };

            _facetFields = new string[]
                               {
                                   LuceneIndexFieldMap.TimeZoneField
                               };

            _simpleFacetedSearch = new SimpleFacetedSearch(_indexReader, _facetFields);
        }
        public void Test6()
        {
            Query query = new MatchAllDocsQuery();

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "nosuchfield");
            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(0, hits.TotalHitCount);
            Assert.AreEqual(0, hits.HitsPerFacet.Length);
        }
        public void Test5()
        {
            Query query = new MatchAllDocsQuery();

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "category");
            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(7, hits.TotalHitCount);
        }
        public void Test4()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("xxxxxxxxxxxxx");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "category");
            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(4, hits.HitsPerFacet.Length);
            Assert.AreEqual(0, hits.HitsPerFacet[0].HitCount);
            Assert.AreEqual(0, hits.HitsPerFacet[1].HitCount);
            Assert.AreEqual(0, hits.HitsPerFacet[2].HitCount);
        }
        public void Test3()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("block*");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "lang", "source", "category" });
            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(6, hits.HitsPerFacet.Length);
            int nohit = 0;

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                //Test for [System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary.]
                var x = hits[hpg.Name];
                var y = hits[hpg.Name.ToString()];

                if (hpg.Name[0] == "us" && hpg.Name[1] == "CCN" && hpg.Name[2] == "politics")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "en" && hpg.Name[1] == "BCC" && hpg.Name[2] == "tech")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "us" && hpg.Name[1] == "CCN" && hpg.Name[2] == "sport")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "en" && hpg.Name[1] == "CCN" && hpg.Name[2] == "tech")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                {
                    nohit++;
                    Assert.AreEqual(0, hpg.HitCount);
                }
            }
            Assert.AreEqual(2, nohit);
            Assert.AreEqual(4, hits.TotalHitCount);

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                foreach (Document doc in hpg.Documents)
                {
                    string text = doc.GetField("text").StringValue;
                    Assert.IsTrue(text.Contains("block"));
                }
            }
        }
示例#14
0
 /// <summary>
 /// 
 /// </summary>
 /// <remarks></remarks>
 /// <seealso cref=""/>
 /// <param name="query"></param>
 /// <param name="facets"></param>
 /// <returns></returns>
 public static IEnumerable<Facet> facetSearch(Query query, IEnumerable<Facet> facets)
 {
     List<Facet> l = new List<Facet>();
     foreach (Facet f in facets)
     {
         Facet c = new Facet();
         c.Name = f.Name;
         c.Text = f.Text;
         c.Value = f.Value;
         c.DisplayName = f.DisplayName;
         c.Childrens = new List<Facet>();
         SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "facet_" + f.Name });
         SimpleFacetedSearch.Hits hits = sfs.Search(query);
         int cCount = 0;
         foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
         {
             if (!hpg.Name.ToString().Equals(""))
             {
                 Facet cc = new Facet();
                 cc.Name = hpg.Name.ToString();
                 cc.Text = hpg.Name.ToString();
                 cc.Value = hpg.Name.ToString();
                 cc.Count = (int)hpg.HitCount;
                 if (cc.Count > 0) cCount++;
                 c.Childrens.Add(cc);
             }
         }
         c.Count = cCount;
         l.Add(c);
     }
     return l;
 }
        public BeerReview GetFacets(string searchString)
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "reviewText", new
            StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30)).Parse(searchString);

            // pass in the reader and the names of the facets that you've created using fields in the documents.
            // the facets are determined

            // first get the BitArray result from the genre query
            //var genreQuery = new TermQuery(new Term("genre", genre));
            //var genreQueryFilter = new QueryFilter(genreQuery);
            //BitArray genreBitArray = genreQueryFilter.Bits(searcher.GetIndexReader());

            //Filter filter = new QueryFil

            //Console.WriteLine("There are " + GetCardinality(genreBitArray) + " document with the genre " + genre);

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(indexSearcher.IndexReader, new string[] { "reviewOverall", "reviewAroma" });

            // then pass in the query into the search like you normally would with a typical search class.

            SimpleFacetedSearch.Hits hits = sfs.Search(query, 10);

            // what comes back is different than normal.
            // the result documents & hits are grouped by facets.

            // you'll need to iterate over groups of hits-per-facet.

            long totalHits = hits.TotalHitCount;
            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                long hitCountPerGroup = hpg.HitCount;
                SimpleFacetedSearch.FacetName facetName = hpg.Name;
                for (int i = 0; i < facetName.Length; i++)
                {
                    string part = facetName[i];
                }
                foreach (Document doc in hpg.Documents)
                {
                    string text = doc.GetField("reviewOverall").StringValue;
                    System.Diagnostics.Debug.WriteLine(">>" + facetName + ": " + text);
                }
            }
            return null;
        }
        /// <summary>
        /// *****************************************************
        /// * SAMPLE USAGE                                      *
        /// *****************************************************
        /// </summary>
        void HowToUse(string searchString)
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse(searchString);

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "source", "category" });
            SimpleFacetedSearch.Hits hits = sfs.Search(query, 10);

            long totalHits = hits.TotalHitCount;
            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                long hitCountPerGroup = hpg.HitCount;
                SimpleFacetedSearch.FacetName facetName = hpg.Name;
                for (int i = 0; i < facetName.Length; i++)
                {
                    string part = facetName[i];
                }
                foreach (Document doc in hpg.Documents)
                {
                    string text = doc.GetField("text").StringValue;
                    System.Diagnostics.Debug.WriteLine(">>" + facetName + ": " + text);
                }
            }
        }
        public void Test7()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("a");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "category");
            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(0, hits.TotalHitCount, "Unexpected TotalHitCount");
            foreach(var x in hits.HitsPerFacet.Where(h=>h.HitCount>0))
            {
                Assert.Fail("There must be no hit");
            }
            
        }
        public void TestMultiThreadedAccess()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("block*");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, new string[] { "lang", "source", "category" });
            _errorCount = 0;

            Thread[] t = new Thread[20];
            for (int i = 0; i < t.Length; i++)
            {
                t[i] = new Thread(MultiThreadedAccessThread);
                t[i].Start(sfs);
            }
            for (int i = 0; i < t.Length; i++)
            {
                t[i].Join();
            }
            
            Assert.AreEqual(0, _errorCount);
        }
        void MultiThreadedAccessThread(object o)
        {
            SimpleFacetedSearch sfs = (SimpleFacetedSearch)o;

            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("block*");

            for (int i = 0; i < 2000; i++)
            {
                SimpleFacetedSearch.Hits hits = sfs.Search(query);

                if (6 != hits.HitsPerFacet.Length)
                {
                    _errorCount++;
                }

                foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
                {
                    if (hpg.Name[0] == "us" && hpg.Name[1] == "CCN" && hpg.Name[2] == "politics")
                    {
                        if (1 != hpg.HitCount)
                        {
                            _errorCount++;
                        }
                    }
                    else
                    if (hpg.Name[0] == "en" && hpg.Name[1] == "BCC" && hpg.Name[2] == "tech")
                    {
                        if (1 != hpg.HitCount)
                        {
                            _errorCount++;
                        }
                    }
                    else
                    if (hpg.Name[0] == "us" && hpg.Name[1] == "CCN" && hpg.Name[2] == "sport")
                    {
                        if (1 != hpg.HitCount)
                        {
                            _errorCount++;
                        }
                    }
                    else
                    if (hpg.Name[0] == "en" && hpg.Name[1] == "CCN" && hpg.Name[2] == "tech")
                    {
                        if (1 != hpg.HitCount)
                        {
                            _errorCount++;
                        }
                    }
                    else
                    {
                        if (0 != hpg.HitCount)
                        {
                            _errorCount++;
                        }
                    }

                    if (4 != hits.TotalHitCount)
                    {
                        _errorCount++;
                    }
                }
            }
        }
        public void Test2()
        {
            Query query = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)).Parse("block*");

            SimpleFacetedSearch sfs = new SimpleFacetedSearch(_Reader, "category");
            SimpleFacetedSearch.Hits hits = sfs.Search(query);

            Assert.AreEqual(4, hits.HitsPerFacet.Length);

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                if (hpg.Name[0] == "politics")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "tech")
                {
                    Assert.AreEqual(2, hpg.HitCount);
                }
                else
                if (hpg.Name[0] == "sport")
                {
                    Assert.AreEqual(1, hpg.HitCount);
                }
                else
                {
                    Assert.AreEqual(0, hpg.HitCount);
                }
            }

            Assert.AreEqual(4, hits.TotalHitCount);

            foreach (SimpleFacetedSearch.HitsPerFacet hpg in hits.HitsPerFacet)
            {
                foreach (Document doc in hpg.Documents)
                {
                    string text = doc.GetField("text").StringValue;
                    Assert.IsTrue(text.Contains("block"));
                }
            }
        }
        public IList<SearchFilter> GetFilter(IList<Filter> searchFilters)
        {
            var reader = IndexReader.Open(_directory, true);

            var filters = new List<SearchFilter>();
            foreach (var searchFilter in searchFilters)
            {
                var items = new List<SearchFilterItem>();

                var filter = new SearchFilter
                {
                    FilterName = searchFilter.FieldName,
                    Items = items
                };

                filters.Add(filter);

                var query = CreateQuery(searchFilters, searchFilter.FieldName);

                SimpleFacetedSearch search = new SimpleFacetedSearch(reader, searchFilter.FieldName);
                var facets = search.Search(query);

                foreach (var facet in facets.HitsPerFacet)
                {
                    var hitCount = facet.HitCount;
                    if (hitCount == 0)
                        continue;

                    string value = string.Empty;
                    foreach (var document in facet.Documents)
                    {
                        value = document.Get(searchFilter.FieldName);
                        if (!string.IsNullOrWhiteSpace(value))
                            break;
                    }

                    if (string.IsNullOrEmpty(value))
                        continue;

                    var isQueried =
                        searchFilter.Values.Any(s => string.Equals(s, value, StringComparison.InvariantCultureIgnoreCase));

                    var item = new SearchFilterItem
                    {
                        Label = value,
                        Value = value,
                        Hits = (int) facet.HitCount,
                        IsQueried = isQueried
                    };

                    filter.Items.Add(item);
                }

                items.Sort((s1, s2) => string.Compare(s1.Value, s2.Value, StringComparison.Ordinal));
            }

            return filters;
        }