OA项目第七天

LUCENE.NET的使用,添加词库,实现搜索,使用队列添加词库

OA项目第七天

LUCENE.NET

C9DkxH.png

C9DEMd.png

我们可以在官网找到最新的“卢司令”,也可以在negut包下载。卢司令自带一级分词,也就是文本全分单个单词。但是我们这里用的是盘古分词,比一般的分词更加智能,而且可以自己自定义添加分词。

用卢司令的一级分词,具体的可以看这里:http://www.zhuzhusoft.com/article.php?id=151(2.0版本和3.0后的版本有点api的差异)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static void Token()
{
Console.WriteLine("测试一元分词");
Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
string str = "my name is julian.zhu,欢迎访问我的个人网站www.zhuzhusoft.com";
StringReader reader = new System.IO.StringReader(str);
TokenStream ts = analyzer.TokenStream(str, reader);
bool hasNext = ts.IncrementToken();
Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
while (hasNext)
{
ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
Console.Write(ita.Term + "|");
hasNext = ts.IncrementToken();
}
Console.WriteLine();
ts.CloneAttributes();
reader.Close();
analyzer.Close();

}

盘古分词用法也是一样,只需要改成

1
2
3
Console.WriteLine("测试盘古分词分词");
Analyzer analyzer = new PanGuAnalyzer();//这里的盘古版本必须跟3.3版本兼容
....

添加词库测试

这下面的代码考虑的只是单人操作,因此它在添加的时候是加了锁的,但是还没有写完,这时候又来一个请求,那么就解锁了,不适合并发。我们在后面会对代码进行修改(队列的考虑:什么时候再队列里写数据,写的是什么数据。在什么情况下取出来写到文件上)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private void button1_Click(object sender, EventArgs e)
{
string indexPath = @"F:\vsproject\FenCiTest\WindowsFormsApp1\lucenedir";//注意和磁盘上文件夹的大小写一致,否则会报错。将创建的分词内容放在该目录下。
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());//指定索引文件(打开索引目录) FS指的是就是FileSystem
bool isUpdate = IndexReader.IndexExists(directory);//IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。
if (isUpdate)
{
//同时只能有一段代码对索引库进行写操作。当使用IndexWriter打开directory时会自动对索引库文件上锁。
//如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这个问题后面会解决)
if (IndexWriter.IsLocked(directory))
{
IndexWriter.Unlock(directory);
}
}
IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);//向索引库中写索引。这时在这里加锁。
for (int i = 1; i <= 3; i++)
{
string txt = File.ReadAllText(@"F:\vsproject\FenCiTest\WindowsFormsApp1\书籍" + 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("number", i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

//Field.Index. ANALYZED:进行分词保存:也就是要进行全文的字段要设置分词 保存(因为要进行模糊查询)

//Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
document.Add(new Field("body", txt, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document);

}
writer.Dispose();//会自动解锁。
directory.Dispose();//不要忘了Close,否则索引结果搜不到
}

搜索测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private void button2_Click(object sender, EventArgs e)
{
string indexPath = @"F:\vsproject\FenCiTest\WindowsFormsApp1\lucenedir";
string kw = "面向对象";//对用户输入的搜索条件进行拆分。
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 kw.Split(' '))//先用空格,让用户去分词,空格分隔的就是词“计算机 专业”
//{
// query.Add(new Term("body", word));
//}
//query.Add(new Term("body","语言"));--可以添加查询条件,两者是add关系.顺序没有关系.
// query.Add(new Term("body", "大学生"));
query.Add(new Term("body", kw));//body中含有kw的文章
query.Slop = 100;//多个查询条件的词之间的最大距离.在文章中相隔太远 也就无意义.(例如 “大学生”这个查询条件和"简历"这个查询条件之间如果间隔的词太多也就没有意义了。)
//TopScoreDocCollector是盛放查询结果的容器
TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true);
searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器
ScoreDoc[] docs = collector.TopDocs(0, collector.TotalHits).ScoreDocs;//得到所有查询结果中的文档,GetTotalHits():表示总条数 TopDocs(300, 20);//表示得到300(从300开始),到320(结束)的文档内容.
//可以用来实现分页功能
this.listBox1.Items.Clear();
for (int i = 0; i < docs.Length; i++)
{
//
//搜索ScoreDoc[]只能获得文档的id,这样不会把查询结果的Document一次性加载到内存中。降低了内存压力,需要获得文档的详细内容的时候通过searcher.Doc来根据文档id来获得文档的详细内容对象Document.
int docId = docs[i].Doc;//得到查询结果文档的id(Lucene内部分配的id)
Document doc = searcher.Doc(docId);//找到文档id对应的文档详细信息
this.listBox1.Items.Add(doc.Get("number") + "\n");// 取出放进字段的值
this.listBox1.Items.Add(doc.Get("body") + "\n");
this.listBox1.Items.Add("-----------------------\n");
}
}

