全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例
开发环境 vs2015 winform 程序
1 首先需要下载对应的DLL 文章后面统一提供程序下载地址 里面都有
2 配置pangu的参数 也可以不配置 采用默认的即可
3 创建索引,将索引存放到本地
4 根据关键字查询本地索引
5 取得查询结果并展示
以上是主要的步骤,下面贴上主要代码,拿来即可用
Form1.Designer.cs
namespace lucuneTest
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnSearch = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.textBox2 = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.txtWords = new System.Windows.Forms.TextBox();
this.btnCutWords = new System.Windows.Forms.Button();
this.txtWords2 = new System.Windows.Forms.TextBox();
this.txtResult = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// btnSearch
//
this.btnSearch.Location = new System.Drawing.Point(, );
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(, );
this.btnSearch.TabIndex = ;
this.btnSearch.Text = "查询";
this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// panel1
//
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.label4);
this.panel1.Controls.Add(this.txtWords2);
this.panel1.Controls.Add(this.btnCutWords);
this.panel1.Controls.Add(this.txtWords);
this.panel1.Controls.Add(this.textBox2);
this.panel1.Controls.Add(this.btnSearch);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(, );
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(, );
this.panel1.TabIndex = ;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(, );
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(, );
this.textBox2.TabIndex = ;
this.textBox2.Text = "天龙八部";
//
// panel2
//
this.panel2.Controls.Add(this.txtResult);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(, );
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(, );
this.panel2.TabIndex = ;
//
// txtWords
//
this.txtWords.Location = new System.Drawing.Point(, );
this.txtWords.Multiline = true;
this.txtWords.Name = "txtWords";
this.txtWords.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtWords.Size = new System.Drawing.Size(, );
this.txtWords.TabIndex = ;
//
// btnCutWords
//
this.btnCutWords.Location = new System.Drawing.Point(, );
this.btnCutWords.Name = "btnCutWords";
this.btnCutWords.Size = new System.Drawing.Size(, );
this.btnCutWords.TabIndex = ;
this.btnCutWords.Text = "分词--》";
this.btnCutWords.UseVisualStyleBackColor = true;
this.btnCutWords.Click += new System.EventHandler(this.btnCutWords_Click);
//
// txtWords2
//
this.txtWords2.Location = new System.Drawing.Point(, );
this.txtWords2.Multiline = true;
this.txtWords2.Name = "txtWords2";
this.txtWords2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtWords2.Size = new System.Drawing.Size(, );
this.txtWords2.TabIndex = ;
//
// txtResult
//
this.txtResult.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtResult.Location = new System.Drawing.Point(, );
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtResult.Size = new System.Drawing.Size(, );
this.txtResult.TabIndex = ;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(, );
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(, );
this.label3.TabIndex = ;
this.label3.Text = "盘古";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(, );
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(, );
this.label4.TabIndex = ;
this.label4.Text = "标准";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false); } #endregion
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button btnCutWords;
private System.Windows.Forms.TextBox txtWords;
private System.Windows.Forms.TextBox txtWords2;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
}
}
Form1.cs
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Store;
using LN = Lucene.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.Analysis.PanGu;
using Lucene.Net.Search;
using PanGu;
using Lucene.Net.QueryParsers;
using PanGu.HighLight;
using System.Diagnostics; namespace lucuneTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//定义盘古分词的xml引用路径
PanGu.Segment.Init(PanGuXmlPath);
//创建索引
createIndex();
} /// <summary>
/// 创建索引
/// </summary>
void createIndex()
{ //IndexWriter第三个参数:true指重新创建索引,false指从当前索引追加....此处为新建索引所以为true,后续应该建立的索引应采用追加
IndexWriter writer = new IndexWriter(direcotry, PanGuAnalyzer, true, IndexWriter.MaxFieldLength.LIMITED); Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = ; i < ; i++)
{
AddIndex(writer, "我的标题" + i, i + "这是我的标题啦" + i, DateTime.Now.AddDays(i).ToString("yyyy-MM-dd"));
AddIndex(writer, "射雕英雄传作者金庸" + i, i + "我是欧阳锋" + i, DateTime.Now.AddDays(i).ToString("yyyy-MM-dd"));
AddIndex(writer, "天龙八部12" + i, i + "慕容废墟,上官静儿,打撒飞艾丝凡爱上,虚竹" + i, DateTime.Now.AddDays(i).ToString("yyyy-MM-dd"));
AddIndex(writer, "倚天屠龙记12" + i, i + "张无忌机" + i, DateTime.Now.AddDays(i).ToString("yyyy-MM-dd"));
AddIndex(writer, "三国演义" + i, i + "刘备,张飞,关羽还有谁来着 忘记啦" + i, DateTime.Now.AddDays(i).ToString("yyyy-MM-dd"));
}
//释放资源
writer.Optimize();
writer.Dispose();
string time = ((double)sw.ElapsedMilliseconds / ).ToString();
sw.Stop();
Console.WriteLine("创建100条记录需要时长:" + time + "秒");
} /// <summary>
/// 创建索引
/// </summary>
/// <param name="analyzer"></param>
/// <param name="title"></param>
/// <param name="content"></param>
private void AddIndex(IndexWriter writer, string title, string content, string date)
{
try
{
Document doc = new Document();
doc.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED));//存储且索引
doc.Add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));//存储且索引
doc.Add(new Field("addtime", date, Field.Store.YES, Field.Index.NOT_ANALYZED));//不分词存储
writer.AddDocument(doc);
}
catch (FileNotFoundException fnfe)
{
throw fnfe;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 分词测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCutWords_Click(object sender, EventArgs e)
{
this.txtWords.Text = "";
Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
txtWords.Text = cutWords(this.textBox2.Text, PanGuAnalyzer);//盘古分词
txtWords2.Text = cutWords(this.textBox2.Text, analyzer); //自带标准分词
} /// <summary>
/// 分词方法
/// </summary>
/// <param name="words">待分词内容</param>
/// <param name="analyzer"></param>
/// <returns></returns>
private string cutWords(string words, Analyzer analyzer)
{
string resultStr = "";
System.IO.StringReader reader = new System.IO.StringReader(words);
Lucene.Net.Analysis.TokenStream ts = analyzer.TokenStream(words, reader);
bool hasNext = ts.IncrementToken();
Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
while (hasNext)
{
ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
resultStr += ita.Term + "|";
hasNext = ts.IncrementToken();
}
ts.CloneAttributes();
reader.Close();
analyzer.Close();
return resultStr;
} protected IList<Article> list = new List<Article>(); /// <summary>
/// 查询多个字段
/// </summary>
private void SearchIndex(string searchKey)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
BooleanQuery bQuery = new BooleanQuery(); #region 一个字段查询
//if (!string.IsNullOrEmpty(title))
//{
// title = GetKeyWordsSplitBySpace(title);
// QueryParser parse = new QueryParser(LN.Util.Version.LUCENE_30, "title", PanGuAnalyzer);//一个字段查询
// Query query = parse.Parse(title);
// parse.DefaultOperator = QueryParser.Operator.OR;
// bQuery.Add(query, new Occur());
// dic.Add("title", title);
//} #endregion string[] fileds = { "title", "content" };//查询字段
searchKey = GetKeyWordsSplitBySpace(searchKey);
QueryParser parse = new MultiFieldQueryParser(LN.Util.Version.LUCENE_30, fileds, PanGuAnalyzer);//多个字段查询
Query query = parse.Parse(searchKey);
bQuery.Add(query, new Occur()); dic.Add("title", searchKey);
dic.Add("content", searchKey); if (bQuery != null && bQuery.GetClauses().Length > )
{
GetSearchResult(bQuery, dic);
}
} /// <summary>
/// 获取
/// </summary>
/// <param name="bQuery"></param>
private void GetSearchResult(BooleanQuery bQuery, Dictionary<string, string> dicKeywords)
{
IndexSearcher search = new IndexSearcher(direcotry, true);
// Stopwatch stopwatch = Stopwatch.StartNew();
//SortField构造函数第三个字段true为降序,false为升序
Sort sort = new Sort(new SortField("addtime", SortField.DOC, true));
int maxNum = ;//查询条数
TopDocs docs = search.Search(bQuery, (Filter)null, maxNum, sort); if (docs != null)
{ for (int i = ; i < docs.TotalHits && i < maxNum; i++)
{
Document doc = search.Doc(docs.ScoreDocs[i].Doc);
Article model = new Article()
{
Title = doc.Get("title").ToString(),
Content = doc.Get("content").ToString(),
AddTime = doc.Get("addtime").ToString()
};
list.Add(SetHighlighter(dicKeywords, model)); }
} } /// <summary>
/// 索引存放目录
/// </summary>
protected string IndexDic
{
get
{
return Application.StartupPath + "/IndexDic";
}
} public LN.Store.Directory direcotry
{
get
{ //创建索引目录
if (!System.IO.Directory.Exists(IndexDic))
{
System.IO.Directory.CreateDirectory(IndexDic);
} LN.Store.Directory direcotry = FSDirectory.Open(IndexDic);
return direcotry;
} }
/// <summary>
/// 盘古分词的配置文件
/// </summary>
protected string PanGuXmlPath
{
get
{
return Application.StartupPath + "/PanGu/PanGu.xml";
}
} /// <summary>
/// 盘古分词器
/// </summary>
protected Analyzer PanGuAnalyzer
{
get { return new PanGuAnalyzer(); } } /// <summary>
/// 处理关键字为索引格式
/// </summary>
/// <param name="keywords"></param>
/// <returns></returns>
private string GetKeyWordsSplitBySpace(string keywords)
{
PanGuTokenizer ktTokenizer = new PanGuTokenizer();
StringBuilder result = new StringBuilder();
ICollection<WordInfo> words = ktTokenizer.SegmentToWordInfos(keywords); foreach (WordInfo word in words)
{
if (word == null)
{
continue;
}
result.AppendFormat("{0}^{1}.0 ", word.Word, (int)Math.Pow(, word.Rank));
}
return result.ToString().Trim();
} /// <summary>
/// 设置关键字高亮
/// </summary>
/// <param name="dicKeywords">关键字列表</param>
/// <param name="model">返回的数据模型</param>
/// <returns></returns>
private Article SetHighlighter(Dictionary<string, string> dicKeywords, Article model)
{
SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"green\">", "</font>");
Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment());
highlighter.FragmentSize = ;
string strTitle = string.Empty;
string strContent = string.Empty;
dicKeywords.TryGetValue("title", out strTitle);
dicKeywords.TryGetValue("content", out strContent);
if (!string.IsNullOrEmpty(strTitle))
{
var transStr = highlighter.GetBestFragment(strTitle, model.Title);
model.Title = string.IsNullOrEmpty(transStr) ? model.Title : transStr;
}
if (!string.IsNullOrEmpty(strContent))
{
var transStr = highlighter.GetBestFragment(strContent, model.Content);
model.Content = string.IsNullOrEmpty(transStr) ? model.Content : transStr;
}
return model;
} /// <summary>
/// 查询方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
list.Clear();
this.txtResult.Text = "";
SearchIndex(this.textBox2.Text);
if (list.Count == )
{
this.txtResult.Text = "没有查询到结果";
return;
}
for (int i = ; i < list.Count; i++)
{
this.txtResult.Text += "标题:" + list[i].Title + " 内容:" + list[i].Content + " 时间:" + list[i].AddTime + "\r\n";
}
} #region 删除索引数据(根据id)
/// <summary>
/// 删除索引数据(根据id)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool Delete(string id)
{
bool IsSuccess = false;
Term term = new Term("id", id);
//Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
//Version version = new Version();
//MultiFieldQueryParser parser = new MultiFieldQueryParser(version, new string[] { "name", "job" }, analyzer);//多个字段查询
//Query query = parser.Parse("小王"); //IndexReader reader = IndexReader.Open(directory_luce, false);
//reader.DeleteDocuments(term);
//Response.Write("删除记录结果: " + reader.HasDeletions + "<br/>");
//reader.Dispose(); IndexWriter writer = new IndexWriter(direcotry, PanGuAnalyzer, false, IndexWriter.MaxFieldLength.LIMITED);
writer.DeleteDocuments(term); // writer.DeleteDocuments(term)或者writer.DeleteDocuments(query);
////writer.DeleteAll();
writer.Commit();
//writer.Optimize();//
IsSuccess = writer.HasDeletions();
writer.Dispose();
return IsSuccess;
}
#endregion #region 删除全部索引数据
/// <summary>
/// 删除全部索引数据
/// </summary>
/// <returns></returns>
public bool DeleteAll()
{
bool IsSuccess = true;
try
{ IndexWriter writer = new IndexWriter(direcotry, PanGuAnalyzer, false, IndexWriter.MaxFieldLength.LIMITED);
writer.DeleteAll();
writer.Commit();
//writer.Optimize();//
IsSuccess = writer.HasDeletions();
writer.Dispose();
}
catch
{
IsSuccess = false;
}
return IsSuccess;
}
#endregion } public class Article
{
public string Id
{
set;
get;
} public string Title
{
set;
get;
} public string Content
{
set;
get;
} public string AddTime
{
set;
get;
}
}
}
实例下载地址:lucuneTest.zip
全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例的更多相关文章
- 盘古分词修改支持mono和lucene.net3.03
盘古分词平台兼容性 在使用Lucece.net,需要一个中文的分词组件,比较好的是盘古分词,但是我希望能够在mono的环境下运行,就使用moma检查了一下盘古分词 Assembly Version M ...
- 让盘古分词支持最新的Lucene.Net 3.0.3
原文:让盘古分词支持最新的Lucene.Net 3.0.3 好多年没升级过的Lucene.Net最近居然升级了,到了3.0.3后接口发生了很大变化,原来好多分词库都不能用了,所以上次我把MMSeg给修 ...
- Unity 4.2.0 官方最新破解版(Unity3D 最新破解版,3D游戏开发工具和游戏引擎套件)
Unity是一款跨平台的游戏开发工具,从一开始就被设计成易于使用的产品.作为一个完全集成的专业级应用,Unity还包含了价值数百万美元的功能强大的游戏引擎.Unity作为一个游戏开发工具,它的设计主旨 ...
- 编译最新ffmpeg2.0.1到iOS设备
www.mingjianhua.com 转载请注明出处. 上一篇文章讲了用NDKr9编译最新ffmpeg2.0.1到android平台,一般做了Android平台的编解码就免不了要做iOS,这次一起把 ...
- 用 Love2D 实现法线贴图的例程(到最新版本 0.10.1)
用 Love2D 实现法线贴图的例程(到最新版本 0.10.1) 概述 一般来说, 复杂的光照模型会被用在 3D 游戏中, 以产生逼真的效果, 不过也有些开发者研究出一些代码可以在 2D 游戏中使用这 ...
- Avada v5.0.6 最新版本破解教程如下:
Avada v5.0.6 最新版本破解教程如下: .找到\themes\Avada\includes\avada-envato-api.php文件,注释掉如下两行代码 $response_code = ...
- CDH 版本 6.0.1 升级到 6.2.0 当前最新版本(CentOS 7.x)
前文「CDH CM版本 6.0.1 升级到 CM 6.2.0 当前最新版本(CentOS 7.x)」 承接上文,当我们完成 CM 6.2.0 的升级之后,我们已经相当于完成了80% minor 的升级 ...
- 阿里云在云栖大会发布SaaS加速器3.0版最新成果,让天下没有难做的SaaS
2019年杭州·云栖大会顺利落幕,超过6万人次观展,200余位顶尖科学家分享了前沿技术.作为“阿里云不做SaaS”,坚持“被集成”战略的落地体现,阿里云SaaS加速器在云栖大会现场发布了SaaS加速器 ...
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)
lucene.net 3.0.3.结合盘古分词进行搜索的小例子(分页功能) 添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新:2013-12-27 新增按分类查询功能, ...
随机推荐
- 用vuex实现购物车功能
效果图 展示目录结构 product组件(纯静态代码) cart组件(纯静态代码) info组件(纯静态代码) 完成以上的三个组件,现在要开始调用这些组件,在App.vue中调用 如果你的姿势正确的话 ...
- Linux随笔 - vi/vim 编辑器显示行号
显示行号 1. 打开vi 编辑器 2. 输入 :set number 3. 回车 关闭行号显示 1. 打开vi 编辑器 2. 输入 :set nonumber 3. 回车 行号在每次打开 vi/v ...
- MDX 入门
之前用到的SQL,解释:结构化查询语言(Structured Query Language)(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言,是一种 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第8节 Math类_18_数学工具类Math
常用几个数学的方法 abs绝对值 ceil向上取整,它并不是四舍五入 floor向下取整 round四舍五入 PI 按住Ctrl+鼠标左键 进入Math这个类的源码里面 Ctrl+F12 然后输入PI ...
- Linux负载均衡实现
配置之前清空所有服务器防火墙规则 iptables -F 关闭selinux: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开 ...
- BIN转换成HEX格式及HEX转换成BIN的两个函数接口
unsigned char HEX2BYTE(unsigned char hex_ch) { ') { '; } if (hex_ch >= 'a' && hex_ch < ...
- hadoop 与 hbase 添加开机启动,按顺序,先hadoop,后hbase,开机启动脚本,hbase学习
hadoop安装,hbase单机安装,参考链接,https://blog.csdn.net/LiuHuan_study/article/details/84347262 开机启动脚本,参考, http ...
- python自动化测试接口测试http请求报404的其中一个坑
在敲代码的路上 ,总是会遇到报错找半天原因,最后发现是个低级错误的时候! 这不今天为了这个错误找了半天原因.......... http请求接口测试中报404我遇到的大部分都是url的问题: 但是今天 ...
- 关于ES6语法的 一些新的特性
1.新的变量声明 :let :块级作用域,解决全局污染问题 const :常量 ,如π:3.1415927 class :类 .var:弱类型 funciton :方法 , import : 导入参 ...
- eclipse中常用的快捷键【开发常用到的】
1.全部选中:Ctrl+A 2.剪切Ctrl+X.复制Ctrl+C.粘贴Ctrl+V.保存Ctrl+S 3.撤销Ctrl+Z.取消撤销Ctrl+Y 4.规范代码:Ctrl+Shift+F 5.将代码更 ...