private static IndexWriter GetWriter(String project) { Remove(); if (String.IsNullOrWhiteSpace(project)) { project = "NoneName"; } String path = LoggerModel.Getpath(project, DateTime.Now); if (m_indexWrite.ContainsKey(project)) { Interlocked.Exchange(ref m_update, 1); return(m_indexWrite[project]); } lock (m_lock) { if (m_indexWrite.ContainsKey(project)) { Interlocked.Exchange(ref m_update, 1); return(m_indexWrite[project]); } IndexWriter fsWriter = null; Boolean isExiested = File.Exists(Path.Combine(path, "write.lock")); FSDirectory fsDir = FSDirectory.Open(new DirectoryInfo(path)); Analyzer analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); //Analyzer analyser = new PanGuAnalyzer(); fsWriter = new IndexWriter(fsDir, analyser, !isExiested, IndexWriter.MaxFieldLength.UNLIMITED); m_indexWrite.TryAdd(project, fsWriter); return(fsWriter); } }
private static IndexSearcher GetSearcher(String project, DateTime dt) { Remove(); if (String.IsNullOrWhiteSpace(project)) { project = "NoneName"; } String path = LoggerModel.Getpath(project, dt); if (m_indexSearch.ContainsKey(project)) { var cacheIndex = m_indexSearch[project]; if (cacheIndex.ContainsKey(path)) { if (Interlocked.CompareExchange(ref m_update, 0, 1) == 1 && dt.Day == m_now.Day) { cacheIndex.TryRemove(path, out var re); } else { return(cacheIndex[path]); } } } lock (m_lock) { if (!m_indexSearch.ContainsKey(project)) { m_indexSearch.TryAdd(project, new ConcurrentDictionary <string, IndexSearcher>()); } var cacheIndex = m_indexSearch[project]; if (cacheIndex.ContainsKey(path)) { return(cacheIndex[path]); } bool ReadOnly = true; FSDirectory fsDir = FSDirectory.Open(new DirectoryInfo(path)); IndexSearcher searcher = new IndexSearcher(IndexReader.Open(fsDir, ReadOnly)); cacheIndex.TryAdd(path, searcher); return(searcher); } }
/// <summary> /// 搜索日志 /// </summary> /// <param name="project"></param> /// <param name="str"></param> /// <returns></returns> public static List <String> SearchData(String project, String str, DateTime dt) { if (String.IsNullOrWhiteSpace(str)) { return(null); } String path = LoggerModel.Getpath(project, dt); if (!File.Exists(Path.Combine(path, "write.lock"))) { return(null); } List <String> list = new List <String>(); try { IndexSearcher searcher = GetSearcher(project, dt); bool InOrder = true; ScoreDoc[] scoreDoc = SearchTime(searcher, str, "Content", 10, InOrder); foreach (var docs in scoreDoc) { Document doc = searcher.Doc(docs.Doc); String result = doc.Get("Content"); if (!String.IsNullOrWhiteSpace(result)) { list.Add(result); } } searcher.Dispose(); } catch (Exception e) { LogHelper.Critical("日志查询报错" + e.Message); } return(list); }