private static void UpdateIndex(List <string> folderParseResults, string indexFolderPath) { Console.WriteLine(" > Adding items to index ..."); FSDirectory directory = null; Analyzer analyzer = null; IndexWriter indexWriter = null; try { directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexFolderPath)); analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); } catch (Exception ex) { throw new ApplicationException("NOTE!", ex); } foreach (string item in folderParseResults) { Document document = new Document(); document.Add(new Field("FilePathString", item, Field.Store.YES, Field.Index.ANALYZED)); indexWriter.AddDocument(document); } indexWriter.Optimize(); indexWriter.Dispose(); analyzer.Dispose(); directory.Dispose(); }
public void Dispose() { isClosing = true; CloseWriter(); CloseReader(); try { if (index != null) { index.Dispose(); } } catch (Exception ex) { Log.ErrorFormat("Failed to dispose Lucene search index.", ex); } try { if (analyzer != null) { analyzer.Close(); analyzer.Dispose(); } } catch (Exception ex) { Log.ErrorFormat("Failed to close/dispose Lucene search analyzer.", ex); } }
public static void CloseAll() { logger.Debug("Lucene: CloseAll"); if (writer != null) { logger.Debug("Lucene: CloseAll - Writer is alive"); writer.Flush(true, true, true); writer.Commit(); writer.WaitForMerges(); writer.Dispose(); writer = null; } if (analyzer != null) { logger.Debug("Lucene: CloseAll - Analyzer is alive"); analyzer.Close(); analyzer.Dispose(); analyzer = null; } if (searcher != null) { logger.Debug("Lucene: CloseAll - Searcher is alive"); searcher.Dispose(); searcher = null; } if (directory != null) { logger.Debug("Lucene: CloseAll - Directory is alive"); directory.Dispose(); directory = null; } }
public void Dispose() { Info("Lucene Disposal."); _indexWriter.Dispose(); _indexDirectory.Dispose(); _standardAnalyzer.Close(); _standardAnalyzer.Dispose(); }
public void Close() { _analyzer.Close(); _analyzer.Dispose(); _searcher.Dispose(); _directory.Dispose(); _logger.Info("Searcher is closed."); }
/// <summary> /// The main lucene search method. /// </summary> /// <param name="searchQuery"></param> /// <param name="searchField"></param> /// <returns></returns> private IList <T> _search(string searchQuery, string searchField = "", int hitlimit = 30, int page = 1) { // validation if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) { return(new List <T>()); } // set up lucene searcher using (IndexReader reader = DirectoryReader.Open(_fluentIndex.Directory)) { var searcher = new IndexSearcher(reader); var analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48); // search by single field if (!string.IsNullOrEmpty(searchField)) { var parser = new QueryParser(LuceneVersion.LUCENE_48, searchField, analyzer); var query = _parseQuery(searchQuery, parser); var hits = searcher.Search(query, hitlimit).ScoreDocs; var results = _mapLuceneToDataList(hits, searcher); analyzer.Dispose(); return(results); } // search by multiple fields (ordered by RELEVANCE) else { var parser = new MultiFieldQueryParser( LuceneVersion.LUCENE_48, _searchConfig.AnalyzedFields.Concat(_searchConfig.NonAnalyzedFields).ToArray(), analyzer ); var query = _parseQuery(searchQuery, parser); var skip = hitlimit * page; var hits = searcher.Search(query, null, hitlimit + skip, Sort.RELEVANCE).ScoreDocs; var pagedHits = hits.Skip(skip); var results = _mapLuceneToDataList(pagedHits, searcher); analyzer.Dispose(); return(results); } } }
public void Dispose() { Info("Lucene Committing."); _indexWriter.Commit(); Info("Lucene Optimizing."); _indexWriter.Optimize(); _indexWriter.Dispose(); _indexDirectory.Dispose(); _standardAnalyzer.Close(); _standardAnalyzer.Dispose(); }
public void AnIndexCanBeCreated() { TestDataGenerator tdg = new TestDataGenerator(); Directory directory = new RAMDirectory(); Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48); _indexer = new LuceneIndexer(directory, analyzer); _indexer.CreateIndex(tdg.AllData); Assert.Equal(2000, _indexer.Count()); analyzer.Dispose(); directory.ClearLock("write.lock"); directory.Dispose(); }
public void Dispose() { CloseWriter(); CloseReader(); if (index != null) { index.Dispose(); } if (analyzer != null) { analyzer.Close(); analyzer.Dispose(); } }
internal static void SearchIndex(string indexFolderPath, string searchTerm) { IndexReader reader; Analyzer analyzer; Searcher searcher; try { FSDirectory directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexFolderPath)); reader = IndexReader.Open(directory, true); analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); searcher = new IndexSearcher(reader); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "FilePathString", analyzer); Query query = parser.Parse(searchTerm); TopScoreDocCollector collector = TopScoreDocCollector.Create(100, true); searcher.Search(query, collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; foreach (ScoreDoc scoreDoc in hits) { Document document = searcher.Doc(scoreDoc.Doc); Console.WriteLine(document.Get("FilePathString")); } reader.Dispose(); searcher.Dispose(); analyzer.Dispose(); } catch (ParseException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { throw new ApplicationException("NOTE!", ex); } finally { } }
public static bool ClearLuceneIndex() { try { var directory = FSDirectory.Open(new DirectoryInfo(_path)); var analyzer = new StandardAnalyzer(_version); using (var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED)) { writer.DeleteAll(); writer.Commit(); analyzer.Dispose(); writer.Dispose(); directory.Dispose(); } } catch (Exception) { return(false); } return(true); }
/// <summary> /// Searches the index. /// </summary> /// <param name="totalHits">The total hits.</param> /// <param name="forumId">The forum identifier.</param> /// <param name="userId">The user identifier.</param> /// <param name="searchQuery">The search query.</param> /// <param name="searchField">The search field.</param> /// <param name="pageIndex">Index of the page.</param> /// <param name="pageSize">Size of the page.</param> /// <returns> /// Returns the Search results /// </returns> private List <SearchMessage> SearchIndex( out int totalHits, int forumId, int userId, string searchQuery, string searchField = "", int pageIndex = 1, int pageSize = 1000) { if (searchQuery.Replace("*", string.Empty).Replace("?", string.Empty).IsNotSet()) { totalHits = 0; return(new List <SearchMessage>()); } // Insert forum access here var userAccessList = this.GetRepository <vaccess>().Get(v => v.UserID == userId); // filter forum if (forumId > 0) { userAccessList = userAccessList.FindAll(v => v.ForumID == forumId); } var searcher = this.GetSearcher(); if (searcher == null) { totalHits = 0; return(new List <SearchMessage>()); } var hitsLimit = this.Get <BoardSettings>().ReturnSearchMax; // 0 => Lucene error; if (hitsLimit == 0) { hitsLimit = pageSize; } var analyzer = new StandardAnalyzer(MatchVersion); var formatter = new SimpleHTMLFormatter("<mark>", "</mark>"); var fragmenter = new SimpleFragmenter(hitsLimit); QueryScorer scorer; // search by single field if (searchField.IsSet()) { var parser = new QueryParser(MatchVersion, searchField, analyzer); var query = ParseQuery(searchQuery, parser); scorer = new QueryScorer(query); var hits = searcher.Search(query, hitsLimit).ScoreDocs; totalHits = hits.Length; var highlighter = new Highlighter(formatter, scorer) { TextFragmenter = fragmenter }; var results = this.MapSearchToDataList( highlighter, analyzer, searcher, hits, pageIndex, pageSize, userAccessList); analyzer.Dispose(); return(results); } else { var parser = new MultiFieldQueryParser( MatchVersion, new[] { "Message", "Topic", this.Get <BoardSettings>().EnableDisplayName ? "AuthorDisplay" : "Author" }, analyzer); var query = ParseQuery(searchQuery, parser); scorer = new QueryScorer(query); // sort by date var sort = new Sort(new SortField("MessageId", SortFieldType.STRING, true)); var hits = searcher.Search(query, null, hitsLimit, sort).ScoreDocs; totalHits = hits.Length; var highlighter = new Highlighter(formatter, scorer) { TextFragmenter = fragmenter }; var results = this.MapSearchToDataList( highlighter, analyzer, searcher, hits, pageIndex, pageSize, userAccessList); this.searcherManager.Release(searcher); return(results); } }
public void Dispose() { _indexDir?.Dispose(); _searcher?.Dispose(); _analyzer?.Dispose(); }
private static Results <T> GetResults <T>(string query, string type) { string didYouMean; TopDocs topDocs; query = query.Trim().ToLower().Replace("'", "").Replace("-", " "); Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); Directory directory = FSDirectory.Open(new System.IO.DirectoryInfo(IndexDirectory + type)); string[] fields = new string[] { "Search" }; MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, fields, analyzer); BooleanQuery booleanQuery = new BooleanQuery(); string[] searchWords = query.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); string[] didYouMeans = new string[searchWords.Length]; didYouMean = string.Empty; foreach (string word in searchWords) { if (word != "&") { booleanQuery.Add(parser.Parse(QueryParser.Escape(word).Replace("~", "") + "~"), Occur.MUST); } } IndexReader reader = IndexReader.Open(directory, true); Searcher searcher = new IndexSearcher(reader); topDocs = searcher.Search(booleanQuery, reader.MaxDoc); ScoreDoc[] scoreDocs = topDocs.ScoreDocs; bool exactMatch = false; List <string> results = new List <string>(); for (int i = 0; i < scoreDocs.Length; i++) { int doc = scoreDocs[i].Doc; float score = scoreDocs[i].Score; DateTime date = DateTime.Now; Document document = searcher.Doc(doc); results.Add(document.Get("Id")); if (!exactMatch) { string input = document.Get("Search").ToLower(); bool match = true; for (int j = 0; j < searchWords.Length; j++) { string word = searchWords[j]; if (Regex.IsMatch(input, @"(^|\s|\b)" + Regex.Escape(word) + @"(\b|\s|$)", RegexOptions.IgnoreCase)) { if (didYouMeans[j] != word) { didYouMeans[j] = word; } } else { match = false; if (string.IsNullOrEmpty(didYouMeans[j])) { didYouMeans[j] = SuggestSimilar(word, type); } } } exactMatch = match; } } query = string.Join(" ", searchWords); if (!exactMatch) { didYouMean = string.Join(" ", didYouMeans); } query = string.Join(" ", searchWords); if (!exactMatch) { didYouMean = string.Join(" ", didYouMeans); } Results <T> model = new Results <T> { DidYouMean = didYouMean, Ids = results.ToList(), TotalHits = topDocs.TotalHits }; reader.Dispose(); searcher.Dispose(); directory.Dispose(); analyzer.Dispose(); return(model); }
public void StartLuceneIndexCreateProcess() { string sqlConnStr = @ConfigurationManager.AppSettings["SqlConnectionString"]; string sqlCmd = "select ID, PLC_ID, ZIP_CD, FRMTD_ADDR, NELAT, NELON, SWLAT, SWLON FROM GEO_LCTN "; SqlConnection sqlConn = new SqlConnection(sqlConnStr); string luceneIndexStoragePath = @ConfigurationManager.AppSettings["LuceneIndexStoragePath"]; bool folderExists = System.IO.Directory.Exists(luceneIndexStoragePath); if (!folderExists) { System.IO.Directory.CreateDirectory(luceneIndexStoragePath); } StandardAnalyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo(luceneIndexStoragePath)); IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED); try { // We will populate below list to create Lucene index. var locationList = new List <LocationModel>(); sqlConn.Open(); using (var cmd = new SqlCommand(sqlCmd, sqlConn)) { var sqlReader = cmd.ExecuteReader(); while (sqlReader.Read()) { locationList.Add(new LocationModel { ID = Convert.ToInt16(sqlReader["ID"]), PLC_ID = sqlReader["PLC_ID"].ToString(), ZIP_CD = sqlReader["ZIP_CD"].ToString(), FRMTD_ADDR = sqlReader["FRMTD_ADDR"].ToString(), NELAT = sqlReader["NELAT"].ToString(), NELON = sqlReader["NELON"].ToString(), SWLAT = sqlReader["SWLAT"].ToString(), SWLON = sqlReader["SWLON"].ToString() }); } } foreach (var item in locationList) { writer.AddDocument(CreateDocument(item)); } } catch { IndexWriter.Unlock(directory); throw; } finally { writer.Optimize(); analyzer.Close(); writer.Dispose(); analyzer.Dispose(); } }