有图有真相

Lucene2.9.2 + 盘古分词2.3.1(一) 入门: 建立简单索引,搜索(原创)

ps:上图可以看到中文分词成功,搜索也命中了;

说明:如果想好好学Lucene建议看Lucene in action 2nd version,另外2.9.2中对以前很多方法已经废弃,旧代码就别看了;

 

下面是代码:

  • public static void IndexFile(this IndexWriter writer, IO.FileInfo file)
  • {
  •     var watch = new Stopwatch();
  •     var startTime = DateTime.Now;
  •     watch.Start();
  •     Console.WriteLine("Indexing  {0}", file.Name);
  •     writer.AddDocument(file.GetDocument());
  •     watch.Stop();
  •     var timeSpan = DateTime.Now - startTime;
  •     Console.WriteLine("Indexing Completed! Cost time {0}[{1}]", timeSpan.ToString("c"), watch.ElapsedMilliseconds);
  •  
  •   }
  •  
  • public static Document GetDocument(this IO.FileInfo file)
  • {
  •     var doc = new Document();
  •     doc.Add(new Field("contents", new IO.StreamReader(file.FullName)));
  •     doc.Add(new Field("filename", file.Name,
  •     Field.Store.YES, Field.Index.ANALYZED));
  •     doc.Add(new Field("fullpath", file.FullName,
  •     Field.Store.YES, Field.Index.NOT_ANALYZED));
  •     return doc;
  • }
  • 相关文章:

    • 2021-11-27
    • 2023-03-23
    • 2021-10-22
    • 2022-03-05
    • 2021-07-03
    • 2022-02-13
    • 2022-12-23
    猜你喜欢
    • 2022-12-23
    • 2021-05-30
    • 2022-02-04
    • 2021-11-05
    • 2022-12-23
    • 2022-12-23
    • 2022-12-23
    相关资源
    相似解决方案