Lucene.NET 实时更新索引

 

更新(添加)

 protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Boolean EnableCreate = true;

            if (System.IO.File.Exists(index + "\\segments.gen"))
            {
                EnableCreate = false;
            }

            IndexWriter writer = new IndexWriter(FSDirectory.Open(new System.IO.FileInfo(index)), new StandardAnalyzer(Version.LUCENE_CURRENT), EnableCreate);

            //   System.IO.FileInfo file = new System.IO.FileInfo(txtID.Text.Trim().Replace("\\", "\\\\"));

            System.IO.FileInfo file = new System.IO.FileInfo(@"E:\Demo\Document\ksp.txt");
            Term term = new Term("path", txtID.Text.Trim());

            Lucene.Net.Documents.Document document = FileDocument.Document(file);

            writer.UpdateDocument(term, document);
           

            writer.Close();
        }

 

删除

 protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                Directory directory = FSDirectory.Open(new System.IO.FileInfo(index));
                IndexReader reader = IndexReader.Open(directory, false);

                删除所有记录
                for (int i = 0; i < reader.MaxDoc(); i++)
                {
                    reader.DeleteDocument(i);
                }
         
                //if (rdlDel.SelectedValue == "1") //根据ID删除记录
                //{
                //    Int32 i = Convert.ToInt32(txtID.Text);
                //    reader.DeleteDocument(i);
                //    ltrResult.Text = "删除成功";
                //}
                //else if (rdlDel.SelectedValue == "2") // 根据路径删除记录
                //{
                //    Term term = new Term("path", txtID.Text.Trim());
                //    Int32 num = reader.DeleteDocuments(term);
                //    ltrResult.Text = "删除" + num.ToString() + "条记录成功";
                //}


                Response.Write(reader.MaxDoc().ToString());
                reader.Close();
                directory.Close();

            }
            catch (System.Exception ex)
            {
                ltrResult.Text = " caught a " + e.GetType() + "\n with message: " + ex.Message;
            }
        }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用C# Lucene.Net创建索引的步骤: 1.添加Lucene.Net库的引用 在Visual Studio中,右键单击项目并选择“管理NuGet程序包”。在搜索框中搜索“Lucene.Net”,然后安装Lucene.Net库。 2.创建索引 ```csharp using Lucene.Net.Analysis.Standard; using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.Store; using System.IO; // 创建索引 public void CreateIndex(string indexPath, string dataPath) { // 创建分析器 var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // 创建索引存储目录 var directory = FSDirectory.Open(new DirectoryInfo(indexPath)); // 创建索引写入器 var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); // 读取数据文件 var lines = File.ReadAllLines(dataPath); // 遍历数据文件中的每一行 foreach (var line in lines) { // 创建文档 var doc = new Document(); // 添加字段 doc.Add(new Field("content", line, Field.Store.YES, Field.Index.ANALYZED)); // 将文档写入索引 writer.AddDocument(doc); } // 关闭索引写入器 writer.Dispose(); } ``` 3.使用索引 ```csharp using Lucene.Net.Analysis.Standard; using Lucene.Net.QueryParsers; using Lucene.Net.Search; using Lucene.Net.Store; using System.IO; // 使用索引 public void SearchIndex(string indexPath, string queryStr) { // 创建分析器 var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // 创建索引存储目录 var directory = FSDirectory.Open(new DirectoryInfo(indexPath)); // 创建索引搜索器 var searcher = new IndexSearcher(directory, true); // 创建查询解析器 var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "content", analyzer); // 解析查询字符串 var query = parser.Parse(queryStr); // 执行查询 var hits = searcher.Search(query, null, 10, Sort.RELEVANCE).ScoreDocs; // 遍历查询结果 foreach (var hit in hits) { // 获取文档 var doc = searcher.Doc(hit.Doc); // 输出文档内容 Console.WriteLine(doc.Get("content")); } // 关闭索引搜索器 searcher.Dispose(); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值