嗨,我剛剛開始在lucen.net上工作!經過大量的網上搜索,我發現了一種方法來使用它..Lucen.net只返回一個命中
我想從我的本地硬盤驅動器(D)上的txt文件中檢測到一個單詞。 我採取這樣的
string indexFileLocation = @"C:\Index";
Directory dir = FSDirectory.Open(indexFileLocation);
//create an analyzer to process the text
Analyzer analyzer = new
Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
IndexWriter indexWriter = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();
Field fldContent = new Field
("text", System.IO.File.ReadAllText(@"D:\SampleDataFile.txt"),
Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
doc.Add(fldContent);
indexWriter.AddDocument(doc);
indexWriter.Optimize();
// indexWriter.Commit();
indexWriter.Dispose();
string strIndexDir = @"C:\Index";
Analyzer std = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", std);
Query qry = parser.Parse("not");
Directory Drct = FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir));
Searcher Srch = new IndexSearcher(IndexReader.Open(Drct,true));
TopScoreDocCollector cllctr = TopScoreDocCollector.Create(100, true);
Srch.Search(qry, cllctr);
ScoreDoc[] hits = cllctr.TopDocs().ScoreDocs;
我創建的文件夾C索引...我的文本文件只包含Macavity cat lyrics
但命中計數結果我得到都是錯誤的,我試圖
word | hits
-------------------
Macavity | 1
not | 0
And | 0
eyes | 0
我試過的每一個單詞都有歌詞,但它們沒有得到點擊..除了Macavity這是給命中1,如果我在同一行或在下一行添加更多單詞「Macavity」,沒有改變在命中...總是它是1.
請別人幫我
thanx for d answer,並解釋... int results = topdoc.ScoreDocs.Length;正在給我正確的結果... – user3860465
還有一件事是我怎麼能在同一文件夾中的不同文件中搜索...? – user3860465