zoukankan      html  css  js  c++  java
  • 基于ASP.NET的lucene.net全文搜索(一)

    在做项目的时候,需求添加全文搜索,选择了lucene.net方向,调研了一下,基本实现了需求,现在将它分享给大家。理解不深请多多包涵。

    在完成需求的时候,查看的大量的资料,本文不介绍详细的lucene.net工程建立,只介绍如何对文档进行全文搜索。对于如何建立lucene.net的工程请大家访问

    lucene.net开发

    使用lucene.net搜索分为两个部分,首先是创建索引,创建文本内容的索引,其次是根据创建的索引进行搜索。那么如何对文档进行索引呢,主要是对文档的内容进行索引,关键是提取出文档的内容,按照常规实现,由简到难,提取txt格式的文本相对比较简单,如果实现了提取txt文本,接下来就容易多了,万丈高楼平地起,这就是地基。

    1.首先创建ASP.NET页面。

    这是一个极其简单的页面,创建页面之后,双击各个按钮生成相应的点击事件,在相应的点击事件中实现程序设计。

    2.实现索引部分。

    前面已经说到了,索引主要是根据文本内容建立索引,所以要提取文本内容。创建提取txt格式文档文本内容的函数。

    View Code
    复制代码
     1 //提取txt文件
     2     public static string FileReaderAll(FileInfo fileName)
     3     {
     4         //读取文本内容,并且默认编码格式,防止出现乱码
     5         StreamReader reader = new StreamReader(fileName.FullName, System.Text.Encoding.Default);
     6         string line = "";
     7         string temp = "";
     8         //循环读取文本内容
     9         while ((line = reader.ReadLine()) != null)
    10         {
    11             temp += line;
    12         }
    13         reader.Close();
    14         //返回字符串,用于lucene.net生成索引
    15         return temp;
    16     }
    复制代码

    文本内容已经提取出来了,接下来要根据提取的内容建立索引

    View Code
    复制代码
     1 protected void Button2_Click(object sender, EventArgs e)
     2     {
     3         //判断存放文本的文件夹是否存在
     4         if (!System.IO.Directory.Exists(filesDirectory))
     5         {
     6             Response.Write("<script>alert('指定的目录不存在');</script>");
     7             return;
     8         }
     9         //读取文件夹内容
    10         DirectoryInfo dirInfo = new DirectoryInfo(filesDirectory);
    11         FileInfo[] files = dirInfo.GetFiles("*.*");
    12         //文件夹判空
    13         if (files.Count() == 0)
    14         {
    15             Response.Write("<script>alert('Files目录下没有文件');</script>");
    16             return;
    17         }
    18         //判断存放索引的文件夹是否存在,不存在创建
    19         if (!System.IO.Directory.Exists(indexDirectory))
    20         {
    21             System.IO.Directory.CreateDirectory(indexDirectory);
    22         }
    23         //创建索引
    24         IndexWriter writer = new IndexWriter(FSDirectory.Open(new DirectoryInfo(indexDirectory)),
    25                 analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
    26         
    27         for (int i = 0; i < files.Count(); i++)
    28         {
    29             string str = "";
    30             FileInfo fileInfo = files[i];
    31             //判断文件格式,为以后其他文件格式做准备
    32             if (fileInfo.FullName.EndsWith(".txt") || fileInfo.FullName.EndsWith(".xml"))
    33             {
    34                 //获取文本
    35                 str = FileReaderAll(fileInfo);
    36             }            
    37             Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
    38             doc.Add(new Lucene.Net.Documents.Field("FileName", fileInfo.Name, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
    39             //根据文本生成索引
    40             doc.Add(new Lucene.Net.Documents.Field("Content", str, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
    41             doc.Add(new Lucene.Net.Documents.Field("Path", fileInfo.FullName, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO));
    42             //添加生成的索引
    43             writer.AddDocument(doc);
    44             writer.Optimize();            
    45         }        
    46         writer.Dispose();
    47         Response.Write("<script>alert('索引创建成功');</script>");
    48     }
    复制代码

    3.索引创建完了,接下来就是搜索,搜索只要按照固定的格式书写不会出现错误。

    View Code
    复制代码
     1 protected void Button1_Click(object sender, EventArgs e)
     2     {
     3         //获取关键字
     4         string keyword = TextBox1.Text.Trim();
     5         int num = 10;
     6         //关键字判空
     7         if (string.IsNullOrEmpty(keyword))
     8         {
     9             Response.Write("<script>alert('请输入要查找的关键字');</script>");
    10             return;
    11         }
    12 
    13         IndexReader reader = null;
    14         IndexSearcher searcher = null;
    15         try
    16         {
    17             reader = IndexReader.Open(FSDirectory.Open(new DirectoryInfo(indexDirectory)), true);
    18             searcher = new IndexSearcher(reader);
    19             //创建查询
    20             PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(analyzer);
    21             wrapper.AddAnalyzer("FileName", analyzer);
    22             wrapper.AddAnalyzer("Path", analyzer);
    23             wrapper.AddAnalyzer("Content", analyzer);
    24             string[] fields = { "FileName", "Path", "Content" };
    25 
    26             QueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields, wrapper);
    27             //根据关键字查询
    28             Query query = parser.Parse(keyword);
    29                         
    30             TopScoreDocCollector collector = TopScoreDocCollector.Create(num, true);
    31 
    32             searcher.Search(query, collector);
    33             //这里会根据权重排名查询顺序
    34             var hits = collector.TopDocs().ScoreDocs;
    35 
    36             int numTotalHits = collector.TotalHits;
    37             
    38             //以后就可以对获取到的collector数据进行操作
    39             for (int i = 0; i < hits.Count(); i++)
    40             {
    41                 var hit = hits[i];
    42                 Lucene.Net.Documents.Document doc = searcher.Doc(hit.Doc);
    43                 Lucene.Net.Documents.Field fileNameField = doc.GetField("FileName");
    44                 Lucene.Net.Documents.Field pathField = doc.GetField("Path");
    45                 Lucene.Net.Documents.Field contentField = doc.GetField("Content");
    46                 //在页面循环输出表格
    47                 strTable.Append("<tr>");
    48                 strTable.Append("<td>" + fileNameField.StringValue + "</td>");
    49                 strTable.Append("</tr>");
    50                 strTable.Append("<tr>");
    51                 strTable.Append("<td>" + pathField.StringValue + "</td>");
    52                 strTable.Append("</tr>");
    53                 strTable.Append("<tr>");
    54                 strTable.Append("<td>" + contentField.StringValue.Substring(0, 300) + "</td>");
    55                 strTable.Append("</tr>");
    56             }
    57         }
    58         finally
    59         {
    60             if (searcher != null)
    61                 searcher.Dispose();
    62 
    63             if (reader != null)
    64                 reader.Dispose();
    65         }
    66     }
    复制代码

    现在整个lucene.net搜索全文的过程就建立完了,现在可以搜索txt格式的文件,搜索其他格式的文件在以后添加,主要核心思想就是提取各个不同格式文件的文本内容。

    显示效果如下:

    在以后的博文里继续接受搜索其他格式的文档。

    --------------------------------------

    欢迎您,进入 我系程序猿 的cnBlog博客。

    你不能改变你的过去,但你可以让你的未来变得更美好。一旦时间浪费了,生命就浪费了。

    You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.

    --------------------------------------

    分享到QQ空间  

  • 相关阅读:
    18.AutoMapper 之条件映射(Conditional Mapping)
    17.AutoMapper 之配置(Configuration)
    16.AutoMapper 之可查询扩展(Queryable Extensions)
    15. AutoMapper 之映射继承(Mapping Inheritance)
    14.AutoMapper 之依赖注入(Dependency Injection)
    13.AutoMapper 之映射前后(Before and After Map Action)
    12.AutoMapper 之Null替换(NullSubstitution)
    Windows 2012 系统汉化
    网站反屏蔽常见的六大方法
    Windows系统 本地文件如何复制到远程服务器
  • 原文地址:https://www.cnblogs.com/jqmtony/p/2910788.html
Copyright © 2011-2022 走看看