Lucene.net只包含最后添加的文档 [英] Lucene.net contains only last added document

查看:118
本文介绍了Lucene.net只包含最后添加的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奇怪的问题,但我每次添加一个新的文件,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>();
 

解决方案

这里:

 新的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.

这篇关于Lucene.net只包含最后添加的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