public void Update(RecordInfo record) { if (_FSDirectory == null) { _FSDirectory = FSDirectory.Open(new DirectoryInfo(_IndexDir)); } if (_IndexWriter == null) { Analyzer analyzer = new JiebaAnalyzer(); _IndexWriter = new IndexWriter(_FSDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); } lock (_IndexDir) { BooleanQuery query = new BooleanQuery() { { new TermQuery(new Term("ModuleType", record.ModuleType)), Occur.MUST }, { new TermQuery(new Term("TableName", record.TableName)), Occur.MUST }, { new TermQuery(new Term("RowId", record.RowId.ToString())), Occur.MUST } }; _IndexWriter.DeleteDocuments(query); Document document = AnalyzerDocument(record); _IndexWriter.AddDocument(document); _IndexWriter.Commit(); _IndexWriter.Optimize(); _IndexWriter.Dispose(); _FSDirectory.Dispose(); } }
public void Delete(string moduleType, string tableName, string rowID) { if (_FSDirectory == null) { _FSDirectory = FSDirectory.Open(new DirectoryInfo(_IndexDir)); } if (_IndexWriter == null) { Analyzer analyzer = new JiebaAnalyzer(); _IndexWriter = new IndexWriter(_FSDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); } lock (_IndexDir) { BooleanQuery query = new BooleanQuery() { { new TermQuery(new Term("ModuleType", moduleType)), Occur.MUST }, { new TermQuery(new Term("TableName", tableName)), Occur.MUST }, { new TermQuery(new Term("RowId", rowID)), Occur.MUST } }; _IndexWriter.DeleteDocuments(query); _IndexWriter.Commit(); _IndexWriter.Optimize(); _IndexWriter.Dispose(); _FSDirectory.Dispose(); } }
public void Add(List <RecordInfo> records) { if (_FSDirectory == null) { _FSDirectory = FSDirectory.Open(new DirectoryInfo(_IndexDir)); } if (_IndexWriter == null) { Analyzer analyzer = new JiebaAnalyzer(); _IndexWriter = new IndexWriter(_FSDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); } lock (_IndexDir) { foreach (RecordInfo record in records) { Document doc = AnalyzerDocument(record); _IndexWriter.AddDocument(doc); } _IndexWriter.Commit(); _IndexWriter.Optimize(); _IndexWriter.Dispose(); _FSDirectory.Dispose(); } }
/// <summary> /// 分词 /// </summary> /// <param name="content"></param> /// <returns></returns> static string[] SplitWords(string content) { List <string> result = new List <string>(); JiebaAnalyzer analyzer = new JiebaAnalyzer(); TokenStream tokenStream = analyzer.TokenStream("", new StringReader(content)); bool hasNext; ITermAttribute ita; while (hasNext = tokenStream.IncrementToken()) { ita = tokenStream.GetAttribute <ITermAttribute>(); result.Add(ita.Term); hasNext = tokenStream.IncrementToken(); } return(result.ToArray()); }
static void JieBaTest() { string txt = "测试一下结巴分词"; JiebaAnalyzer analyzer = new JiebaAnalyzer(); TokenStream tokenStream = analyzer.TokenStream("", new StringReader(txt)); string result = string.Empty; bool hasNext; ITermAttribute ita; while (hasNext = tokenStream.IncrementToken()) { ita = tokenStream.GetAttribute <ITermAttribute>(); result += ita.Term + " | "; hasNext = tokenStream.IncrementToken(); } Console.WriteLine(result); }