Beispiel #1
0
        private static IEnumerable <Offer> _search(string searchQuery, string searchField = "")
        {
            // validation
            if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", "")))
            {
                return(new List <Offer>());
            }

            // set up lucene searcher
            using (var searcher = new IndexSearcher(_directory, true))
            {
                var hits_limit = 1000;
                //var analyzer = new StandardAnalyzer(Version.LUCENE_30);
                var analyzer = new SnowballAnalyzer(Lucene.Net.Util.Version.LUCENE_30, "English", StopAnalyzer.ENGLISH_STOP_WORDS_SET);
                searcher.SetDefaultFieldSortScoring(true, true);

                // search by single field
                if (!string.IsNullOrEmpty(searchField))
                {
                    var parser  = new QueryParser(Version.LUCENE_30, searchField, analyzer);
                    var query   = parseQuery(searchQuery, parser);
                    var hits    = searcher.Search(query, hits_limit).ScoreDocs;
                    var results = _mapLuceneToOfferList(hits, searcher);
                    analyzer.Close();
                    searcher.Dispose();
                    return(results);
                }
                // search by multiple fields (ordered by RELEVANCE)
                else
                {
                    var parser  = new MultiFieldQueryParser(Version.LUCENE_30, _fields.Keys.ToArray(), analyzer);
                    var query   = parseQuery(searchQuery, parser);
                    var hits    = searcher.Search(query, null, hits_limit, Sort.RELEVANCE).ScoreDocs;
                    var results = _mapLuceneToOfferList(hits, searcher);
                    analyzer.Close();
                    searcher.Dispose();
                    return(results);
                }
            }
        }
Beispiel #2
0
        public static void AddUpdateLuceneIndex(IEnumerable <Offer> offers)
        {
            // init lucene
            //var analyzer = new StandardAnalyzer(Version.LUCENE_30);
            var analyzer = new SnowballAnalyzer(Lucene.Net.Util.Version.LUCENE_30, "English", StopAnalyzer.ENGLISH_STOP_WORDS_SET);

            using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // add data to lucene search index (replaces older entry if any)
                foreach (var offer in offers)
                {
                    _addToLuceneIndex(offer, writer);
                }

                // close handles
                analyzer.Close();
                writer.Dispose();
            }
        }