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

添加:2013-12-25

更新:2013-12-26 新增分页功能。

更新:2013-12-27 新增按分类查询功能,调整索引行新增记录的图片字段。

//封装类

[csharp] view plaincopyprint?
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Lucene.Net.Analysis;
  6. using Lucene.Net.Index;
  7. using Lucene.Net.Documents;
  8. using System.Reflection;
  9. using Lucene.Net.QueryParsers;
  10. using Lucene.Net.Search;
  11. namespace SearchTest
  12. {
  13. /// <summary>
  14. /// 盘古分词在lucene.net中的使用帮助类
  15. /// 调用PanGuLuceneHelper.instance
  16. /// </summary>
  17. public class PanGuLuceneHelper
  18. {
  19. private PanGuLuceneHelper() { }
  20. #region 单一实例
  21. private static PanGuLuceneHelper _instance = null;
  22. /// <summary>
  23. /// 单一实例
  24. /// </summary>
  25. public static PanGuLuceneHelper instance
  26. {
  27. get
  28. {
  29. if (_instance == null) _instance = new PanGuLuceneHelper();
  30. return _instance;
  31. }
  32. }
  33. #endregion
  34. #region 分词测试
  35. /// <summary>
  36. /// 分词测试
  37. /// </summary>
  38. /// <param name="keyword"></param>
  39. /// <returns></returns>
  40. public string Token(string keyword)
  41. {
  42. string ret = "";
  43. System.IO.StringReader reader = new System.IO.StringReader(keyword);
  44. Lucene.Net.Analysis.TokenStream ts = analyzer.TokenStream(keyword, reader);
  45. bool hasNext = ts.IncrementToken();
  46. Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
  47. while (hasNext)
  48. {
  49. ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
  50. ret += ita.Term + "|";
  51. hasNext = ts.IncrementToken();
  52. }
  53. ts.CloneAttributes();
  54. reader.Close();
  55. analyzer.Close();
  56. return ret;
  57. }
  58. #endregion
  59. #region 创建索引
  60. /// <summary>
  61. /// 创建索引
  62. /// </summary>
  63. /// <param name="datalist"></param>
  64. /// <returns></returns>
  65. public bool CreateIndex(List<MySearchUnit> datalist)
  66. {
  67. IndexWriter writer = null;
  68. try
  69. {
  70. writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入)
  71. }
  72. catch
  73. {
  74. writer = new IndexWriter(directory_luce, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入)
  75. }
  76. foreach (MySearchUnit data in datalist)
  77. {
  78. CreateIndex(writer, data);
  79. }
  80. writer.Optimize();
  81. writer.Dispose();
  82. return true;
  83. }
  84. public bool CreateIndex(IndexWriter writer, MySearchUnit data)
  85. {
  86. try
  87. {
  88. if (data == null) return false;
  89. Document doc = new Document();
  90. Type type = data.GetType();//assembly.GetType("Reflect_test.PurchaseOrderHeadManageModel", true, true); //命名空间名称 + 类名
  91. //创建类的实例
  92. //object obj = Activator.CreateInstance(type, true);
  93. //获取公共属性
  94. PropertyInfo[] Propertys = type.GetProperties();
  95. for (int i = 0; i < Propertys.Length; i++)
  96. {
  97. //Propertys[i].SetValue(Propertys[i], i, null); //设置值
  98. PropertyInfo pi = Propertys[i];
  99. string name=pi.Name;
  100. object objval = pi.GetValue(data, null);
  101. string value = objval == null ? "" : objval.ToString(); //值
  102. if (name == "id" || name=="flag" )//id在写入索引时必是不分词,否则是模糊搜索和删除,会出现混乱
  103. {
  104. doc.Add(new Field(name, value, Field.Store.YES, Field.Index.NOT_ANALYZED));//id不分词
  105. }
  106. else
  107. {
  108. doc.Add(new Field(name, value, Field.Store.YES, Field.Index.ANALYZED));
  109. }
  110. }
  111. writer.AddDocument(doc);
  112. }
  113. catch (System.IO.FileNotFoundException fnfe)
  114. {
  115. throw fnfe;
  116. }
  117. return true;
  118. }
  119. #endregion
  120. #region 在title和content字段中查询数据
  121. /// <summary>
  122. /// 在title和content字段中查询数据
  123. /// </summary>
  124. /// <param name="keyword"></param>
  125. /// <returns></returns>
  126. public List<MySearchUnit> Search(string keyword)
  127. {
  128. string[] fileds = { "title", "content" };//查询字段
  129. //Stopwatch st = new Stopwatch();
  130. //st.Start();
  131. QueryParser parser = null;// new QueryParser(Lucene.Net.Util.Version.LUCENE_30, field, analyzer);//一个字段查询
  132. parser = new MultiFieldQueryParser(version, fileds, analyzer);//多个字段查询
  133. Query query = parser.Parse(keyword);
  134. int n = 1000;
  135. IndexSearcher searcher = new IndexSearcher(directory_luce, true);//true-表示只读
  136. TopDocs docs = searcher.Search(query, (Filter)null, n);
  137. if (docs == null || docs.TotalHits == 0)
  138. {
  139. return null;
  140. }
  141. else
  142. {
  143. List<MySearchUnit> list = new List<MySearchUnit>();
  144. int counter = 1;
  145. foreach (ScoreDoc sd in docs.ScoreDocs)//遍历搜索到的结果
  146. {
  147. try
  148. {
  149. Document doc = searcher.Doc(sd.Doc);
  150. string id = doc.Get("id");
  151. string title = doc.Get("title");
  152. string content = doc.Get("content");
  153. string flag = doc.Get("flag");
  154. string imageurl = doc.Get("imageurl");
  155. string updatetime = doc.Get("updatetime");
  156. string createdate = doc.Get("createdate");
  157. PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
  158. PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
  159. highlighter.FragmentSize = 50;
  160. content = highlighter.GetBestFragment(keyword, content);
  161. string titlehighlight = highlighter.GetBestFragment(keyword, title);
  162. if (titlehighlight != "") title = titlehighlight;
  163. list.Add(new MySearchUnit(id, title, content, flag,imageurl, updatetime));
  164. }
  165. catch (Exception ex)
  166. {
  167. Console.WriteLine(ex.Message);
  168. }
  169. counter++;
  170. }
  171. return list;
  172. }
  173. //st.Stop();
  174. //Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>");
  175. }
  176. #endregion
  177. #region 在不同的分类下再根据title和content字段中查询数据(分页)
  178. /// <summary>
  179. /// 在不同的类型下再根据title和content字段中查询数据(分页)
  180. /// </summary>
  181. /// <param name="_flag">分类,传空值查询全部</param>
  182. /// <param name="keyword"></param>
  183. /// <param name="PageIndex"></param>
  184. /// <param name="PageSize"></param>
  185. /// <param name="TotalCount"></param>
  186. /// <returns></returns>
  187. public List<MySearchUnit> Search(string _flag,string keyword, int PageIndex, int PageSize, out int TotalCount)
  188. {
  189. if (PageIndex < 1) PageIndex = 1;
  190. //Stopwatch st = new Stopwatch();
  191. //st.Start();
  192. BooleanQuery bq = new BooleanQuery();
  193. if (_flag != "")
  194. {
  195. QueryParser qpflag = new QueryParser(version, "flag", analyzer);
  196. Query qflag = qpflag.Parse(_flag);
  197. bq.Add(qflag, Occur.MUST);//与运算
  198. }
  199. if (keyword != "")
  200. {
  201. string[] fileds = { "title", "content" };//查询字段
  202. QueryParser parser = null;// new QueryParser(version, field, analyzer);//一个字段查询
  203. parser = new MultiFieldQueryParser(version, fileds, analyzer);//多个字段查询
  204. Query queryKeyword = parser.Parse(keyword);
  205. bq.Add(queryKeyword, Occur.MUST);//与运算
  206. }
  207. TopScoreDocCollector collector = TopScoreDocCollector.Create(PageIndex * PageSize, false);
  208. IndexSearcher searcher = new IndexSearcher(directory_luce, true);//true-表示只读
  209. searcher.Search(bq, collector);
  210. if (collector == null || collector.TotalHits == 0)
  211. {
  212. TotalCount = 0;
  213. return null;
  214. }
  215. else
  216. {
  217. int start = PageSize * (PageIndex - 1);
  218. //结束数
  219. int limit = PageSize;
  220. ScoreDoc[] hits = collector.TopDocs(start, limit).ScoreDocs;
  221. List<MySearchUnit> list = new List<MySearchUnit>();
  222. int counter = 1;
  223. TotalCount = collector.TotalHits;
  224. foreach (ScoreDoc sd in hits)//遍历搜索到的结果
  225. {
  226. try
  227. {
  228. Document doc = searcher.Doc(sd.Doc);
  229. string id = doc.Get("id");
  230. string title = doc.Get("title");
  231. string content = doc.Get("content");
  232. string flag = doc.Get("flag");
  233. string imageurl = doc.Get("imageurl");
  234. string updatetime = doc.Get("updatetime");
  235. PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
  236. PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new PanGu.Segment());
  237. highlighter.FragmentSize = 50;
  238. content = highlighter.GetBestFragment(keyword, content);
  239. string titlehighlight = highlighter.GetBestFragment(keyword, title);
  240. if (titlehighlight != "") title = titlehighlight;
  241. list.Add(new MySearchUnit(id, title, content, flag,imageurl, updatetime));
  242. }
  243. catch (Exception ex)
  244. {
  245. Console.WriteLine(ex.Message);
  246. }
  247. counter++;
  248. }
  249. return list;
  250. }
  251. //st.Stop();
  252. //Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>");
  253. }
  254. #endregion
  255. #region 删除索引数据(根据id)
  256. /// <summary>
  257. /// 删除索引数据(根据id)
  258. /// </summary>
  259. /// <param name="id"></param>
  260. /// <returns></returns>
  261. public bool Delete(string id)
  262. {
  263. bool IsSuccess = false;
  264. Term term = new Term("id", id);
  265. //Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
  266. //Version version = new Version();
  267. //MultiFieldQueryParser parser = new MultiFieldQueryParser(version, new string[] { "name", "job" }, analyzer);//多个字段查询
  268. //Query query = parser.Parse("小王");
  269. //IndexReader reader = IndexReader.Open(directory_luce, false);
  270. //reader.DeleteDocuments(term);
  271. //Response.Write("删除记录结果: " + reader.HasDeletions + "<br/>");
  272. //reader.Dispose();
  273. IndexWriter writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);
  274. writer.DeleteDocuments(term); // writer.DeleteDocuments(term)或者writer.DeleteDocuments(query);
  275. writer.DeleteAll();
  276. writer.Commit();
  277. //writer.Optimize();//
  278. IsSuccess = writer.HasDeletions();
  279. writer.Dispose();
  280. return IsSuccess;
  281. }
  282. #endregion
  283. #region 删除全部索引数据
  284. /// <summary>
  285. /// 删除全部索引数据
  286. /// </summary>
  287. /// <returns></returns>
  288. public bool DeleteAll()
  289. {
  290. bool IsSuccess = true;
  291. try
  292. {
  293. IndexWriter writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);
  294. writer.DeleteAll();
  295. writer.Commit();
  296. //writer.Optimize();//
  297. IsSuccess = writer.HasDeletions();
  298. writer.Dispose();
  299. }
  300. catch
  301. {
  302. IsSuccess = false;
  303. }
  304. return IsSuccess;
  305. }
  306. #endregion
  307. #region directory_luce
  308. private Lucene.Net.Store.Directory _directory_luce = null;
  309. /// <summary>
  310. /// Lucene.Net的目录-参数
  311. /// </summary>
  312. public Lucene.Net.Store.Directory directory_luce
  313. {
  314. get
  315. {
  316. if (_directory_luce == null) _directory_luce = Lucene.Net.Store.FSDirectory.Open(directory);
  317. return _directory_luce;
  318. }
  319. }
  320. #endregion
  321. #region directory
  322. private System.IO.DirectoryInfo _directory = null;
  323. /// <summary>
  324. /// 索引在硬盘上的目录
  325. /// </summary>
  326. public System.IO.DirectoryInfo directory
  327. {
  328. get
  329. {
  330. if (_directory == null)
  331. {
  332. string dirPath = AppDomain.CurrentDomain.BaseDirectory + "SearchIndex";
  333. if (System.IO.Directory.Exists(dirPath) == false) _directory = System.IO.Directory.CreateDirectory(dirPath);
  334. else _directory = new System.IO.DirectoryInfo(dirPath);
  335. }
  336. return _directory;
  337. }
  338. }
  339. #endregion
  340. #region analyzer
  341. private Analyzer _analyzer = null;
  342. /// <summary>
  343. /// 分析器
  344. /// </summary>
  345. public Analyzer analyzer
  346. {
  347. get
  348. {
  349. //if (_analyzer == null)
  350. {
  351. _analyzer = new Lucene.Net.Analysis.PanGu.PanGuAnalyzer();//盘古分词分析器
  352. //_analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);//标准分析器
  353. }
  354. return _analyzer;
  355. }
  356. }
  357. #endregion
  358. #region version
  359. private static Lucene.Net.Util.Version _version = Lucene.Net.Util.Version.LUCENE_30;
  360. /// <summary>
  361. /// 版本号枚举类
  362. /// </summary>
  363. public Lucene.Net.Util.Version version
  364. {
  365. get
  366. {
  367. return _version;
  368. }
  369. }
  370. #endregion
  371. }
  372. #region 索引的一个行单元,相当于数据库中的一行数据
  373. /// <summary>
  374. /// 索引的一个行单元,相当于数据库中的一行数据
  375. /// </summary>
  376. public class MySearchUnit
  377. {
  378. public MySearchUnit(string _id, string _title, string _content, string _flag, string _imageurl, string _updatetime)
  379. {
  380. this.id = _id;
  381. this.title = _title;
  382. this.content = _content;
  383. this.flag = _flag;
  384. this.imageurl = _imageurl;
  385. this.updatetime = _updatetime;
  386. }
  387. /// <summary>
  388. /// 唯一的id号
  389. /// </summary>
  390. public string id { get; set; }
  391. /// <summary>
  392. /// 标题
  393. /// </summary>
  394. public string title { get; set; }
  395. /// <summary>
  396. /// 内容
  397. /// </summary>
  398. public string content { get; set; }
  399. /// <summary>
  400. /// 其他信息
  401. /// </summary>
  402. public string flag { get; set; }
  403. /// <summary>
  404. /// 图片路径
  405. /// </summary>
  406. public string imageurl { get; set; }
  407. /// <summary>
  408. /// 时间
  409. /// </summary>
  410. public string updatetime { get; set; }
  411. }
  412. #endregion
  413. }
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
{/// <summary>/// 盘古分词在lucene.net中的使用帮助类/// 调用PanGuLuceneHelper.instance/// </summary>public class PanGuLuceneHelper{private PanGuLuceneHelper() { }#region 单一实例private static PanGuLuceneHelper _instance = null;/// <summary>/// 单一实例/// </summary>public static PanGuLuceneHelper instance{get{if (_instance == null) _instance = new PanGuLuceneHelper();return _instance;}}#endregion#region 分词测试/// <summary>/// 分词测试/// </summary>/// <param name="keyword"></param>/// <returns></returns>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 创建索引/// <summary>/// 创建索引/// </summary>/// <param name="datalist"></param>/// <returns></returns>public bool CreateIndex(List<MySearchUnit> datalist){IndexWriter writer = null;try{writer = new IndexWriter(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入)}catch{writer = new IndexWriter(directory_luce, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入)}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();//assembly.GetType("Reflect_test.PurchaseOrderHeadManageModel", true, true); //命名空间名称 + 类名    //创建类的实例    //object obj = Activator.CreateInstance(type, true);  //获取公共属性    PropertyInfo[] Propertys = type.GetProperties();for (int i = 0; i < Propertys.Length; i++){//Propertys[i].SetValue(Propertys[i], i, null); //设置值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" )//id在写入索引时必是不分词,否则是模糊搜索和删除,会出现混乱{doc.Add(new Field(name, value, Field.Store.YES, Field.Index.NOT_ANALYZED));//id不分词}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字段中查询数据/// <summary>/// 在title和content字段中查询数据/// </summary>/// <param name="keyword"></param>/// <returns></returns>public List<MySearchUnit> Search(string keyword){string[] fileds = { "title", "content" };//查询字段//Stopwatch st = new Stopwatch();//st.Start();QueryParser parser = null;// new QueryParser(Lucene.Net.Util.Version.LUCENE_30, field, analyzer);//一个字段查询parser = new MultiFieldQueryParser(version, fileds, analyzer);//多个字段查询Query query = parser.Parse(keyword);int n = 1000;IndexSearcher searcher = new IndexSearcher(directory_luce, true);//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;}//st.Stop();//Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>");}#endregion#region 在不同的分类下再根据title和content字段中查询数据(分页)/// <summary>/// 在不同的类型下再根据title和content字段中查询数据(分页)/// </summary>/// <param name="_flag">分类,传空值查询全部</param>/// <param name="keyword"></param>/// <param name="PageIndex"></param>/// <param name="PageSize"></param>/// <param name="TotalCount"></param>/// <returns></returns>public List<MySearchUnit> Search(string _flag,string keyword, int PageIndex, int PageSize, out int TotalCount){if (PageIndex < 1) PageIndex = 1;//Stopwatch st = new Stopwatch();//st.Start();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;// new QueryParser(version, field, analyzer);//一个字段查询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);//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;}//st.Stop();//Response.Write("查询时间:" + st.ElapsedMilliseconds + " 毫秒<br/>");}#endregion#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(directory_luce, analyzer, 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(directory_luce, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);writer.DeleteAll();writer.Commit();//writer.Optimize();//IsSuccess = writer.HasDeletions();writer.Dispose();}catch{IsSuccess = false;}return IsSuccess;}#endregion#region directory_luceprivate Lucene.Net.Store.Directory _directory_luce = null;/// <summary>/// Lucene.Net的目录-参数/// </summary>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 directoryprivate System.IO.DirectoryInfo _directory = null;/// <summary>/// 索引在硬盘上的目录/// </summary>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 analyzerprivate Analyzer _analyzer = null;/// <summary>/// 分析器/// </summary>public Analyzer analyzer{get{//if (_analyzer == null){_analyzer = new Lucene.Net.Analysis.PanGu.PanGuAnalyzer();//盘古分词分析器//_analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);//标准分析器}return _analyzer;}} #endregion#region versionprivate static Lucene.Net.Util.Version _version = Lucene.Net.Util.Version.LUCENE_30;/// <summary>/// 版本号枚举类/// </summary>public Lucene.Net.Util.Version version{get{return _version;}}#endregion}#region 索引的一个行单元,相当于数据库中的一行数据/// <summary>/// 索引的一个行单元,相当于数据库中的一行数据/// </summary>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;}/// <summary>/// 唯一的id号/// </summary>public string id { get; set; }/// <summary>/// 标题/// </summary>public string title { get; set; }/// <summary>/// 内容/// </summary>public string content { get; set; }/// <summary>/// 其他信息/// </summary>public string flag { get; set; }/// <summary>/// 图片路径/// </summary>public string imageurl { get; set; }/// <summary>/// 时间/// </summary>public string updatetime { get; set; }} #endregion
}

