Lucene.net只包含最后添加的文档文档、Lucene、net

2023-09-04 23:35:45 作者:跟着风走把孤独当自由

这是一个奇怪的问题,但我每次添加一个新的文件,Lucene.net的,它会覆盖最后一个,因此它总是保持最后插入的文件。我已经证实了使用LUKE这个行为,让我打开索引文件。我倒是AP preciate如果有人能阐明问题的光。这是我的code:

 公共类SearchService:ISearchService
{
    目录indexFileLocation;
    分析仪分析仪;

    公共SearchService(字符串indexLocation)
    {
        indexFileLocation = FSDirectory.GetDirectory(indexLocation,真正的);
        分析器=新StandardAnalyzer();
    }

    公共无效AddToSearchIndex(ISearchableData数据)
    {
        IndexWriter类的IndexWriter =新的IndexWriter(indexFileLocation,分析,真正的);
        文档DOC =新的文件();

        的foreach(数据录入VAR)
        {
            场场=新领域(
                entry.Key,
                entry.Value,
                Lucene.Net.Documents.Field.Store.NO,
                Lucene.Net.Documents.Field.Index.TOKENIZED,
                Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS);

            doc.Add(场);
        }

        现场关键字段=新领域(
            SearchField.Key.ToString(),
            data.Key,
            Lucene.Net.Documents.Field.Store.YES,
            Lucene.Net.Documents.Field.Index.UN_TOKENIZED);

        文档。新增(关键字段);
        indexWriter.AddDocument(DOC);
        indexWriter.Optimize();
        indexWriter.Close();
    }

    公众的IDictionary<的Int32,浮动> SearchContent(串词)
    {
        IndexSearcher的搜索=新IndexSearcher的(indexFileLocation);
        TermQuery查询=新TermQuery(新名词(SearchField.Content.ToString()项));
        命中命中= searcher.Search(查询);
        sea​​rcher.Close();

        返回OrganizeSearchResults(点击);
    }

    公众的IDictionary<的Int32,浮动> OrganizeSearchResults(点击点击)
    {
        IDictionary的<的Int32,浮动>结果=新字典<的Int32,浮动>();
        串关键字段= SearchField.Key.ToString();

        的for(int i = 0; I< hits.Length();我++)
        {
            文档的文档= hits.Doc(ⅰ);
            场场= doc.GetField(关键字段);
            result.Add(Int32.Parse(
                field.StringValue()),
                hits.Score(ⅰ));
        }

        返回结果;
    }
}
 

我添加的文件是这样的:

 新SearchService(searchIndexFolderPath).AddToSearchIndex(entry.ToSearchableData());
 

和寻找它是这样的:

  ISearchService搜索=新SearchService(MvcApplication.SearchIndexPath);
IList的<的Int32> submissionIds = search.SearchContent(搜索关键词)。选择(命中=> hit.Key).ToList<的Int32>();
 
NET快速信息化系统开发框架 V3.2 Web版本新增 文件管理中心 非常实用的功能

解决方案

这里:

 新的IndexWriter(indexFileLocation,分析,真正的);
 

告诉Lucene来创建新的索引,删除旧的。

It's a weird problem but every time I add a new document to Lucene.net it overrides the last one and thus it always holds the last inserted document. I've confirmed this behavior using LUKE which lets me open the index files. I'd appreciate if someone could shed light on the problem. Here is my code:

public class SearchService : ISearchService
{
    Directory indexFileLocation;
    Analyzer analyzer;

    public SearchService(String indexLocation)
    {
        indexFileLocation = FSDirectory.GetDirectory(indexLocation, true);
        analyzer = new StandardAnalyzer();
    }

    public void AddToSearchIndex(ISearchableData data)
    {
        IndexWriter indexWriter = new IndexWriter(indexFileLocation, analyzer, true);
        Document    doc         = new Document();

        foreach (var entry in data)
        {
            Field field = new Field(
                entry.Key, 
                entry.Value, 
                Lucene.Net.Documents.Field.Store.NO, 
                Lucene.Net.Documents.Field.Index.TOKENIZED, 
                Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS);

            doc.Add(field);
        }

        Field keyField = new Field(
            SearchField.Key.ToString(), 
            data.Key, 
            Lucene.Net.Documents.Field.Store.YES, 
            Lucene.Net.Documents.Field.Index.UN_TOKENIZED);

        doc        .Add(keyField);
        indexWriter.AddDocument(doc);
        indexWriter.Optimize();
        indexWriter.Close();
    }

    public IDictionary<Int32, float> SearchContent(String term)
    {
        IndexSearcher searcher = new IndexSearcher(indexFileLocation);
        TermQuery     query = new TermQuery(new Term(SearchField.Content.ToString(), term));
        Hits          hits = searcher.Search(query);
        searcher.Close();

        return OrganizeSearchResults(hits);
    }

    public IDictionary<Int32, float> OrganizeSearchResults(Hits hits)
    {
        IDictionary<Int32, float> result = new Dictionary<Int32, float>();
        String keyField = SearchField.Key.ToString();

        for (int i = 0; i < hits.Length(); i++)
        {
            Document doc = hits.Doc(i);
            Field field = doc.GetField(keyField);
            result.Add(Int32.Parse(
                field.StringValue()),
                hits.Score(i));
        }

        return result;
    }
}

I add documents like this:

new SearchService(searchIndexFolderPath).AddToSearchIndex(entry.ToSearchableData());

and search for it like this:

ISearchService search = new SearchService(MvcApplication.SearchIndexPath);
IList<Int32> submissionIds = search.SearchContent(SearchTerm).Select(hit => hit.Key).ToList<Int32>();

解决方案

The true here:

new IndexWriter(indexFileLocation, analyzer, true);

tells Lucene to create a new index, deleting the old one.