标签:
转自:http://blog.csdn.net/pukuimin1226/article/details/17558247
添加:2013-12-25
更新:2013-12-26 新增分页功能。
更新:2013-12-27 新增按分类查询功能,调整索引行新增记录的图片字段。
最新盘古分词dll和词典管理工具下载:http://pangusegment.codeplex.com/
词典下载:http://pangusegment.codeplex.com/releases/view/47411
Lucene.net下载:http://lucenenet.apache.org/download.cgi
//封装类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using Lucene.Net.Analysis;
- using Lucene.Net.Index;
- using Lucene.Net.Documents;
- using System.Reflection;
- using Lucene.Net.QueryParsers;
- using Lucene.Net.Search;
- namespace SearchTest
- {
-
-
-
-
- public class PanGuLuceneHelper
- {
- private PanGuLuceneHelper() { }
-
- #region 单一实例
- private static PanGuLuceneHelper _instance = null;
-
-
-
- public static PanGuLuceneHelper instance
- {
- get
- {
- if (_instance == null) _instance = new PanGuLuceneHelper();
- return _instance;
- }
- }
- #endregion
-
- #region 分词测试
-
-
-
-
-
- public string Token(string keyword)
- {
- string ret = "";
- System.IO.StringReader reader = new System.IO.StringReader(keyword);
- Lucene.Net.Analysis.TokenStream ts = analyzer.TokenStream(keyword, reader);
- bool hasNext = ts.IncrementToken();
- Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
- while (hasNext)
- {
- ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
- ret += ita.Term + "|";
- hasNext = ts.IncrementToken();
- }
- ts.CloneAttributes();
- reader.Close();
- analyzer.Close();
- return ret;
- }
- #endregion
-
- #region 创建索引
-
-
-
-
-
- public bool CreateIndex(List<MySearchUnit> datalist)
- {
- IndexWriter writer = null;
- try
- {
- writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);
- }
- catch
- {
- writer = new IndexWriter(directory_luce, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
- }
- foreach (MySearchUnit data in datalist)
- {
- CreateIndex(writer, data);
- }
- writer.Optimize();
- writer.Dispose();
- return true;
- }
-
- public bool CreateIndex(IndexWriter writer, MySearchUnit data)
- {
- try
- {
-
- if (data == null) return false;
- Document doc = new Document();
- Type type = data.GetType();
-
-
-
-
- PropertyInfo[] Propertys = type.GetProperties();
- for (int i = 0; i < Propertys.Length; i++)
- {
-
- PropertyInfo pi = Propertys[i];
- string name=pi.Name;
- object objval = pi.GetValue(data, null);
- string value = objval == null ? "" : objval.ToString();
- if (name == "id" || name=="flag" )
- {
- doc.Add(new Field(name, value, Field.Store.YES, Field.Index.NOT_ANALYZED));
- }
- else
- {
- doc.Add(new Field(name, value, Field.Store.YES, Field.Index.ANALYZED));
- }
- }
- writer.AddDocument(doc);
- }
- catch (System.IO.FileNotFoundException fnfe)
- {
- throw fnfe;
- }
- return true;
- }
- #endregion
-
- #region 在title和content字段中查询数据
-
-
-
-
-
- public List<MySearchUnit> Search(string keyword)
- {
-
- string[] fileds = { "title", "content" };
-
-
- QueryParser parser = null;
- parser = new MultiFieldQueryParser(version, fileds, analyzer);
- Query query = parser.Parse(keyword);
- int n = 1000;
- IndexSearcher searcher = new IndexSearcher(directory_luce, true);
- TopDocs docs = searcher.Search(query, (Filter)null, n);
- if (docs == null || docs.TotalHits == 0)
- {
- return null;
- }
- else
- {
- List<MySearchUnit> list = new List<MySearchUnit>();
- int counter = 1;
- foreach (ScoreDoc sd in docs.ScoreDocs)
- {
- try
- {
- Document doc = searcher.Doc(sd.Doc);
- string id = doc.Get("id");
- string title = doc.Get("title");
- string content = doc.Get("content");
- string flag = doc.Get("flag");
- string imageurl = doc.Get("imageurl");
- string updatetime = doc.Get("updatetime");
-
- string createdate = doc.Get("createdate");
- PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
- PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
- highlighter.FragmentSize = 50;
- content = highlighter.GetBestFragment(keyword, content);
- string titlehighlight = highlighter.GetBestFragment(keyword, title);
- if (titlehighlight != "") title = titlehighlight;
- list.Add(new MySearchUnit(id, title, content, flag,imageurl, updatetime));
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- counter++;
- }
- return list;
- }
-
-
-
- }
- #endregion
-
- #region 在不同的分类下再根据title和content字段中查询数据(分页)
-
-
-
-
-
-
-
-
-
- public List<MySearchUnit> Search(string _flag,string keyword, int PageIndex, int PageSize, out int TotalCount)
- {
- if (PageIndex < 1) PageIndex = 1;
-
-
- BooleanQuery bq = new BooleanQuery();
- if (_flag != "")
- {
- QueryParser qpflag = new QueryParser(version, "flag", analyzer);
- Query qflag = qpflag.Parse(_flag);
- bq.Add(qflag, Occur.MUST);
- }
- if (keyword != "")
- {
- string[] fileds = { "title", "content" };
- QueryParser parser = null;
- parser = new MultiFieldQueryParser(version, fileds, analyzer);
- Query queryKeyword = parser.Parse(keyword);
- bq.Add(queryKeyword, Occur.MUST);
- }
-
- TopScoreDocCollector collector = TopScoreDocCollector.Create(PageIndex * PageSize, false);
- IndexSearcher searcher = new IndexSearcher(directory_luce, true);
- searcher.Search(bq, collector);
- if (collector == null || collector.TotalHits == 0)
- {
- TotalCount = 0;
- return null;
- }
- else
- {
- int start = PageSize * (PageIndex - 1);
-
- int limit = PageSize;
- ScoreDoc[] hits = collector.TopDocs(start, limit).ScoreDocs;
- List<MySearchUnit> list = new List<MySearchUnit>();
- int counter = 1;
- TotalCount = collector.TotalHits;
- foreach (ScoreDoc sd in hits)
- {
- try
- {
- Document doc = searcher.Doc(sd.Doc);
- string id = doc.Get("id");
- string title = doc.Get("title");
- string content = doc.Get("content");
- string flag = doc.Get("flag");
- string imageurl = doc.Get("imageurl");
- string updatetime = doc.Get("updatetime");
-
- PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
- PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
- highlighter.FragmentSize = 50;
- content = highlighter.GetBestFragment(keyword, content);
- string titlehighlight = highlighter.GetBestFragment(keyword, title);
- if (titlehighlight != "") title = titlehighlight;
- list.Add(new MySearchUnit(id, title, content, flag,imageurl, updatetime));
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- counter++;
- }
- return list;
- }
-
-
-
- }
- #endregion
-
- #region 删除索引数据(根据id)
-
-
-
-
-
- public bool Delete(string id)
- {
- bool IsSuccess = false;
- Term term = new Term("id", id);
-
-
-
-
-
-
-
-
-
-
- IndexWriter writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);
- writer.DeleteDocuments(term);
-
- writer.Commit();
-
- IsSuccess = writer.HasDeletions();
- writer.Dispose();
- return IsSuccess;
- }
- #endregion
-
- #region 删除全部索引数据
-
-
-
-
- public bool DeleteAll()
- {
- bool IsSuccess = true;
- try
- {
- IndexWriter writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);
- writer.DeleteAll();
- writer.Commit();
-
- IsSuccess = writer.HasDeletions();
- writer.Dispose();
- }
- catch
- {
- IsSuccess = false;
- }
- return IsSuccess;
- }
- #endregion
-
- #region directory_luce
- private Lucene.Net.Store.Directory _directory_luce = null;
-
-
-
- public Lucene.Net.Store.Directory directory_luce
- {
- get
- {
- if (_directory_luce == null) _directory_luce = Lucene.Net.Store.FSDirectory.Open(directory);
- return _directory_luce;
- }
- }
- #endregion
-
- #region directory
- private System.IO.DirectoryInfo _directory = null;
-
-
-
- public System.IO.DirectoryInfo directory
- {
- get
- {
- if (_directory == null)
- {
- string dirPath = AppDomain.CurrentDomain.BaseDirectory + "SearchIndex";
- if (System.IO.Directory.Exists(dirPath) == false) _directory = System.IO.Directory.CreateDirectory(dirPath);
- else _directory = new System.IO.DirectoryInfo(dirPath);
- }
- return _directory;
- }
- }
- #endregion
-
- #region analyzer
- private Analyzer _analyzer = null;
-
-
-
- public Analyzer analyzer
- {
- get
- {
-
- {
- _analyzer = new Lucene.Net.Analysis.PanGu.PanGuAnalyzer();
-
- }
- return _analyzer;
- }
- }
- #endregion
-
- #region version
- private static Lucene.Net.Util.Version _version = Lucene.Net.Util.Version.LUCENE_30;
-
-
-
- public Lucene.Net.Util.Version version
- {
- get
- {
- return _version;
- }
- }
- #endregion
- }
-
- #region 索引的一个行单元,相当于数据库中的一行数据
-
-
-
- public class MySearchUnit
- {
- public MySearchUnit(string _id, string _title, string _content, string _flag, string _imageurl, string _updatetime)
- {
- this.id = _id;
- this.title = _title;
- this.content = _content;
- this.flag = _flag;
- this.imageurl = _imageurl;
- this.updatetime = _updatetime;
- }
-
-
-
- public string id { get; set; }
-
-
-
- public string title { get; set; }
-
-
-
- public string content { get; set; }
-
-
-
- public string flag { get; set; }
-
-
-
- public string imageurl { get; set; }
-
-
-
- public string updatetime { get; set; }
- }
- #endregion
- }
//调用测试
- protected void Page_Load(object sender, EventArgs e)
- {
-
-
-
- bool exec = false;
- if (exec)
- {
- List<MySearchUnit> list = new List<MySearchUnit>();
- list.Add(new MySearchUnit("1a", "标题小王", "今天是小王的生日,大家都很高兴去他家喝酒,玩了一整天。", new Random().Next(1, 10).ToString(), "", ""));
- list.Add(new MySearchUnit("1b", "标题小张", "今天是小张的生日,大家都很高兴去他家喝酒,玩了几天。", new Random().Next(1, 10).ToString(), "", ""));
- list.Add(new MySearchUnit("1c", "标题小王", "今天是小王的生日,大家都很高兴去他家喝酒,玩了一整天。", new Random().Next(1, 10).ToString(), "", ""));
- list.Add(new MySearchUnit("1d", "标题小张", "今天是小张的生日,大家都很高兴去他家喝酒,玩了几天。", new Random().Next(1, 10).ToString(), "", ""));
- PanGuLuceneHelper.instance.CreateIndex(list);
- }
- int count = 0;
- int PageIndex=2;
- int PageSize=4;
- string html_content = "";
- List<MySearchUnit> searchlist = PanGuLuceneHelper.instance.Search("3","小王 生日",PageIndex,PageSize,out count);
- html_content+=("查询结果:" + count + "条数据<br/>");
- if (searchlist == null || searchlist.Count==0)
- {
- html_content += ("未查询到数据。<br/>");
- }
- else
- {
- foreach (MySearchUnit data in searchlist)
- {
- html_content += (string.Format("id:{0},title:{1},content:{2},flag:{3},updatetime:{4}<br/>", data.id, data.title, data.content, data.flag, data.updatetime));
- }
- }
- html_content += (PanGuLuceneHelper.instance.version);
- div_content.InnerHtml = html_content;
- }
//效果:
第一版源码示例下载:http://download.csdn.net/detail/pukuimin1226/6768179
最新源码示例下载:http://download.csdn.net/detail/pukuimin1226/6776049
百度云盘下载链接:http://pan.baidu.com/s/1o69cCD8
Lucene.Net没有判断数据重复性,同一条数据插入多少遍它就有多少条相同的数据,所以,我们人为地用id区分,在数据量大,全部重新创建索引时间长的情况下(数据量到几万以上就耗资源了,从数据库中查询出来,再写入索引,使得数据库和程序本身都增加负担),增量建立索引是很有必要的。
新增一条数据,就直接添加一条索引;
修改一条数据,先删除同一个id的索引(不管有多少个id相同的,都会一次性删除),再添加一条。
数据库中的id建议大家都用guid去掉“-”,还可以加日期“yyyyMMddHHmmss”这样组合,长度一致看起来美观,也充分保证唯一。
lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
标签:
原文地址:http://www.cnblogs.com/zjoch/p/4467910.html