//调用测试

[csharp] view plaincopyprint?
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. //PanGuLuceneHelper.instance.DeleteAll();//删除全部
  4. //PanGuLuceneHelper.instance.Delete("1d");//根据id删除
  5. bool exec = false;
  6. if (exec)
  7. {
  8. List<MySearchUnit> list = new List<MySearchUnit>();
  9. list.Add(new MySearchUnit("1a", "标题小王", "今天是小王的生日,大家都很高兴去他家喝酒,玩了一整天。", new Random().Next(1, 10).ToString(), "", ""));
  10. list.Add(new MySearchUnit("1b", "标题小张", "今天是小张的生日,大家都很高兴去他家喝酒,玩了几天。", new Random().Next(1, 10).ToString(), "", ""));
  11. list.Add(new MySearchUnit("1c", "标题小王", "今天是小王的生日,大家都很高兴去他家喝酒,玩了一整天。", new Random().Next(1, 10).ToString(), "", ""));
  12. list.Add(new MySearchUnit("1d", "标题小张", "今天是小张的生日,大家都很高兴去他家喝酒,玩了几天。", new Random().Next(1, 10).ToString(), "", ""));
  13. PanGuLuceneHelper.instance.CreateIndex(list);//添加索引
  14. }
  15. int count = 0;
  16. int PageIndex=2;
  17. int PageSize=4;
  18. string html_content = "";
  19. List<MySearchUnit> searchlist = PanGuLuceneHelper.instance.Search("3","小王 生日",PageIndex,PageSize,out count);
  20. html_content+=("查询结果:" + count + "条数据<br/>");
  21. if (searchlist == null || searchlist.Count==0)
  22. {
  23. html_content += ("未查询到数据。<br/>");
  24. }
  25. else
  26. {
  27. foreach (MySearchUnit data in searchlist)
  28. {
  29. 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));
  30. }
  31. }
  32. html_content += (PanGuLuceneHelper.instance.version);
  33. div_content.InnerHtml = html_content;
  34. }
       protected void Page_Load(object sender, EventArgs e){//PanGuLuceneHelper.instance.DeleteAll();//删除全部//PanGuLuceneHelper.instance.Delete("1d");//根据id删除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 教程(转载)
Lucene(.net)学习
Lucene的缺点
web.config 学习之 httpHandlers
LUCENE 3.6 学习笔记
Lucene小练九——各种搜索(精确,范围,数字)
Lucene小练三——索引删除,恢复,更新
在 Asp.NET MVC 中使用 SignalR 实现推送功能
FieldCache在lucene中使用的代码解析,使用场景个人分析
Lucene3.0.1 学习笔记

转载于:https://www.cnblogs.com/sumg/p/4030036.html

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

  1. 全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例

    开发环境 vs2015 winform 程序 1 首先需要下载对应的DLL 文章后面统一提供程序下载地址 里面都有 2 配置pangu的参数 也可以不配置 采用默认的即可 3 创建索引,将索引存放到本 ...

  2. 盘古分词-关键字搜索没有结果(关键字由未收录词组成)

    由未收录词A.B.C等组成的任意组合在搜索时没有结果的问题. 解决方案很简单在设置中UnknownWordIdentify选项置为False就可以了. 下面是Pangu代码中处理段: if (coun ...

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

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

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

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

  5. Lucene.Net3.0.3+盘古分词器学习使用

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

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

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

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

    Lucene.Net+盘古分词器(详细介绍)(转) Lucene.Net+盘古分词器(详细介绍) 本章阅读概要 1.Lucenne.Net简介 2.介绍盘古分词器 3.Lucene.Net实例分析 4 ...

  8. java盘古分词_.NET使用Lucene.Net和盘古分词类库实现中文分词

    .NET中文分词实现http://http:// 使用 Lucene.Net.dll http://www.apache.org/dist/incubator/lucene.net/binaries/ ...

  9. Lucene.net和盘古分词使用小结

    盘古分词是开源项目,核心技术基于Lucene.net.虽然有点旧(2010年),但是还是可以用的.案例.应用程序.以及源码可以详见以下链接. http://pangusegment.codeplex. ...

  10. Lucence.Net学习+盘古分词

    创建索引库 //读取文件,存储到索引库 public string CreateDatebase() { //获取索引库的路径 var indexPath = AppDomain.CurrentDom ...

最新文章

  1. mac上mysql关闭不了了_python操作mysql数据库
  2. 《深入浅出Nodejs》—— 读后总结
  3. 自学python入门-自学Python编程基础学习笔记 PDF 完整超清版
  4. 试试Linux下的ip命令,ifconfig已经过时了
  5. 07-CoreData清除所有数据
  6. 创建型模式—单例模式
  7. delphi开发LINUX程序,DELPHI开发LINUX包
  8. JZOJ 3457. 【NOIP2013模拟联考3】沙耶的玩偶(doll)
  9. 解决 HttpClient 模拟 http 的get 请求后 ,出现 403 错误
  10. iOS 8 Xcode6 设置Launch Image 启动图片转
  11. Docker系列(四)守护式容器
  12. gsonformat java代码_插件GsonFormat快速實現JavaBean
  13. 分享10个值得每天一看的精品网站,可以让你全方面得到提升,每一个都会让你大开眼界。
  14. 如何使用SPSS进行判别分析
  15. java怎么判断素数_java判断是否为素数(质数)的方法
  16. Crystal Reports - 根据模板导出PDF文件
  17. 小程序发布文章-微信小程序视频教程28
  18. 手把手带你学微信小程序 —— 如何开发属于自己的第三方微信小程序组件库
  19. 机器学习笔记之基础概念
  20. ros使用usb摄像头追踪ArUco markers

热门文章

  1. Dubbo本地伪装 Mock
  2. jquery validation用法
  3. keil5图标变成白色_【网上最简单】Chrome安装后打不开任何页面 amp; 改名后图标变成小白块[30秒解决]...
  4. MyBatis的XML配置文件(二)
  5. 开启MyBatis(一)
  6. 【渝粤教育】国家开放大学2018年秋季 1039t高级财务会计 参考试题
  7. 【渝粤教育】国家开放大学2018年春季 0579-21T电路及磁路(2)(一) 参考试题
  8. [渝粤教育] 中国地质大学 管理信息系统 复习题
  9. 【sklearn第十八讲】神经网络模型
  10. pku 2348 Euclid's Game