Lucene.Net.dll:用做全文索引

PanGu.dll(盘古分词):作为中文分词的条件

大致原理:

1.Lucene先根据PanGu将需要搜索的内容分隔、分词,然后根据分词的结果,做一个索引页。

2.搜索的时候,直接从索引页里面进行查找个。

直接上代码:

分词演示代码:

 protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear(); //标准分词,只能对英文,不能对中文
//Analyzer analyzer = new StandardAnalyzer(); //盘古分词
Analyzer analyzer = new PanGuAnalyzer();
TokenStream tokenStream = analyzer.TokenStream("",new StringReader(txtString.Text));
Lucene.Net.Analysis.Token token = null; //.Next()获取到下一个词
while ((token=tokenStream.Next())!=null)
{
string word = token.TermText();//分到的词
ListBox1.Items.Add(word);
}
}

新建索引代码:演示了两种读取数据的方式

一:文本文件的查找

protected void Button1_Click(object sender, EventArgs e)
{
string indexPath = @"C:\index";//注意和磁盘上文件夹的大小写一致,否则会报错。
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
bool isUpdate = IndexReader.IndexExists(directory);
if (isUpdate)
{
//暂时规定:同时只能有一段代码操作索引库
//如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
if (IndexWriter.IsLocked(directory))
{
IndexWriter.Unlock(directory);
}
}
//IndexWriter负责把数据向索引库中写入
IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
for (int i = ; i < ; i++)
{
string txt =System.IO.File.ReadAllText(@"D:\net\net\代码\搜索及分词\文章\" + i + ".txt");
Document document = new Document();//文档对象。相当于表的一行记录
document.Add(new Field("number", i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("body", txt, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document); }
writer.Close();
directory.Close();//不要忘了Close,否则索引结果搜不到 this.ClientScript.RegisterStartupScript(typeof(indexPage),
"alert", "alert('创建索引完成')", true);
}

二:数据库里面查找数据

 protected void Button3_Click(object sender, EventArgs e)
{
string indexPath = @"D:\net\net\代码\搜索及分词\index1";//注意和磁盘上文件夹的大小写一致,否则会报错。
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
bool isUpdate = IndexReader.IndexExists(directory);
if (isUpdate)
{
//暂时规定:同时只能有一段代码操作索引库
//如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
if (IndexWriter.IsLocked(directory))
{
IndexWriter.Unlock(directory);
}
}
//IndexWriter负责把数据向索引库中写入
IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED); List<Writings> list = GetData();
foreach (Writings item in list)
{
Document document = new Document();//文档对象。相当于表的一行记录
document.Add(new Field("ID",item.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("Title", item.Title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("Contents", item.Contents, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document);
}
writer.Close();
directory.Close();//不要忘了Close,否则索引结果搜不到 this.ClientScript.RegisterStartupScript(typeof(indexPage),
"alert", "alert('创建索引完成')", true);
} private List<Writings> GetData()
{
string conn = "server=.;user id=sa; pwd=123; database=SharesTradeNew";
string sql = "SELECT * FROM dbo.Writings";
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataTable dt = new DataTable();
int a=da.Fill(dt);
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<Writings>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt));
}
} public class Writings
{
public int ID { get; set; }
public string Title { get; set; }
public string Contents { get; set; }
}

通过索引查找数据:

对应一:

protected void Button1_Click(object sender, EventArgs e)
{
//“计算机 专业”
string kw = TextBox1.Text;
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(@"c:\index"), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
PhraseQuery query = new PhraseQuery();//查询条件
foreach (string word in kw.Split(' '))//先用空格,让用户去分词,空格分隔的就是词“计算机 专业”
{
query.Add(new Term("body", word));//Contains("body",word)
}
//where Contains("body","计算机") and Contains("body","专业") query.SetSlop();
TopScoreDocCollector collector = TopScoreDocCollector.create(, true);//盛放搜索结果的容器
searcher.Search(query, null, collector);//用query这个查询条件进行搜索,搜索结果放入collector容器中 List<SearchResult> list = new List<SearchResult>(); // collector.GetTotalHits()查询结果的总条数
ScoreDoc[] docs = collector.TopDocs(, collector.GetTotalHits()).scoreDocs;
for (int i = ; i < docs.Length; i++)
{
int docId = docs[i].doc;//文档编号(lucene.net内部分配的,和number无关)
Document doc = searcher.Doc(docId);//根据文档编号拿到文档对象
string number = doc.Get("number");//取出文档的number字段的值。必须是Field.Store.YES才能取出来
string body = doc.Get("body"); SearchResult sr = new SearchResult();
sr.Body = body;
sr.Number = number; list.Add(sr);
}
Repeater1.DataSource = list;
Repeater1.DataBind();
}

对应二:

