lucene.net索引中的重复文档
本文关键字:文档 net 索引 lucene | 更新日期: 2023-09-27 18:05:12
我正在使用lucene.net索引我的pdf文件。刷新索引后,它将多次显示相同的文档(=我刷新索引的时间)。
我正在使用最新版本的lucene.net索引(lucene.net 3.0.3)。
这是我的代码索引。
public void refreshIndexes()
{
// Create Index Writer
string strIndexDir = @"Z:'Munavvar'LuceneTest'index";
IndexWriter writer = new IndexWriter(Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir)), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, IndexWriter.MaxFieldLength.UNLIMITED);
writer.DeleteAll();
// Find all files in root folder create index on them
List<string> lstFiles = searchFiles(@"Z:'Munavvar'LuceneTest'PDFs");
foreach (string strFile in lstFiles)
{
Document doc = new Document();
string FileName = System.IO.Path.GetFileNameWithoutExtension(strFile);
string Text = ExtractTextFromPdf(strFile);
string Path = strFile;
string ModifiedDate = Convert.ToString(File.GetLastWriteTime(strFile));
string DocumentType = string.Empty;
string Vault = string.Empty;
string headerText = Text.Substring(0, Text.Length < 150 ? Text.Length : 150);
foreach (var docs in ltDocumentTypes)
{
if (headerText.ToUpper().Contains(docs.searchText.ToUpper()))
{
DocumentType = docs.DocumentType;
Vault = docs.VaultName; ;
}
}
if (string.IsNullOrEmpty(DocumentType))
{
DocumentType = "Default";
Vault = "Default";
}
doc.Add(new Field("filename", FileName, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("text", Text, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("path", Path, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("modifieddate", ModifiedDate, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("documenttype", DocumentType, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("vault", Vault, Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(doc);
}
writer.Optimize();
writer.Dispose();
}
这是我的索引搜索代码
public List<IndexDocument> searchFromIndexes(string searchText)
{
try
{
#region search in indexes and fill list
// Create list
List<IndexDocument> searchResult = new List<IndexDocument>();
if (!string.IsNullOrEmpty(searchText))
{
string strIndexDir = @"Z:'Munavvar'LuceneTest'index";
var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
IndexSearcher searcher = new IndexSearcher(Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir)));
// parse the query, "text" is the default field to search
Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", analyzer);
Query query = parser.Parse(searchText);
// search
TopDocs hits = searcher.Search(query, searcher.MaxDoc);
// showing first TotalHits results
for (int i = 0; i < hits.TotalHits; i++)
{
// get the document from index
Document doc = searcher.Doc(hits.ScoreDocs[i].Doc);
// create a new row with the result data
searchResult.Add(new IndexDocument()
{
FileName = doc.Get("filename"),
Text = doc.Get("text"),
Path = doc.Get("path"),
ModifiedDate = doc.Get("modifieddate"),
Vault = doc.Get("vault"),
DocumentType = doc.Get("documenttype"),
});
}
searcher.Dispose();
}
return searchResult;
#endregion
}
catch (Exception ex)
{
throw ex;
}
}
我有一个按钮的窗口,调用refreshIndexes方法。
当我关闭并再次运行应用程序并单击该按钮时,它将清除旧索引
想个办法。
问题:我从全局类对象调用refreshIndexes方法。
VaultIndexes vIndexes = new VaultIndexes();
private void btnRefreshIndex_Click(object sender, RoutedEventArgs e)
{
vIndexes.refreshIndexes();
}
解决方案:每次创建一个新对象。
private void btnRefreshIndex_Click(object sender, RoutedEventArgs e)
{
VaultIndexes vIndexes = new VaultIndexes();
vIndexes.refreshIndexes();
}
我不知道为什么它是用全局类创建重复的文档对象。
正如@RichaGarg在评论中所说,它不能创建新文档根据IndexWriter
的第三个参数
IndexWriter writer = new IndexWriter(Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir)), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, IndexWriter.MaxFieldLength.UNLIMITED);