实现搜索

首先我们新建一个Search的控制器,前端的view视图如下,添加了一些简单的样式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
.search-text2{ display:block; width:528px; height:26px; line-height:26px; float:left; margin:3px 5px; border:1px solid gray; outline:none; font-family:'Microsoft Yahei'; font-size:14px;}
.search-btn2{width:102px; height:32px; line-height:32px; cursor:pointer; border:0px; background-color:#d6000f;font-family:'Microsoft Yahei'; font-size:16px;color:#f3f3f3;}
.search-list-con{width:640px; background-color:#fff; overflow:hidden; margin-top:0px; padding-bottom:15px; padding-top:5px;}
.search-list{width:600px; overflow:hidden; margin:15px 20px 0px 20px;}

.search-list dt{font-family:'Microsoft Yahei'; font-size:16px; line-height:20px; margin-bottom:7px; font-weight:normal;}
.search-list dt a{color:#2981a9;}
.search-list dt a em{ font-style:normal; color:#cc0000;}
</style>
</head>
<body>
<div>
<form method="post" action="/Search/SearchContent">
<input type="text" name="searchtxt" class="search-text2" />
<input type="submit" value="搜一搜" class="search-btn2" >
<input type="submit" value="生成词库">//这里不能这样直接用一个点击按钮生成词库
</form>
<br/>
<div class="search-list-con">
@{
List<ViewModelContent> ls = (List<ViewModelContent>) ViewData["list"];
if (ls != null)
{
foreach (ViewModelContent ViewModelContent in ls)
{
<dl class="search-list">
<dt><a href="javascript:void(0)" >@MvcHtmlString.Create(ViewModelContent.Title)</a></dt>
<dt>@MvcHtmlString.Create(ViewModelContent.Content)</dt>
</dl>
}
}
}
</div>

</div>
</body>

控制器的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class SearchController : Controller
{
private IBLL.IBooksService bll { get; set; }//记得在config里面配置

// GET: Search
public ActionResult Index()
{
return View();
}

public ActionResult SearchContent()
{
if (string.IsNullOrEmpty(Request["searchtxt"]))//如果搜索为空就代表是生成词库
{
CreateSerachContent();//这里我们改写在IndexManager这个类里面去生成词库
}
else
{
List<ViewModelContent> ls= ShowSearchContent();
ViewData["list"] = ls;//把要显示的搜索结果放入ViewData
return View("Index");
}

return Content("OK");
}
//显示搜索结果
private List<ViewModelContent> ShowSearchContent()
{
string indexPath = Request.MapPath("/lucenedir"); ;
// string kw = Request["searchtxt"];//对用户输入的搜索条件进行拆分。
List<string> lists = Common.WebCommon.PanGuSplitWord(Request["searchtxt"]);//对用户输入的搜索条件进行拆分。
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 lists)//先用空格,让用户去分词,空格分隔的就是词“计算机 专业”
{
query.Add(new Term("Content", word));
}
query.Slop = 100;//多个查询条件的词之间的最大距离.在文章中相隔太远 也就无意义.(例如 “大学生”这个查询条件和"简历"这个查询条件之间如果间隔的词太多也就没有意义了。)
//TopScoreDocCollector是盛放查询结果的容器
TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true);
searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器
ScoreDoc[] docs = collector.TopDocs(0, collector.TotalHits).ScoreDocs;//得到所有查询结果中的文档,GetTotalHits():表示总条数 TopDocs(300, 20);//表示得到300(从300开始),到320(结束)的文档内容.
List<ViewModelContent> list = new List<ViewModelContent>();
for (int i = 0; i < docs.Length; i++)
{
ViewModelContent vm = new ViewModelContent();
//搜索ScoreDoc[]只能获得文档的id,这样不会把查询结果的Document一次性加载到内存中。降低了内存压力,需要获得文档的详细内容的时候通过searcher.Doc来根据文档id来获得文档的详细内容对象Document.
int docId = docs[i].Doc;//得到查询结果文档的id(Lucene内部分配的id)
Document doc = searcher.Doc(docId);//找到文档id对应的文档详细信息
vm.Id= Convert.ToInt32(doc.Get("Id"));// 取出放进字段的值
vm.Title = doc.Get("Title");
vm.Content = WebCommon.CreateHightLight(Request["searchtxt"], doc.Get("Content"));
list.Add(vm);
}

return list;
}

//生成词库
private void CreateSerachContent()
{
//不能直接用按钮生成,改在IndexManager类生成
}
}

T4模板的简单提示

navigationProperties 导航属性

BeginNamespace 导入命名空间

关于文件并发

24EhpG.png

在上方的生成词库我们发现了一个多线程并发的问题,就是如果索引目录被锁定,现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这就需要我们用队列来解决。(队列的考虑:什么时候再队列里写数据,写的是什么数据。在什么情况下取出来写到文件上)

IndexManager类改写词库的生成(添加词库),注意一点!!!!!其他类访问web程序文件夹可以使用HttpRuntime.AppDomainAppPath+“//需要的文件夹”,,记得转义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
public sealed class IndexManager
{
#region 单例模式
static readonly IndexManager indexManager = new IndexManager();

private IndexManager()
{
}

public static IndexManager GetInstance => indexManager;
#endregion

//建立一个队列。添加和删除共用
Queue<ViewModelContent> queue = new Queue<ViewModelContent>();
//队列的添加方法
public void AddQueue(int id, string title, string content)
{
ViewModelContent vm = new ViewModelContent();
vm.Id = id;
vm.Title = title;
vm.Content = content;
vm.EmumType = LuceneTypeEnum.Add;
queue.Enqueue(vm);
}
/// <summary>
/// 要删除的数据
/// </summary>
/// <param name="id"></param>
public void DeleteQueue(int id)
{
ViewModelContent viewModel = new ViewModelContent();
viewModel.Id = id;
viewModel.EmumType = LuceneTypeEnum.Delete;
queue.Enqueue(viewModel);
}

//新开一个线程去执行,通常在appstart这个函数开启。扫描这个队列里面有没有数据,有就做添加删除的操作
public void StartThread()
{
Thread thread = new Thread(WriteIndexContent);
thread.IsBackground = true;
thread.Start();
}
//检测队列中是否有数据,有就取
private void WriteIndexContent()
{
while (true)
{
if (queue.Count > 0)
{
CreateIndexContent();
}
else
{
Thread.Sleep(3000);
}
}

}

private void CreateIndexContent()
{
string indexPath = HttpContext.Current.Request.MapPath("/lucenedir");//注意和磁盘上文件夹的大小写一致,否则会报错。将创建的分词内容放在该目录下。
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());//指定索引文件(打开索引目录) FS指的是就是FileSystem
bool isUpdate = IndexReader.IndexExists(directory);//IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。
if (isUpdate)//下面用到的队列会一个个的执行这里解锁文件夹也没事
{
//同时只能有一段代码对索引库进行写操作。当使用IndexWriter打开directory时会自动对索引库文件上锁。
//如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这个问题后面会解决)
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 viewModel = queue.Dequeue();//对象出列
writer.DeleteDocuments(new Term("Id",viewModel.Id.ToString()));//这里就算lucene里面没有这个数据也不会报错,如果我们想要修改一个数据,那么先删除后添加这个做法无疑更好
if (viewModel.EmumType==LuceneTypeEnum.Delete)
{
continue;
}
Document document = new Document();//表示一篇文档。
//Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
document.Add(new Field("Id", viewModel.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("Title", viewModel.Title, Field.Store.YES, Field.Index.NOT_ANALYZED));
//Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
document.Add(new Field("Content", viewModel.Content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document);

}
writer.Dispose();//会自动解锁。
directory.Dispose();//不要忘了Close,否则索引结果搜不到
}
}

涉及到的ViewModelContent类,这个是我们文档的添加时所用到的,里面包含我们需要写进的数据

1
2
3
4
5
6
7
public class ViewModelContent
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public LuceneTypeEnum EmumType { get; set; }//表示添加还是删除
}

我们在Application_Start函数里面开启线程去扫描LuceneNet对应的数据队列

1
2
3
4
5
6
7
8
9
public class MvcApplication : SpringMvcApplication /*System.Web.HttpApplication*/
{
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();//读取配置文件中关于log4net的配置
IndexManager.GetInstance.StartThread();//开启线程扫描LuceneNet对应的数据队列。
.........
}
}

最后让我们来测试下,创建一个Test控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class TestController : Controller
{
// GET: Test
public ActionResult Index()
{
return View();
}

public ActionResult TestSearch()
{
OAProject.Modle.Books model = new Books();
model.AurhorDescription = "jlkfdjf";
model.Author = "asfasd";
model.CategoryId = 1;
model.Clicks = 1;
model.ContentDescription = "Ajax高级编程";
model.EditorComment = "adfsadfsadf";
model.ISBN = "111111111111111111";
model.PublishDate = DateTime.Now;
model.PublisherId = 72;
model.Title = "Ajax";
model.WordsCount = 1234;
//1.将数据先存储到数据库中。获取刚插入的数据的主键ID值。
IndexManager.GetInstance.AddQueue(9999, model.Title, model.ContentDescription);//向队列中添加
return Content("ok");
}
}

最后我们发现结果是可以搜索到我们新添加的书

高级(了解)

24Kfrg.png

24KCIK.png


OA项目第七天
http://www.kju147.com/2018/04/15/OA项目第七天/
作者
个人学习
发布于
2018年4月15日
许可协议