【问题标题】:Lucene.net contains only last added documentLucene.net 仅包含最后添加的文档
【发布时间】:2010-04-14 10:07:54
【问题描述】:

这是一个奇怪的问题,但每次我向 Lucene.net 添加一个新文档时,它都会覆盖最后一个文档,因此它总是保存最后插入的文档。我已经使用 LUKE 确认了这种行为,它可以让我打开索引文件。如果有人能阐明这个问题,我将不胜感激。这是我的代码:

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;
    }
}

我添加这样的文档:

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

然后像这样搜索它:

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

【问题讨论】:

    标签: .net asp.net-mvc lucene.net


    【解决方案1】:

    true 在这里:

    new IndexWriter(indexFileLocation, analyzer, true);
    

    告诉 Lucene 创建一个新索引,删除旧索引。

    【讨论】:

    • 感谢您的回复。这应该始终为 false 还是我应该根据目录是否已经索引数据有条件地传递此值?
    • 您需要使用true 调用一次以创建索引。您可以在与文档添加代码不同的代码中执行此操作 - 执行 indexWriter = new IndexWriter(..., true); indexWriter.Close() 只是为了创建索引,然后再执行 indexWriter = new IndexWriter(..., false); 来写入它是很正常的。
    • 尝试了你的建议,它的工作方式就像一个魅力: Boolean createNewIndexFile = indexFileLocation.List().Length == 0; IndexWriter indexWriter = new IndexWriter(indexFileLocation, analyzer, createNewIndexFile);
    猜你喜欢
    • 2014-05-29
    • 2022-01-17
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    相关资源
    最近更新 更多