开发环境 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 实例的更多相关文章

  1. 盘古分词修改支持mono和lucene.net3.03

    盘古分词平台兼容性 在使用Lucece.net,需要一个中文的分词组件,比较好的是盘古分词,但是我希望能够在mono的环境下运行,就使用moma检查了一下盘古分词 Assembly Version M ...

  2. 让盘古分词支持最新的Lucene.Net 3.0.3

    原文:让盘古分词支持最新的Lucene.Net 3.0.3 好多年没升级过的Lucene.Net最近居然升级了,到了3.0.3后接口发生了很大变化,原来好多分词库都不能用了,所以上次我把MMSeg给修 ...

  3. Unity 4.2.0 官方最新破解版(Unity3D 最新破解版,3D游戏开发工具和游戏引擎套件)

    Unity是一款跨平台的游戏开发工具,从一开始就被设计成易于使用的产品.作为一个完全集成的专业级应用,Unity还包含了价值数百万美元的功能强大的游戏引擎.Unity作为一个游戏开发工具,它的设计主旨 ...

  4. 编译最新ffmpeg2.0.1到iOS设备

    www.mingjianhua.com 转载请注明出处. 上一篇文章讲了用NDKr9编译最新ffmpeg2.0.1到android平台,一般做了Android平台的编解码就免不了要做iOS,这次一起把 ...

  5. 用 Love2D 实现法线贴图的例程(到最新版本 0.10.1)

    用 Love2D 实现法线贴图的例程(到最新版本 0.10.1) 概述 一般来说, 复杂的光照模型会被用在 3D 游戏中, 以产生逼真的效果, 不过也有些开发者研究出一些代码可以在 2D 游戏中使用这 ...

  6. Avada v5.0.6 最新版本破解教程如下:

    Avada v5.0.6 最新版本破解教程如下: .找到\themes\Avada\includes\avada-envato-api.php文件,注释掉如下两行代码 $response_code = ...

  7. 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 的升级 ...

  8. 阿里云在云栖大会发布SaaS加速器3.0版最新成果,让天下没有难做的SaaS

    2019年杭州·云栖大会顺利落幕,超过6万人次观展,200余位顶尖科学家分享了前沿技术.作为“阿里云不做SaaS”,坚持“被集成”战略的落地体现,阿里云SaaS加速器在云栖大会现场发布了SaaS加速器 ...

  9. lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)

    lucene.net 3.0.3.结合盘古分词进行搜索的小例子(分页功能)   添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新:2013-12-27 新增按分类查询功能, ...

随机推荐

  1. day47—JavaScript事件基础应用

    转行学开发,代码100天——2018-05-02 1.事件对象 JavaScript中事件对象通常用定义变量ev或event表示.为了兼顾浏览器兼容问题,定义事件对象为 var oEvent = ev ...

  2. Java 之 回调方法

    打个比方,我们点外卖都有到达提醒的服务,顾客自己选择提醒的方式.可以是电话提醒.短信提醒.也可以敲门提醒,这里,“提醒”这个行为是美团或饿了么这样的平台提供的,相当于库函数,但是提醒的方式是由顾客决定 ...

  3. 最长k可重线段集问题【费用流】【优先队列Dijkstra费用流】

    题目链接 与最长K可重区间问题一样的解法,但是这道题却有很多需要注意的地方,譬如就是精度问题,一开始没考虑到sprt()里面的乘会爆了精度,然后交上去竟然是TLE,然后找的原因方向也没对,最后TLE了 ...

  4. 001/Docker入门(Mooc)

    docker官网:https://www.docker.com/ 1.什么是docker 2.Docker思想     ==> [1].集装箱:保证程序完整(不缺东西,如配置文件等). [2]. ...

  5. WOJ#1243 蜥蜴 lizard

    描述 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任 ...

  6. [BZOJ 2301] [HAOI 2011] Problem b (莫比乌斯反演)(有证明)

    [BZOJ 2301] [HAOI 2011] Problem b (莫比乌斯反演)(有证明) 题面 T组询问,每次给出a,b,c,d,k,求\(\sum _{i=a}^b\sum _{j=c}^d[ ...

  7. [BZOJ 3731] Gty的超级妹子树 (树分块)

    [BZOJ 3731] Gty的超级妹子树 (树分块) 题面 给出一棵树(或森林),每个点都有一个值.现在有四种操作 1.查询x子树里>y的值有多少个 2.把点x的值改成y 3.添加一个新节点, ...

  8. 使用ssh协议在linux主机之间快速上传和下载文件

    scp 要上传的文件 上传主机用户名@主机地址:要上传的主机目录 例如: scp *20181111*.gz inas@10.2.13.57:/INAS/dsgbak/20181110 表示将当前目录 ...

  9. .NET基础篇——利用泛型与反射更新实体(ADO.NET Entity Framework)(转)

    自从ADO.NET Entity Framework面世以来,受到大家的热捧,它封装了大量代码生成的工具,用户只需要建立好实体之间的关系,系统就是会为用户自动成功了Add.Delete.CreateO ...

  10. SQLyog安装

    安装 使用 首先看到下面的界面