Esempio n. 1
0
        public static SearchResult MultiSearch(string term, List<FacetMatch> filters)
        {
            var result = new SearchResult();

            if (_directory == null)
            {
                _directory = new RAMDirectory(FSDirectory.Open(@"c:\test\luceneindex"));
            }

            //var reader = IndexReader.Open(FSDirectory.Open(@"c:\test\luceneindex"), true);
            var reader = IndexReader.Open(_directory, true);

            var searcher = new FacetSearcher(reader);
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            var queryParser = new QueryParser(Version.LUCENE_30, "name", analyzer);
            queryParser.AllowLeadingWildcard = true;

            var query = queryParser.Parse("*" + term + "*");
            query = CreateFacetedQuery(query, filters, "");

            //var query = new TermQuery(new Term(term + "*"));
            var facetFields = new List<FacetFieldInfo>();
            var db = new Database();

            facetFields.Add(new FacetFieldInfo()
            {
                FieldName = "category"
            });

            foreach (var specType in db.GetSpecTypes())
            {
                facetFields.Add(new FacetFieldInfo()
                {
                    FieldName = specType,
                    Selections = filters.Where(f => f.FacetName == specType).Select(f => f.FacetValue).ToList()
                });
            }

            var actual = searcher.SearchWithFacets(query, 200, facetFields);

            if (actual.Hits.TotalHits > 0)
            {
                foreach (var hit in actual.Hits.ScoreDocs)
                {
                    var doc = searcher.Doc(hit.Doc);

                    result.Products.Add(new Product()
                    {
                        Name = doc.GetField("name").StringValue,
                        Id = int.Parse(doc.GetField("id").StringValue)
                    });
                }

                foreach (var facet in actual.Facets)
                {
                    result.Facets.Add(new FacetMatch()
                    {
                        FacetName = facet.FacetFieldName,
                        FacetValue = facet.Value,
                        HitCount = facet.Count
                    });
                }
            }

            result.Facets = result.Facets.OrderBy(f => f.FacetName).ThenBy(f => f.FacetValue).ToList();
            var groups = result.Facets.GroupBy(g => g.FacetName);
            return result;
        }
Esempio n. 2
0
        public static void RebuildIndex()
        {
            var db = new Database();
            var data = db.GetProducts();
            var cats = db.GetProductCategories();
            var specs = db.GetProductSpecs();
            var directory = FSDirectory.Open(@"c:\test\LuceneIndex");
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            var indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);

            foreach (var p in data)
            {
                var doc = new Document();
                doc.Add(new Field("id", p.Id.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("name", p.StoreName(), Field.Store.YES, Field.Index.ANALYZED));

                foreach (var c in cats.Where(c => c.ProductId == p.Id))
                {
                    doc.Add(new Field("category", c.CategoryId.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                }

                foreach (var spec in specs.Where(s => s.ProductId == p.Id))
                {
                    doc.Add(new Field(spec.SpecType, spec.Value, Field.Store.YES, Field.Index.ANALYZED));
                }

                indexWriter.AddDocument(doc);
            }

            indexWriter.Optimize();
            indexWriter.Dispose();
        }