Lucene.Net 创建全文搜索最核心的内容是Create Index 和 Search ,而创建索引是后面搜索的基础,因为后面的搜索是使用索引来搜索的。对于创建索引,Lucene.Net 专门提供了个类实现,其中 Lucene.Net.Index..IndexWrite 创建索引并将索引写入文件,对应的Lucene.Net.Index..IndexReader 实现从文件夹中将索引读出来,以便对索引进行修改等操作

创建索引:

IndexWriter indexwriter = new IndexWriter("index", new StandardAnalyzer(), true);

首先是定义一个索引写入器indexwrite,其中第一个参数index表示要存储索引的文件夹,第二个参数是一个分析对象,主要用于从文本中抽取那些需要建立索引的内容,把不需要参与建索引的文本内容去掉.比如去掉一些a the之类的常用词,还有决定是否大小写敏感.不同的选项通过指定不同的分析对象控制,第三个参数用于确定是否覆盖原有索引,true表示新创建的索引将覆盖掉原来的索引,false 将重新创建并保留原有索引。

Document doc = new Document();

创建一个文档对象

doc.Add(Field.UnStored("text", context);

doc.Add(Field.Keyword("path", path));

doc.Add(Field .Text ("filename",filename));

给文档添加属性,Add方法是将一个属性添加到doc中,text是要添家的属性的名字,context是要创建索引的内容,可以是任何可以解读的数据源,这里要注意的是Field中的几个类型,总共有4个

1、Keyword 见名知意就是关键字,该字段中的内容不经过分析但会被索引并直接保存到索引中,比如:good,filename,teacher等字符串常量,也可以是一个字符串数组,如string [] contex={“doc”,”xls”,”ppt”,”pdf”,html”,txt”}

Foreach(stirng strcontex in contex)

{
doc.Add(Filed.Keyword(“text”,strcontex);
}

也可以这样将你的关键字添家到文档中。

2、 UnIndexed 不被分析,不被索引,但却保存在索引中

3、Unstrored 和UnIndexed刚好相反

4、 Text 和UnStrored类似.如果值的类型为string还会被保存.如果值的类型为Reader就不会被保存和UnStored一样.

indexriter.AddDocument(doc);

将doc添加到索引中
writer.Optimize();

对创建的索引进行优化
writer.Close();

关闭写入器

到此一个简单的索引创建完毕。

下面再提供一个创建索引的例子:

private String[] keywords = {"20001895", "20001896"};

private String[] unindexed = {"Red star", "good morning"};

private String[] unstored = { "I am a programer", "you are programmer ,too",};

private String[] text1 = { " programer ", "morning" };

private String[] text2 = { "200606", "200609" };

private String[] text3 = { "/Computers/red", "/Computers/star" };

private Directory dir;

protected void AddDocuments()

{
string indexDir = "index";
dir = FSDirectory.GetDirectory(indexDir, true);
IndexWriter writer=new IndexWriter(dir, GetAnalyzer(), true);
for (int i = 0; i < keywords.Length; i++)
{
Document doc = new Document();
doc.Add(Field.Keyword("isbn", keywords[i]));
doc.Add(Field.UnIndexed("title", unindexed[i]));
doc.Add(Field.UnStored("contents", unstored[i]));
doc.Add(Field.Text("subject", text1[i]));
doc.Add(Field.Text("pubmonth", text2[i]));
doc.Add(Field.Text("category", text3[i]));
writer.AddDocument(doc);
}
writer.Optimize();
writer.Close();
}


数据检索:

创建完了索引后怎么来利用索引检索数据,这里就要用到Lucene.Net.Searcher.IndexSercher个类来读取索引文件,并将读取的结果放在 Hits中,这里的Hits是一个集合,和DataSet有相似之处,DataSet中放的是Tables,Hits中放的是Documents,然后就是将Hits中的数据怎么处理的问题,这不是论述的重点,以后有时间再写这部分。

IndexSearcher searcher = new IndexSearcher(indexDirectory);

创建一个搜索器,参数是创建索引的路径

Query query = QueryParser.Parse(condition, "text", new StandardAnalyzer());

定义一个查询对象,参数condition表示查询的条件,text 我们创建索引时的要被分析的内容,第三个是个分析对象

Hits hits = searcher.Search(query);

通过搜索器将搜索的Document放到Hits中

Int total = hits.Length();

计算hits中有多少个Document

for (int i = 0; i < total; i++)

循环遍历

{
Document doc = hits.Doc(i);
string path = doc.Get("path");
string plainText =doc.Get(“text”);;
string str=doc.Get ("filename");
通过Get方法将搜索的内容提取出来
}

searcher.Close();//关闭搜索器



















1、Keyword 见名知意就是关键字,该字段中的内容不经过分析但会被索引并直接保存到索引中,比如:good,filename,teacher等字符串常量,也可以是一个字符串数组,如string [] contex={“doc”,”xls”,”ppt”,”pdf”,html”,txt”}



{
doc.Add(Filed.Keyword(“text”,strcontex);
}























private String[] keywords = {"20001895", "20001896"};

private String[] unindexed = {"Red star", "good morning"};

private String[] unstored = { "I am a programer", "you are programmer ,too",};

private String[] text1 = { " programer ", "morning" };

private String[] text2 = { "200606", "200609" };

private String[] text3 = { "/Computers/red", "/Computers/star" };





{
string indexDir = "index";
dir = FSDirectory.GetDirectory(indexDir, true);
IndexWriter writer=new IndexWriter(dir, GetAnalyzer(), true);
for (int i = 0; i < keywords.Length; i++)
{
Document doc = new Document();
doc.Add(Field.Keyword("isbn", keywords[i]));
doc.Add(Field.UnIndexed("title", unindexed[i]));
doc.Add(Field.UnStored("contents", unstored[i]));
doc.Add(Field.Text("subject", text1[i]));
doc.Add(Field.Text("pubmonth", text2[i]));
doc.Add(Field.Text("category", text3[i]));
writer.AddDocument(doc);
}
writer.Optimize();
writer.Close();
}



























{
Document doc = hits.Doc(i);
string path = doc.Get("path");
string plainText =doc.Get(“text”);;
string str=doc.Get ("filename");
通过Get方法将搜索的内容提取出来
}

