我正在使用 Lucene.Net 搜索上传文档的集合,并且在一个文档上测试它时效果很好但是当我添加另一个文档时,我得到的结果属于仅限最新文档,在第一份文档中搜索条款时,找不到任何结果
并且在搜索时遇到了question我遇到了同样的问题,但每次文档编入索引时他都会使用true
来重新创建索引。
这是我的搜索类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lucene.Net.Store;
using Lucene.Net.Index;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Analysis.AR;
using Lucene.Net.Search.Highlight;
/// <summary>
/// Summary description for Searcher
/// </summary>
public class Searcher
{
public Searcher()
{
//
// TODO: Add constructor logic here
//
}
private const int HITS_LIMIT = 25;
private const int MAX_FRAGMENTS_NUMBER = 3;
private Directory _Directory;
public Directory Directory
{
get
{
string path = HttpContext.Current.Server.MapPath("~/App_Data/Index");
if (_Directory == null)
_Directory = FSDirectory.Open(path);
return _Directory;
}
}
private Analyzer _Analyzer;
public Analyzer Analyzer
{
get
{
if (_Analyzer == null)
_Analyzer = new ArabicAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
return _Analyzer;
}
}
#region Mapping
private Document MapDataToLuceneDocument(Data data)
{
Document document = new Document();
document.Add(new Field("ID", data.DataID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("Path", data.Path, Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("Title", data.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("Content", data.Content, Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
return document;
}
private Data MapLuceneDocumentToData(Document document)
{
Data result = new Data()
{
DataID = int.Parse(document.Get("ID")),
Path = document.Get("Path"),
Title = document.Get("Title"),
Content = document.Get("Content"),
};
return result;
}
#endregion
#region Indexing
private void _Index(Data data, IndexWriter writer)
{
Query query = new TermQuery(new Term("ID", data.DataID.ToString()));
writer.DeleteDocuments(query);
writer.AddDocument(this.MapDataToLuceneDocument(data));
}
public void Index(IEnumerable<Data> data)
{
IndexWriter writer = null;
try
{
writer = new IndexWriter(this.Directory, this.Analyzer,false, IndexWriter.MaxFieldLength.UNLIMITED);
}
catch
{
writer = new IndexWriter(this.Directory, this.Analyzer,true, IndexWriter.MaxFieldLength.UNLIMITED);
}
foreach (var item in data)
{
this._Index(item, writer);
}
writer.Dispose();
}
public void Index(Data data)
{
this.Index(new List<Data>() { data });
}
#endregion
#region Searching
private List<Data> _Search(string searchPhrase, string searchField = "")
{
List<Data> searchResults = new List<Data>();
if (string.IsNullOrWhiteSpace(searchPhrase))
return searchResults;
QueryParser parser;
if (string.IsNullOrWhiteSpace(searchField))
{
parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new String[] { "Title", "Content" }, this.Analyzer);
}
else
{
parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, searchField, this.Analyzer);
}
Query query;
try
{
query = parser.Parse(searchPhrase.Trim());
}
catch (Exception exception)
{
query = parser.Parse(QueryParser.Escape( searchPhrase.Trim()));
}
IndexSearcher searcher = new IndexSearcher(this.Directory);
//QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "text", this.Analyzer);
//Query query_ = parser.Parse(query);
TopDocs hits = searcher.Search(query, null, Searcher.HITS_LIMIT, Sort.RELEVANCE);
foreach (var doc in hits.ScoreDocs)
{
Document document = searcher.Doc(doc.Doc);
searchResults.Add(this.MapLuceneDocumentToData(document));
}
return searchResults;
}
public List<Data> Search(string searchPhrase, string searchField = "")
{
return this._Search(searchPhrase, searchField);
}
#endregion
}
答案 0 :(得分:0)
每次拨打&#34;索引&#34;时,您可能正在删除索引。
不是每次都创建一个新的IndexWriter,而是保留一个初始化一次的成员var(我通常有一个&#34; Open&#34;方法)。
In&#34; Search&#34;使用&#34; searcher = searcher = new IndexSearcher(writer.Getreader())&#34;。
将编写器视为您的数据库,将搜索器视为针对db运行的select语句。