lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)
添加:2013-12-25
更新:2013-12-26 新增分页功能。
更新:2013-12-27 新增按分类查询功能,调整索引行新增记录的图片字段。
//封装类
- 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_luce
- private 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 directory
- private 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 analyzer
- private 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 version
- private 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
- }
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_luce
private 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 directory
private 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 analyzer
private 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 version
private 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
}
//调用测试
- 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;
- }
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 学习笔记
lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)的更多相关文章
- lucene.net helper类 【结合盘古分词进行搜索的小例子(分页功能)】
转自:http://blog.csdn.net/pukuimin1226/article/details/17558247 添加:2013-12-25 更新:2013-12-26 新增分页功能. ...
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
转自:http://blog.csdn.net/pukuimin1226/article/details/17558247 添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新 ...
- Lucene.net入门学习(结合盘古分词)
Lucene简介 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整 ...
- Lucene.net入门学习(结合盘古分词)(转载)
作者:释迦苦僧 出处:http://www.cnblogs.com/woxpp/p/3972233.html 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显 ...
- 使用Lucene.net+盘古分词实现搜索查询
这里我的的Demo的逻辑是这样的:首先我基本的数据是储存在Sql数据库中,然后我把我的必需的数据推送到MongoDB中,这样再去利用Lucene.net+盘古创建索引:其中为什么要这样把数据推送到Mo ...
- 全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例
开发环境 vs2015 winform 程序 1 首先需要下载对应的DLL 文章后面统一提供程序下载地址 里面都有 2 配置pangu的参数 也可以不配置 采用默认的即可 3 创建索引,将索引存放到本 ...
- Lucene.Net+盘古分词->开发自己的搜索引擎
//封装类 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Lucene. ...
- 盘古分词修改支持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给修 ...
随机推荐
- Solr导入数据库数据
接Solr-4.10.2与Tomcat整合.1.在solrconfig.xml中添加数据导入节点,solrconfig.xml路径为D:\solr\data\solr\collection1\conf ...
- iOS_SN_深浅拷贝( 百度的)_转载
文章原地址:http://www.cnblogs.com/5ishare/p/4362459.html 深浅拷贝前提是:是实现NSCopying或者NSMutableCopying协议. 浅拷贝只是复 ...
- jquery cookie 删除不了的处理办法
$.cookie(name, null);$.cookie(name, null, {path : "/"}); Jquery Cookie的值直接设置null,并不能直接删除Co ...
- [转载]VIM命令合集
Vim命令合集 http://www.cnblogs.com/softwaretesting/archive/2011/07/12/2104435.html 命令历史 以:和/开头的命令都有历史纪录, ...
- nodejs 保存 payload 发送过来的文件
1:接受文件 http://stackoverflow.com/questions/24610996/how-to-get-uploaded-file-in-node-js-express-app-u ...
- 新花生壳+tomcat(内网映射,无需设置路由器)建站攻略
说明: 1.适用于内网用户(局域网,校园网,或者公司网等无法更改路由器映射的情况) 2.一共花了8块钱…………心疼.不过如果大家有钱的话,8块钱,少吃一顿麻辣烫就好了~总之,这个适用于测试网站,小访问 ...
- CSS3笔记(一)
最开始的时候 CSS3产生的一个新属性是一个浏览器的私有的,然后W3C 可能会拿来采用做个标准,再没公布标准之前就只能用私有属性(加前缀)来表达各自厂商的实现,主要是CSS3刚出现那会儿,它暗示该CS ...
- ubuntu下安装postgres
PostgreSQL 是一款强大的,开源的,对象关系型数据库系统.它支持所有的主流操作系统,包括 Linux.Unix(AIX.BSD.HP-UX,SGI IRIX.Mac OS.Solaris.Tr ...
- 正式学习react(二)
今天把上一篇还没学习完的 webpack部分学习完: 之前有说过关于css的webpack使用.我们讲了 ExtractTextPlugin 来单独管理css讲了module.loaders下关于 c ...
- java基础总结——数组
数组需要掌握的: 1.数组的定义 2.数组的内存分配及特点 3.数组操作常见问题 4.数组常见操作 5.数组中的数组(理解) 数组唯一属性:length,即数组的长度. 1.数组定义 格式一: 元素类 ...