1
Lucene.Net 创建全文搜索最核心的内容是Create Index 和 Search ,而创建索引是后面搜索的基础,因为后面的搜索是使用索引来搜索的。对于创建索引,Lucene.Net 专门提供了个类实现,其中 Lucene.Net.Index..IndexWrite 创建索引并将索引写入文件,对应的Lucene.Net.Index..IndexReader 实现从文件夹中将索引读出来,以便对索引进行修改等操作
2![](/Images/OutliningIndicators/None.gif)
3
创建索引:
4![](/Images/OutliningIndicators/None.gif)
5
IndexWriter indexwriter = new IndexWriter("index", new StandardAnalyzer(), true);
6![](/Images/OutliningIndicators/None.gif)
7
首先是定义一个索引写入器indexwrite,其中第一个参数index表示要存储索引的文件夹,第二个参数是一个分析对象,主要用于从文本中抽取那些需要建立索引的内容,把不需要参与建索引的文本内容去掉.比如去掉一些a the之类的常用词,还有决定是否大小写敏感.不同的选项通过指定不同的分析对象控制,第三个参数用于确定是否覆盖原有索引,true表示新创建的索引将覆盖掉原来的索引,false 将重新创建并保留原有索引。
8![](/Images/OutliningIndicators/None.gif)
9
Document doc = new Document();
10![](/Images/OutliningIndicators/None.gif)
11
创建一个文档对象
12![](/Images/OutliningIndicators/None.gif)
13
doc.Add(Field.UnStored("text", context);
14![](/Images/OutliningIndicators/None.gif)
15
doc.Add(Field.Keyword("path", path));
16![](/Images/OutliningIndicators/None.gif)
17
doc.Add(Field .Text ("filename",filename));
18![](/Images/OutliningIndicators/None.gif)
19
给文档添加属性,Add方法是将一个属性添加到doc中,text是要添家的属性的名字,context是要创建索引的内容,可以是任何可以解读的数据源,这里要注意的是Field中的几个类型,总共有4个
20![](/Images/OutliningIndicators/None.gif)
21
1、Keyword 见名知意就是关键字,该字段中的内容不经过分析但会被索引并直接保存到索引中,比如:good,filename,teacher等字符串常量,也可以是一个字符串数组,如string [] contex={“doc”,”xls”,”ppt”,”pdf”,html”,txt”}
22![](/Images/OutliningIndicators/None.gif)
23
Foreach(stirng strcontex in contex)
24![](/Images/OutliningIndicators/None.gif)
25
{
26![](/Images/OutliningIndicators/InBlock.gif)
27
doc.Add(Filed.Keyword(“text”,strcontex);
28![](/Images/OutliningIndicators/InBlock.gif)
29
}
30![](/Images/OutliningIndicators/None.gif)
31
也可以这样将你的关键字添家到文档中。
32![](/Images/OutliningIndicators/None.gif)
33
2、 UnIndexed 不被分析,不被索引,但却保存在索引中
34![](/Images/OutliningIndicators/None.gif)
35
3、Unstrored 和UnIndexed刚好相反
36![](/Images/OutliningIndicators/None.gif)
37
4、 Text 和UnStrored类似.如果值的类型为string还会被保存.如果值的类型为Reader就不会被保存和UnStored一样.
38![](/Images/OutliningIndicators/None.gif)
39
indexriter.AddDocument(doc);
40![](/Images/OutliningIndicators/None.gif)
41
将doc添加到索引中 writer.Optimize();
42![](/Images/OutliningIndicators/None.gif)
43
对创建的索引进行优化
44
writer.Close();
45![](/Images/OutliningIndicators/None.gif)
46
关闭写入器
47![](/Images/OutliningIndicators/None.gif)
48
到此一个简单的索引创建完毕。
49![](/Images/OutliningIndicators/None.gif)
50
下面再提供一个创建索引的例子:
51![](/Images/OutliningIndicators/None.gif)
52
private String[] keywords = {"20001895", "20001896"};
53![](/Images/OutliningIndicators/None.gif)
54
private String[] unindexed = {"Red star", "good morning"};
55![](/Images/OutliningIndicators/None.gif)
56
private String[] unstored = { "I am a programer", "you are programmer ,too",};
57![](/Images/OutliningIndicators/None.gif)
58
private String[] text1 = { " programer ", "morning" };
59![](/Images/OutliningIndicators/None.gif)
60
private String[] text2 = { "200606", "200609" };
61![](/Images/OutliningIndicators/None.gif)
62
private String[] text3 = { "/Computers/red", "/Computers/star" };
63![](/Images/OutliningIndicators/None.gif)
64
private Directory dir;
65![](/Images/OutliningIndicators/None.gif)
66
protected void AddDocuments()
67![](/Images/OutliningIndicators/None.gif)
68
{
69![](/Images/OutliningIndicators/InBlock.gif)
70
string indexDir = "index";
71![](/Images/OutliningIndicators/InBlock.gif)
72
dir = FSDirectory.GetDirectory(indexDir, true);
73![](/Images/OutliningIndicators/InBlock.gif)
74
IndexWriter writer=new IndexWriter(dir, GetAnalyzer(), true);
75![](/Images/OutliningIndicators/InBlock.gif)
76
for (int i = 0; i < keywords.Length; i++)
77![](/Images/OutliningIndicators/InBlock.gif)
78
{
79![](/Images/OutliningIndicators/InBlock.gif)
80
Document doc = new Document();
81![](/Images/OutliningIndicators/InBlock.gif)
82
doc.Add(Field.Keyword("isbn", keywords[i]));
83![](/Images/OutliningIndicators/InBlock.gif)
84
doc.Add(Field.UnIndexed("title", unindexed[i]));
85![](/Images/OutliningIndicators/InBlock.gif)
86
doc.Add(Field.UnStored("contents", unstored[i]));
87![](/Images/OutliningIndicators/InBlock.gif)
88
doc.Add(Field.Text("subject", text1[i]));
89![](/Images/OutliningIndicators/InBlock.gif)
90
doc.Add(Field.Text("pubmonth", text2[i]));
91![](/Images/OutliningIndicators/InBlock.gif)
92
doc.Add(Field.Text("category", text3[i]));
93![](/Images/OutliningIndicators/InBlock.gif)
94
writer.AddDocument(doc);
95![](/Images/OutliningIndicators/InBlock.gif)
96
}
97![](/Images/OutliningIndicators/InBlock.gif)
98
writer.Optimize();
99![](/Images/OutliningIndicators/InBlock.gif)
100
writer.Close();
101![](/Images/OutliningIndicators/InBlock.gif)
102
}
103![](/Images/OutliningIndicators/None.gif)
104
105![](/Images/OutliningIndicators/None.gif)
106
数据检索:
107![](/Images/OutliningIndicators/None.gif)
108
创建完了索引后怎么来利用索引检索数据,这里就要用到Lucene.Net.Searcher.IndexSercher个类来读取索引文件,并将读取的结果放在 Hits中,这里的Hits是一个集合,和DataSet有相似之处,DataSet中放的是Tables,Hits中放的是Documents,然后就是将Hits中的数据怎么处理的问题,这不是论述的重点,以后有时间再写这部分。
109![](/Images/OutliningIndicators/None.gif)
110
IndexSearcher searcher = new IndexSearcher(indexDirectory);
111![](/Images/OutliningIndicators/None.gif)
112
创建一个搜索器,参数是创建索引的路径
113![](/Images/OutliningIndicators/None.gif)
114
Query query = QueryParser.Parse(condition, "text", new StandardAnalyzer());
115![](/Images/OutliningIndicators/None.gif)
116
定义一个查询对象,参数condition表示查询的条件,text 我们创建索引时的要被分析的内容,第三个是个分析对象
117![](/Images/OutliningIndicators/None.gif)
118
Hits hits = searcher.Search(query);
119![](/Images/OutliningIndicators/None.gif)
120
通过搜索器将搜索的Document放到Hits中
121![](/Images/OutliningIndicators/None.gif)
122
Int total = hits.Length();
123![](/Images/OutliningIndicators/None.gif)
124
计算hits中有多少个Document
125![](/Images/OutliningIndicators/None.gif)
126
for (int i = 0; i < total; i++)
127![](/Images/OutliningIndicators/None.gif)
128
循环遍历
129![](/Images/OutliningIndicators/None.gif)
130
{
131![](/Images/OutliningIndicators/InBlock.gif)
132
Document doc = hits.Doc(i);
133![](/Images/OutliningIndicators/InBlock.gif)
134
string path = doc.Get("path");
135![](/Images/OutliningIndicators/InBlock.gif)
136
string plainText =doc.Get(“text”);;
137![](/Images/OutliningIndicators/InBlock.gif)
138
string str=doc.Get ("filename");
139![](/Images/OutliningIndicators/InBlock.gif)
140
通过Get方法将搜索的内容提取出来
141![](/Images/OutliningIndicators/InBlock.gif)
142
}
143![](/Images/OutliningIndicators/None.gif)
144
searcher.Close();//关闭搜索器
145![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
22
![](/Images/OutliningIndicators/None.gif)
23
![](/Images/OutliningIndicators/None.gif)
24
![](/Images/OutliningIndicators/None.gif)
25
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
30
![](/Images/OutliningIndicators/None.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/None.gif)
34
![](/Images/OutliningIndicators/None.gif)
35
![](/Images/OutliningIndicators/None.gif)
36
![](/Images/OutliningIndicators/None.gif)
37
![](/Images/OutliningIndicators/None.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
40
![](/Images/OutliningIndicators/None.gif)
41
![](/Images/OutliningIndicators/None.gif)
42
![](/Images/OutliningIndicators/None.gif)
43
![](/Images/OutliningIndicators/None.gif)
44
![](/Images/OutliningIndicators/None.gif)
45
![](/Images/OutliningIndicators/None.gif)
46
![](/Images/OutliningIndicators/None.gif)
47
![](/Images/OutliningIndicators/None.gif)
48
![](/Images/OutliningIndicators/None.gif)
49
![](/Images/OutliningIndicators/None.gif)
50
![](/Images/OutliningIndicators/None.gif)
51
![](/Images/OutliningIndicators/None.gif)
52
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
53
![](/Images/OutliningIndicators/None.gif)
54
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
55
![](/Images/OutliningIndicators/None.gif)
56
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
57
![](/Images/OutliningIndicators/None.gif)
58
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
59
![](/Images/OutliningIndicators/None.gif)
60
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
61
![](/Images/OutliningIndicators/None.gif)
62
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
63
![](/Images/OutliningIndicators/None.gif)
64
![](/Images/OutliningIndicators/None.gif)
65
![](/Images/OutliningIndicators/None.gif)
66
![](/Images/OutliningIndicators/None.gif)
67
![](/Images/OutliningIndicators/None.gif)
68
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
103
![](/Images/OutliningIndicators/None.gif)
104
![](/Images/OutliningIndicators/None.gif)
105
![](/Images/OutliningIndicators/None.gif)
106
![](/Images/OutliningIndicators/None.gif)
107
![](/Images/OutliningIndicators/None.gif)
108
![](/Images/OutliningIndicators/None.gif)
109
![](/Images/OutliningIndicators/None.gif)
110
![](/Images/OutliningIndicators/None.gif)
111
![](/Images/OutliningIndicators/None.gif)
112
![](/Images/OutliningIndicators/None.gif)
113
![](/Images/OutliningIndicators/None.gif)
114
![](/Images/OutliningIndicators/None.gif)
115
![](/Images/OutliningIndicators/None.gif)
116
![](/Images/OutliningIndicators/None.gif)
117
![](/Images/OutliningIndicators/None.gif)
118
![](/Images/OutliningIndicators/None.gif)
119
![](/Images/OutliningIndicators/None.gif)
120
![](/Images/OutliningIndicators/None.gif)
121
![](/Images/OutliningIndicators/None.gif)
122
![](/Images/OutliningIndicators/None.gif)
123
![](/Images/OutliningIndicators/None.gif)
124
![](/Images/OutliningIndicators/None.gif)
125
![](/Images/OutliningIndicators/None.gif)
126
![](/Images/OutliningIndicators/None.gif)
127
![](/Images/OutliningIndicators/None.gif)
128
![](/Images/OutliningIndicators/None.gif)
129
![](/Images/OutliningIndicators/None.gif)
130
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
131
![](/Images/OutliningIndicators/InBlock.gif)
132
![](/Images/OutliningIndicators/InBlock.gif)
133
![](/Images/OutliningIndicators/InBlock.gif)
134
![](/Images/OutliningIndicators/InBlock.gif)
135
![](/Images/OutliningIndicators/InBlock.gif)
136
![](/Images/OutliningIndicators/InBlock.gif)
137
![](/Images/OutliningIndicators/InBlock.gif)
138
![](/Images/OutliningIndicators/InBlock.gif)
139
![](/Images/OutliningIndicators/InBlock.gif)
140
![](/Images/OutliningIndicators/InBlock.gif)
141
![](/Images/OutliningIndicators/InBlock.gif)
142
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
143
![](/Images/OutliningIndicators/None.gif)
144
![](/Images/OutliningIndicators/None.gif)
145
![](/Images/OutliningIndicators/None.gif)