MVC5 框架 配置 盘古分词

时间:2023-11-26 10:37:56

2018.5.10日记

1.将sql数据库的内容添加到索引库中,

 public static readonly IndexManager instance;
//静态构造函数,CLR只执行一次
static IndexManager()
{
instance = new IndexManager();
}
//私有构造函数,防止外界调用
private IndexManager() { } /// <summary>
/// 定义 实体队列
/// </summary>
public Queue<ViewModelContent> queue = new Queue<ViewModelContent>();
public void DeleteQuene(int id)
{
ViewModelContent viewmodelcontent = new ViewModelContent() {
Id=id,LuceneTypeEnum=LuceneTypeEnum.Delete
};
queue.Enqueue(viewmodelcontent);
}
/// <summary>
/// 新增到数据库 成功后,需要将该记录新增到lucene.net中
/// </summary>
/// <param name="id"></param>
/// <param name="title"></param>
/// <param name="content"></param>
public void AddQueue(int id,string title,string content)
{
ViewModelContent viewmodelcontent = new ViewModelContent()
{
Id = id,
Title = title,
Content = content,
LuceneTypeEnum=LuceneTypeEnum.Add
};
queue.Enqueue(viewmodelcontent);
}
/// <summary>
/// 开启线程
/// </summary>
public void StartThread()
{
Thread thread = new Thread(WriteCotent);
thread.IsBackground = true;
thread.Start();
}
/// <summary>
/// 循环扫描队列
/// </summary>
private void WriteCotent()
{
while (true)
{
if (queue.Count > 0)
{
//创建索引库
CreateIndexContent();
}
else {
Thread.Sleep(3000);
}
}
}
/// <summary>
/// 创建索引库
/// </summary>
private void CreateIndexContent()
{
string indexPath = ConfigurationManager.AppSettings["LuceneDir"];
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
bool isUpdate = IndexReader.IndexExists(directory);
if (isUpdate)
{
if (IndexWriter.IsLocked(directory))
{
IndexWriter.Unlock(directory);
}
}
IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);//向索引库中写索引。这时在这里加锁。
//现在的数据 是从队列里面来 就是循环队列
while (queue.Count > 0)
{
//出队
ViewModelContent viewmodecontent = queue.Dequeue();
//根据ID 删除lucene.net的数据
writer.DeleteDocuments(new Term("Id", viewmodecontent.Id.ToString()));
if (viewmodecontent.LuceneTypeEnum == LuceneTypeEnum.Delete)
{
continue;
}
//string txt = File.ReadAllText(@"C:\Users\Administrator\Desktop\lucene.net\" + i + ".txt", System.Text.Encoding.Default);//注意这个地方的编码
Document document = new Document();//表示一篇文档。
//Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
document.Add(new Field("Id", viewmodecontent.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
//Field.Index. ANALYZED:进行分词保存:也就是要进行全文的字段要设置分词 保存(因为要进行模糊查询)
//Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
document.Add(new Field("Title", viewmodecontent.Title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("Content", viewmodecontent.Content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document);
}
writer.Close();//会自动解锁。
directory.Close();//不要忘了Close,否则索引结果搜不到
}
}
}

3.分词搜索

        //分词搜索
// GET: Search
public ActionResult SearchContent()
{
return View();
} public ActionResult SearchContentByKw()
{
//判断
if (!string.IsNullOrEmpty(Request["btnSearch"]))
{
//查询搜索内容
List<ViewModelContent> list = ShowSearchContent();
ViewData["list"] = list;
return View("SearchContent");
}
else
{
//创建词库
CreateSerachContent();
}
return Content("ok");
} private List<ViewModelContent> ShowSearchContent()
{
string indexPath = ConfigurationManager.AppSettings["LuceneDir"];
List<string> list = Common.WebCommon.PanGuSplitWord(Request["txtSearch"]);//对用户输入的搜索条件进行拆分。
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
//搜索条件
PhraseQuery query = new PhraseQuery();
foreach (string word in list)//先用空格,让用户去分词,空格分隔的就是词“计算机 专业”
{
query.Add(new Term("Content", word));
}
//query.Add(new Term("body","语言"));--可以添加查询条件,两者是add关系.顺序没有关系.
// query.Add(new Term("body", "大学生"));
// query.Add(new Term("body", kw));//body中含有kw的文章
query.SetSlop();//多个查询条件的词之间的最大距离.在文章中相隔太远 也就无意义.(例如 “大学生”这个查询条件和"简历"这个查询条件之间如果间隔的词太多也就没有意义了。)
//TopScoreDocCollector是盛放查询结果的容器
TopScoreDocCollector collector = TopScoreDocCollector.create(, true);
searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器
ScoreDoc[] docs = collector.TopDocs(, collector.GetTotalHits()).scoreDocs;//得到所有查询结果中的文档,GetTotalHits():表示总条数 TopDocs(300, 20);//表示得到300(从300开始),到320(结束)的文档内容.
//可以用来实现分页功能
List<ViewModelContent> viewModelList = new List<ViewModelContent>();
for (int i = ; i < docs.Length; i++)
{
//
//搜索ScoreDoc[]只能获得文档的id,这样不会把查询结果的Document一次性加载到内存中。降低了内存压力,需要获得文档的详细内容的时候通过searcher.Doc来根据文档id来获得文档的详细内容对象Document.
ViewModelContent viewModel = new ViewModelContent();
int docId = docs[i].doc;//得到查询结果文档的id(Lucene内部分配的id)
Document doc = searcher.Doc(docId);//找到文档id对应的文档详细信息
viewModel.Id = Convert.ToInt32(doc.Get("Id"));// 取出放进字段的值
viewModel.Title = doc.Get("Title");
viewModel.Content = Common.WebCommon.CreateHightLight(Request["txtSearch"], doc.Get("Content"));//将搜索的关键字高亮显示。
viewModelList.Add(viewModel);
}
return viewModelList;
} public void CreateSerachContent()
{ }

4.高亮显示关键词

public  class WebCommon
{
public static List<string> PanGuSplitWord(string msg)
{
Analyzer analyzer = new PanGuAnalyzer();
TokenStream tokenStream = analyzer.TokenStream("", new StringReader(msg));
Lucene.Net.Analysis.Token token = null;
List<string> list = new List<string>();
while ((token = tokenStream.Next()) != null)
{
list.Add(token.TermText());
}
return list;
}
// /创建HTMLFormatter,参数为高亮单词的前后缀
public static string CreateHightLight(string keywords, string Content)
{
PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
//创建Highlighter ,输入HTMLFormatter 和盘古分词对象Semgent
PanGu.HighLight.Highlighter highlighter =
new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
new Segment());
//设置每个摘要段的字符数
highlighter.FragmentSize = ;
//获取最匹配的摘要段
return highlighter.GetBestFragment(keywords, Content); }
}

5.Global开启线程

      //new IndexManager().StartThread();
IndexManager.instance.StartThread();

6.web.config文件配置

        <add key="LuceneDir" value="E:\lucenedir"/>