Lucene.Net仅从上次添加的文档中获取结果

本文关键字:文档 获取 结果 添加 Net Lucene | 更新日期: 2023-09-27 17:58:33

我使用的是Lucene。Net搜索上传的文档集合,在一个文档上测试时效果很好,但当我添加另一个文档时,我得到的结果只属于最新的文档,在第一个文档中搜索术语时,找不到结果
在搜索时,我遇到了一个问题,怎么会有同样的问题,但他每次索引文档时都使用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
}

Lucene.Net仅从上次添加的文档中获取结果

很可能每次调用"index"时都会删除索引。

与其每次都创建一个新的IndexWriter,不如保留一个初始化一次的成员var(我通常有一个"Open"方法)。

在"搜索"中使用"searcher=searcher=new IndexSearcher(writer.Getreader())"。

将编写器视为数据库,将搜索器视为针对数据库运行的select语句。