public static IEnumerable<LuceneSearchEO> Search(LuceneSearchType searchType, string input, string fieldName = "") { if (string.IsNullOrEmpty(input)) return new List<LuceneSearchEO>(); string[] temrs = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); input = string.Empty; foreach (var item in temrs) input += "+" + item + " "; return _search(searchType, input.Trim(), fieldName); }
public static IEnumerable<LuceneSearchEO> GetAllIndexRecords(LuceneSearchType searchType) { string _luceneDir = getLuceneDirectory(searchType); if (!System.IO.Directory.EnumerateFiles(_luceneDir).Any()) return new List<LuceneSearchEO>(); // set up lucene searcher var searcher = new IndexSearcher(getFSDirectory(searchType), false); var reader = IndexReader.Open(getFSDirectory(searchType), false); var docs = new List<Document>(); var term = reader.TermDocs(); // v 2.9.4: use 'term.Doc()' // v 3.0.3: use 'term.Doc' while (term.Next()) docs.Add(searcher.Doc(term.Doc)); reader.Dispose(); searcher.Dispose(); return _mapLuceneToDataList(docs); }
private static IEnumerable<LuceneSearchEO> _search(LuceneSearchType searchType, string searchQuery, string searchField = "") { // validation if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<LuceneSearchEO>(); // set up lucene searcher using (var searcher = new IndexSearcher(getFSDirectory(searchType), false)) { var hits_limit = 20; var analyzer = new StandardAnalyzer(Version.LUCENE_30); // 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 = _mapLuceneToDataList(hits, searcher); analyzer.Close(); searcher.Dispose(); return results; } // search by multiple fields (ordered by RELEVANCE) else { string[] fields = getLuceneSearchEOFields(); var parser = new MultiFieldQueryParser (Version.LUCENE_30, fields, analyzer); var query = parseQuery(searchQuery, parser); var hits = searcher.Search(query, null, hits_limit, Sort.INDEXORDER).ScoreDocs; var results = _mapLuceneToDataList(hits, searcher); analyzer.Close(); searcher.Dispose(); return results; } } }
public static bool ClearLuceneIndex(LuceneSearchType searchType) { try { var analyzer = new StandardAnalyzer(Version.LUCENE_30); using (var writer = new IndexWriter(getFSDirectory(searchType), analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED)) { // remove older index entries writer.DeleteAll(); // close handles analyzer.Close(); writer.Dispose(); } } catch (Exception) { return false; } return true; }
public static void Optimize(LuceneSearchType searchType) { var analyzer = new StandardAnalyzer(Version.LUCENE_30); using (var writer = new IndexWriter(getFSDirectory(searchType), analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { analyzer.Close(); writer.Optimize(); writer.Dispose(); } }
public static void ClearLuceneIndexRecord(LuceneSearchType searchType, int record_id) { // init lucene var analyzer = new StandardAnalyzer(Version.LUCENE_30); using (var writer = new IndexWriter(getFSDirectory(searchType), analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { LuceneSearchEO LuceneSearchEO = new LuceneSearchEO(); PropertyInfo[] props = LuceneSearchEO.GetType().GetProperties(); foreach (var prop in props) { Attribute att = prop.GetCustomAttribute(typeof(LuceneTypeAttribute)); LuceneTypeAttribute luceneAtt = (LuceneTypeAttribute)att; if (luceneAtt != null && luceneAtt.Key) { // remove older index entry var searchQuery = new TermQuery(new Term(prop.Name, record_id.ToString())); writer.DeleteDocuments(searchQuery); } } // close handles analyzer.Close(); writer.Dispose(); } }
private static FSDirectory getFSDirectory(LuceneSearchType searchType) { string _luceneDir = getLuceneDirectory(searchType); FSDirectory _directoryTemp = FSDirectory.Open(new DirectoryInfo(_luceneDir)); if (IndexWriter.IsLocked(_directoryTemp)) IndexWriter.Unlock(_directoryTemp); var lockFilePath = Path.Combine(_luceneDir, "write.lock"); if (File.Exists(lockFilePath)) File.Delete(lockFilePath); return _directoryTemp; }
public static void AddUpdateLuceneIndex(LuceneSearchType searchType, IEnumerable<LuceneSearchEO> LuceneSearchEOs) { string _luceneDir = getLuceneDirectory(searchType); // init lucene var analyzer = new StandardAnalyzer(Version.LUCENE_30); using (var writer = new IndexWriter(getFSDirectory(searchType), analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { // add data to lucene search index (replaces older entries if any) foreach (var LuceneSearchEO in LuceneSearchEOs) _addToLuceneIndex(LuceneSearchEO, writer); // close handles analyzer.Close(); writer.Dispose(); } }
public static void AddUpdateLuceneIndex(LuceneSearchType searchType, LuceneSearchEO LuceneSearchEO) { AddUpdateLuceneIndex(searchType, new List<LuceneSearchEO> { LuceneSearchEO }); }
private static string getLuceneDirectory(LuceneSearchType searchType) { string lucenePath = HttpContext.Current.Request.PhysicalApplicationPath + "Lucene_Index\\"; DirectoryInfo diLucenePath = new DirectoryInfo(lucenePath); if (!diLucenePath.Exists) diLucenePath.Create(); string _luceneDir = Path.Combine(lucenePath + searchType.ToString() + "_index"); diLucenePath = new DirectoryInfo(_luceneDir); if (!diLucenePath.Exists) diLucenePath.Create(); return _luceneDir; }