#define Search
usingLucene.Net.Analysis;usingLucene.Net.Analysis.Tokenattributes;usingLucene.Net.Documents;usingLucene.Net.Index;usingLucene.Net.QueryParsers;usingLucene.Net.Search;usingLucene.Net.Store;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingTestApp.Bll;usingTestApp.Model;namespaceTestApp
{classProgram
{static voidMain()
{#if CreateIndexConsole.WriteLine("開始建立索引");var bll = newItemBll();
CreateIndex(bll.GetItemInfos());#endif
#if Search
#region 查詞StringBuilder sb= newStringBuilder();//索引庫目錄
Lucene.Net.Store.Directory dir_search = FSDirectory.Open(new System.IO.DirectoryInfo("ItemIndexDir"), newNoLockFactory());
IndexReader reader= IndexReader.Open(dir_search, true);
IndexSearcher search= null;try{
search= newIndexSearcher(reader);
QueryParser parser= new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "ItemName", newPanGuAnalyzer());
Query query= parser.Parse(LuceneHelper.GetKeyWordSplid("甲醇"));//執行搜尋,擷取查詢結果集對象
TopDocs ts = search.Search(query, null, 20000);///擷取命中的文檔資訊對象
ScoreDoc[] docs =ts.ScoreDocs;
Console.WriteLine(docs.Length);for (int i = 0; i < docs.Length; i++)
{int docId =docs[i].Doc;
Document doc=search.Doc(docId);var id = doc.Get("id");
Console.WriteLine(id);var itemName = doc.Get("ItemName");
Console.WriteLine(itemName);var purity = doc.Get("Purity");
Console.WriteLine(purity);var size = doc.Get("Size");
Console.WriteLine(size);var unit = doc.Get("Unit");
Console.WriteLine(unit);var venderName = doc.Get("VenderName");
Console.WriteLine(venderName);
}
}catch(Exception ex)
{throw;
}finally{if (search != null)
search.Dispose();if (dir_search != null)
dir_search.Dispose();
}#endregion
#endif}//幫助類,對搜尋的關鍵詞進行分詞
public static classLuceneHelper
{public static string GetKeyWordSplid(stringkeywords)
{
StringBuilder sb= newStringBuilder();
Analyzer analyzer= newPanGuAnalyzer();
TokenStream stream= analyzer.TokenStream(keywords, newStringReader(keywords));
ITermAttribute ita= null;bool hasNext =stream.IncrementToken();while(hasNext)
{
ita= stream.GetAttribute();
sb.Append(ita.Term+ " ");
hasNext=stream.IncrementToken();
}returnsb.ToString();
}
}///
///建立索引檔案///
private static void CreateIndex(Listlist)
{
IndexWriter writer= null;
Analyzer analyzer= newPanGuAnalyzer();
Lucene.Net.Store.Directory dir= FSDirectory.Open(new System.IO.DirectoryInfo("ItemIndexDir"));int i = 0;try{IndexReader:對索引進行讀取的類。
//該語句的作用:判斷索引庫檔案夾是否存在以及索引特征檔案是否存在。
bool isCreate = !IndexReader.IndexExists(dir);
writer= newIndexWriter(dir, analyzer, isCreate, IndexWriter.MaxFieldLength.UNLIMITED);//添加索引
foreach (var item inlist)
{
Document doc= newDocument();if (item.ItemId % 1000 == 0)
Console.WriteLine($"開始寫入{item.ItemId}");
doc.Add(new Field("id", item.ItemId.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i= 1;
doc.Add(new Field("ItemName", item.ItemName?.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
i= 2;
doc.Add(new Field("Purity", item.Purity?.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i= 3;
doc.Add(new Field("Size", item.Size.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i= 4;
doc.Add(new Field("Unit", item.Unit?.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i= 5;
doc.Add(new Field("VenderName", item.VenderName?.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
i= 6;
doc.Add(new Field("Price", item.Price.ToString(), Field.Store.YES, Field.Index.ANALYZED));
i= 7;
writer.AddDocument(doc, analyzer);
}
writer.Optimize();
}catch(Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine($"error step {i}");throw;
}finally{if (writer != null)
writer.Dispose();if (dir != null)
dir.Dispose();
}
}
}
}