最近想提高下自己的能力,也是由于自己的项目中需要用到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. Python通用序列操作

    1.序列概览 1.数据结构 序列.容器 Python中最基本的数据结构是序列,其有索引(从左到右第一个索引为0,从右到左第一个索引为-1). Python包含6中内建的序列: 列表 元组 字符串 Un ...

  2. Codeforces Round #173 (Div. 2)

    A. Bit++ 模拟. B. Painting Eggs 贪心,每个物品给使差值较小的那个人,根据题目的约数条件,可证明贪心的正确性. C. XOR and OR \(,,00 \to 00,01 ...

  3. 笔记 线程(threads)

    线程:CPU使用的基本单元(线程ID.程序计数器.寄存器集合.栈). 多线程:一个进程有多个线程 多线程的优点: 增加响应度:当一个交互程序部分阻塞,该程序能继续执行 一个应用程序在同一地址空间有多个 ...

  4. 打算从oschina的博客搬运到cnblog了

    如题,感觉cnblog似乎要更加专业一点,顺便也禁水.提高下文章质量 以后就都是干货了 oschina原址 顺便庆祝一下Windows Live Writer配置成功

  5. Caffe+CUDA7.5+CuDNNv3+OpenCV3.0+Ubuntu14.04 配置参考文献 以及 常见编译问题总结

    Caffe+CUDA7.5+CuDNNv3+OpenCV3.0+Ubuntu14.04  配置参考文献 ---- Wang Xiao Warning: Please make sure the cud ...

  6. ClownFish:比手写代码还快的通用数据访问层

    http://www.cnblogs.com/fish-li/archive/2012/07/17/ClownFish.html 阅读目录 开始 ClownFish是什么? 比手写代码还快的执行速度 ...

  7. Golang里面使用protobuf(proto3)

    参考文章:https://developers.google.com/protocol-buffers/docs/gotutorial 1.执行指令: go envgo get github.com/ ...

  8. linux一些常用配置

    1.vi编辑退出不清屏 .bashrc最后加: 2.

  9. WebService异常时,查看请求状态码方法

    /// <summary> /// Test 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri ...

  10. PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组转换

    PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组之间的转换关系见下图 通过转换链,可以实现任意两个类型之间的互转.如PChar转PAnsiChar ...