最近想提高下自己的能力,也是由于自己的项目中需要用到Lucene,所以开始接触这门富有挑战又充满新奇的技术。。
刚刚开始,只是写了个小小的demo,用了用lucene,确实很好
 
创建索引
DataTable dt = DB.SqlHelper.ExecuteDataset(connectionString, CommandType.Text, "select top 1000 id,title,productsummary from dbo.products").Tables[0];
 
            Lucene.Net.Store.FSDirectory fs = Lucene.Net.Store.FSDirectory.GetDirectory(basePath);
 
            if (Lucene.Net.Index.IndexReader.IsLocked(fs))
                Lucene.Net.Index.IndexReader.Unlock(fs);
 
            Lucene.Net.Index.IndexWriter iw = new Lucene.Net.Index.IndexWriter(basePath, new Lucene.Net.Analysis.Standard.StandardAnalyzer());
            foreach (DataRow dr in dt.Rows)
            {
                Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
                doc.Add(new Lucene.Net.Documents.Field("id", dr["id"].ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO));
                doc.Add(new Lucene.Net.Documents.Field("title", dr["title"].ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
                doc.Add(new Lucene.Net.Documents.Field("productsummary", dr["productsummary"].ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
                iw.AddDocument(doc);
            }
            iw.Optimize();
            iw.Close();
 
搜索结果
System.Text.StringBuilder sb = new System.Text.StringBuilder();
            Lucene.Net.Store.FSDirectory dir = Lucene.Net.Store.FSDirectory.GetDirectory(basePath);
            Lucene.Net.Analysis.PanGu.PanGuAnalyzer pgAnalyzer = new Lucene.Net.Analysis.PanGu.PanGuAnalyzer();
 
            Lucene.Net.Search.IndexSearcher search = new Lucene.Net.Search.IndexSearcher(dir);
            Lucene.Net.QueryParsers.QueryParser qp = new Lucene.Net.QueryParsers.QueryParser("title", pgAnalyzer);
            Lucene.Net.Search.Query query = qp.Parse(this.txtKeywords.Text);
          
            //第一种结果,使用Hits集合
            Lucene.Net.Search.Hits hits = search.Search(query);
            for (int i = 0; i < hits.Length(); i++)
            {
                Lucene.Net.Documents.Document doc = hits.Doc(i);
                sb.Append(doc.GetField("title").StringValue()+"<br />");
            }
           
            //第二种结果,使用TopDocs(最精确的结果)
           Lucene.Net.Search.TopDocs topDocs = search.Search(query, 100);//100为返回的结果数目,必须是大于0的数字,分页的时候会用到
            Lucene.Net.Search.ScoreDoc[] result = topDocs.scoreDocs;
            for (int i = 0; i < result.Length; i++)
            {
                Lucene.Net.Documents.Document doc = search.Doc(result[i].doc);
                sb.Append((i+1).ToString() + "\t" + highter.GetBestFragment(this.txtKeywords.Text, doc.Get("title")) + "<br />");
            }
 
 
            Response.Write(sb.ToString());
            dir.Close();
 
结果
 

自己写的第一个小例子,当出现结果的那一刹那,瞬间充满了惊喜,因为自己调试了很久才出现结果,真的对自己也是一种鼓励,坚持下去,以后还会继续深入研究lucene。。。。

初识Lucene.net的更多相关文章

  1. 初识 Lucene

    Lucene是一个信息检索工具库,而不是一个完整的搜索程序 搜索程序 Lucene索引核心类 Lucene索引核心类: Document: 文档对象代表一些域(field)的集合 Field: 每个文 ...

  2. 第一章 初识Lucene

    多看几遍,慢就是快 1.1 应对信息爆炸 1.2 Lucene 是什么 1.2.1 Lucene 能做些什么 1.2.2 Lucene 的历史 1.3 Lucene 和搜索程序组件 基本概念 索引操作 ...

  3. 初识lucene

    lucene的介绍网上有好多,再写一遍可能有点多余了. 使用lucene之前,有一系列的疑问 为什么lucene就比数据库快? 倒排索引是什么,他是怎么做到的 lucene的数据结构是什么样的,cpu ...

  4. 初识lucene(想看代码的跳过)

    最早是在百度贴吧里看到的lucene这个名称,只知道跟搜索引擎有关,因为工作中一直以来没有类似的需求,所以没有花时间学习这方面的知识. 刚过完年,公司不忙,自己闲不住把<Netty权威指南> ...

  5. 1. 初识 Lucene

    在学习Lucene之前呢,我们当然首先要了解下什么是Lucene. 0x01 什么是Lucene ? Lucene是一套用于全文检索和搜索的开放源代码程序库,由Apache软件基金会支持和提供. Lu ...

  6. (转)初识 Lucene

    Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能.Lucene 目前是 Apache Jakarta 家族中的一个开源项目. ...

  7. 实战 Lucene,第 1 部分: 初识 Lucene (zhuan)

    http://www.ibm.com/developerworks/cn/Java/j-lo-lucene1/ ******************************************** ...

  8. 搜索引擎学习(一)初识Lucene

    一.Lucene相关基础概念 定义:一个简易的工具包,实现文件搜索的功能,支持中文,关键字,多条件查询,凡是文件名或文件内容包含的都查出来. 数据分类:结构化数据(固定格式或有限长度的数据)和非结构化 ...

  9. 【转载】Lucene.Net入门教程及示例

    本人看到这篇非常不错的Lucene.Net入门基础教程,就转载分享一下给大家来学习,希望大家在工作实践中可以用到. 一.简单的例子 //索引Private void Index(){    Index ...

随机推荐

  1. 关于java中static的应用及一种常见错误

    JAVA中的static的应用 在web项目的开发中,遇到了类中的static方法不奏效. 在开发过程中,我定义了一个静态方法初始化数组,但是在创建类的对象后,访问该数组是全为null.我一直以为st ...

  2. JavaScript:修改作用域外变量

    今天在看JavaScript学习指南的时候做的课后习题,也因此详细的对函数的传入参数进行比较深入的研究. 题目如下: 函数如何才能修改其作用域之外的变量?编写一个函数,由1~5的数字组成的数组作为参数 ...

  3. MySql分组函数-Group by与having理解

    注意:select 后的字段,必须要么包含在group by中,要么包含在having 后的聚合函数里. 1. GROUP BY 是分组查询, 一般 GROUP BY 是和聚合函数配合使用 group ...

  4. IOS上解决内存越界访问问题

    IOS经常会混合使用C代码,而在C中,对内存的读写是很频繁的操作. 其中,内存越界读写 unsigned char* p =(unsigned char*)malloc(10); unsigned c ...

  5. oracle单机改变归档路径

    oracle 归档日志文件路径设置 1.  查看LOG_ARCHIVE_DEST 与 ( LOG_ARCHIVE_DEST_n 或 DB_RECOVERY_FILE_DEST )参数情况注意(  LO ...

  6. IDEA Generating project in Batch mode

    Idea 设置 -DarchetypeCatalog=internal 然后再执行 : Archetype repository not defined. Using the one from [or ...

  7. 一段关于测试和自定义Attribute的代码

    来自<西夏普入门经典> using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  8. 配置文件的生成,关于“make menuconfig”

    之前学习嵌入式的时候从来没有注意过内核源文件下配置文件的生成(都是跟着老师的步骤直接复制过来修改成.config),其实意思也差不多,只是我没有细想其所以然: 编译内核的过程中,配置文件生成过程,A. ...

  9. Python使用struct处理二进制

    有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重 ...

  10. DES,AeS加解密,MD5,SHA加密

    1.DES一共就有4个参数参与运作:明文.密文.密钥.向量.其中这4者的关系可以理解为: 密文=明文+密钥+向量: 明文=密文-密钥-向量: 为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复, ...