protected void Button3_Click(object sender, EventArgs e)
{
//“计算机 专业”
string kw = TextBox3.Text;
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(@"D:\net\net\代码\搜索及分词\index1"), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
PhraseQuery query = new PhraseQuery();//查询条件
foreach (string word in kw.Split(' '))//先用空格,让用户去分词,空格分隔的就是词“计算机 专业”
{
query.Add(new Term("Contents", word));//Contains("body",word)
//query.Add(new Term("Title", word));
}
//where Contains("body","计算机") and Contains("body","专业") query.SetSlop();
TopScoreDocCollector collector = TopScoreDocCollector.create(, true);//盛放搜索结果的容器
searcher.Search(query, null, collector);//用query这个查询条件进行搜索,搜索结果放入collector容器中 List<Writings> list = new List<Writings>(); // collector.GetTotalHits()查询结果的总条数
ScoreDoc[] docs = collector.TopDocs(, collector.GetTotalHits()).scoreDocs;
for (int i = ; i < docs.Length; i++)
{
int docId = docs[i].doc;//文档编号(lucene.net内部分配的,和number无关)
Document doc = searcher.Doc(docId);//根据文档编号拿到文档对象
string id = doc.Get("ID");//取出文档的number字段的值。必须是Field.Store.YES才能取出来
string title = doc.Get("Title");
string content = doc.Get("Contents"); Writings sr = new Writings();
sr.ID = int.Parse(id);
sr.Title = title;
sr.Contents = content; list.Add(sr);
}
Repeater3.DataSource = list;
Repeater3.DataBind();
}

Lucene.Net和盘古分词应用的更多相关文章

  1. Lucene.net 全文检索 盘古分词

    lucene.net + 盘古分词 引用: 1.Lucene.Net.dll 2.PanGu.Lucene.Analyzer.dll 3.PanGu.HighLight.dll 4.PanGu.dll ...

  2. Lucene.Net 与 盘古分词

    1.关键的一点,Lucene.Net要使用3.0下面的版本号,否则与盘古分词接口不一致. 关键代码例如以下 using System; using System.IO; using System.Co ...

  3. Net Core使用Lucene.Net和盘古分词器 实现全文检索

    Lucene.net Lucene.net是Lucene的.net移植版本,是一个开源的全文检索引擎开发包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎, ...

  4. Lucene.net入门学习(结合盘古分词)

    Lucene简介 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整 ...

  5. Lucene.net入门学习(结合盘古分词)(转载)

    作者:释迦苦僧  出处:http://www.cnblogs.com/woxpp/p/3972233.html  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显 ...

  6. 【原创】Lucene.Net+盘古分词器(详细介绍)

    本章阅读概要 1.Lucenne.Net简介 2.介绍盘古分词器 3.Lucene.Net实例分析 4.结束语(Demo下载) Lucene.Net简介 Lucene.net是Lucene的.net移 ...

  7. 站内搜索——Lucene +盘古分词

    为了方便的学习站内搜索,下面我来演示一个MVC项目. 1.首先在项目中[添加引入]三个程序集和[Dict]文件夹,并新建一个[分词内容存放目录] Lucene.Net.dll.PanGu.dll.Pa ...

  8. Lucene.Net+盘古分词->开发自己的搜索引擎

    //封装类 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Lucene. ...

  9. Lucene.Net+盘古分词器(详细介绍)(转)

    出处:http://www.cnblogs.com/magicchaiy/archive/2013/06/07/LuceneNet%E7%9B%98%E5%8F%A4%E5%88%86%E8%AF%8 ...

随机推荐

  1. poj1753-Flip Game 【状态压缩+bfs】

    http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. ==, equals, hashcode的理解

    一.java对象的比较 等号(==): 对比对象实例的内存地址(也即对象实例的ID),来判断是否是同一对象实例:又可以说是判断对象实例是否物理相等: equals(): 对比两个对象实例是否相等. 当 ...

  3. SQl Server T-sql语句学习

    T-sql语句就是通过代码来代替鼠标完成一些操作,使用起来要比鼠标方便很多. 创建数据库   careate  database  +数据库名. 数据库名不能为中文,不能以数字开头. use  数据库 ...

  4. Python 网络爬虫 001 (科普) 网络爬虫简介

    Python 网络爬虫 001 (科普) 网络爬虫简介 1. 网络爬虫是干什么的 我举几个生活中的例子: 例子一: 我平时会将 学到的知识 和 积累的经验 写成博客发送到CSDN博客网站上,那么对于我 ...

  5. Django rest-framework框架十大功能分析

    rest-framework框架有哪些作用? 一共有十点. 路由 - 可以通过as_view传参数,根据请求方式不同执行相应的方法 - 可以在url中设置一个结尾,类似于: .json 视图 - 帮助 ...

  6. 解决Spring MVC 对AOP不起作用的问题

    用的是 SSM3的框架 Spring MVC 3.1 + Spring 3.1 + Mybatis3.1 第一种情况: Spring MVC 和 Spring 整合的时候,SpringMVC的spri ...

  7. Chrome Plugin Recommendation

    1.AdBlock 拦截广告神器 2.IPBlade 变更IP地址,使你自由 3.JSON-handle 让接口返回的JSON数据更好看 4.Proxy SwitchyOmega 变更浏览器代理 5. ...

  8. 在GridView控件FooterTemplate内添加记录

    在GridView控件FooterTemplate内添加记录,想实现这个功能,有几点要清楚的,这个添加铵钮是在FooterTemplate内,还是在GridView控件外部,位置不同,某些处理逻辑会有 ...

  9. Join方法把阵列转为字符串

    string有一个方法,就是Join,可以把阵列转换为字符串: 下面是Join方法重载,可以指定那个元素开始,连续取几个元素: 可以传入一个IEnumerable<T>参数:

  10. metasploit 读书笔记-信息收集

    三、信息收集 被动信息收集 在不接触目标系统时进行的信息收集,包括使用工具Yeti、Whois (1)Whois msf > whois secmaniac.net (2)Netcraft:fi ...