Lucene.Net 开发介绍 —— (一)、接触Lucene.Net

  1、引用Lucene.Net类库


找到Lucene.Net的源代码,在“C#/src/Lucene.Net”目录。打开Visual Studio,我的版本是2008,而Lucene.Net默认的是2005。先创建一个项目,简单起见,创建一个C#控制台程序。

1_vs_create.jpg
图 1.1

然后添加Lucene.Net进项目,如图 1.2 - 1.3。

1_vs_2.jpg
图 1.2

1_vs_3.jpg
图 1.3

这个过程要进行一个VS2005到2008的转换。添加后,解决方案就有Lucene.Net项目了,如图1.4。

1_vs_4.jpg
图 1.4

然后把Lucene.Net引入TestLucene项目。如图1.5 -1.6:

1_vs_5.jpg
图1.5

1_vs_6.jpg
图1.6

点确定后就可以了。这时候,就可以在TestLucene项目中使用Lucene.Net的API了。

2、简单示例
对Lucene.Net的操作分为建立索引,和搜索两部分。

2.1 建立索引

通过代码 2.1.1,就可以简单地建立一个索引了。代码 2.1.1将在应用程序目录下建立一个IndexDirectory目录,并在目录下创建索引文件。

1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5
 6namespace TestLucene
 7{
 8    using Lucene.Net.Index;
 9    using Lucene.Net.Store;
10    using Lucene.Net.Analysis;
11    using Lucene.Net.Analysis.Standard;
12    using Lucene.Net.Documents;
13
14    class Program
15    {
16        static void Main(string[] args)
17        {
18            Analyzer analyzer = new StandardAnalyzer();
19            IndexWriter writer = new IndexWriter("IndexDirectory", analyzer, true);
20            AddDocument(writer, "SQL Server 2008 的发布""SQL Server 2008 的新特性");
21            AddDocument(writer, "ASP.Net MVC框架配置与分析""而今,微软推出了新的MVC开发框架,也就是Microsoft ASP.NET 3.5 Extensions");
22            writer.Optimize();
23            writer.Close();
24        }

25
26        static void AddDocument(IndexWriter writer, string title, string content)
27        {
28            Document document = new Document();
29            document.Add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
30            document.Add(new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));
31            writer.AddDocument(document);
32        }

33    }

34}

35

 

 

2.2 搜索索引

代码2.2.1就可以搜索刚才建立的索引。

 


 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5
 6namespace TestLucene
 7{
 8    using Lucene.Net.Index;
 9    using Lucene.Net.Store;
10    using Lucene.Net.Analysis;
11    using Lucene.Net.Analysis.Standard;
12    using Lucene.Net.Documents;
13    using Lucene.Net.Search;
14    using Lucene.Net.QueryParsers;
15
16    class Program
17    {
18        static void Main(string[] args)
19        {
20            Analyzer analyzer = new StandardAnalyzer();
21            //IndexWriter writer = new IndexWriter("IndexDirectory", analyzer, true);
22            //AddDocument(writer, "SQL Server 2008 的发布", "SQL Server 2008 的新特性");
23            //AddDocument(writer, "ASP.Net MVC框架配置与分析", "而今,微软推出了新的MVC开发框架,也就是Microsoft ASP.NET 3.5 Extensions");
24            //writer.Optimize();
25            //writer.Close();
26
27            IndexSearcher searcher = new IndexSearcher("IndexDirectory");
28            MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[] "title""content" }, analyzer);
29            Query query = parser.Parse("sql");
30            Hits hits = searcher.Search(query);
31
32            for (int i = 0; i < hits.Length(); i++)
33            {
34                Document doc = hits.Doc(i);
35                Console.WriteLine(string.Format("title:{0} content:{1}", doc.Get("title"), doc.Get("content")));
36            }

37            searcher.Close();
38
39            Console.ReadKey();
40        }

41
42        //static void AddDocument(IndexWriter writer, string title, string content)
43        //{
44        //    Document document = new Document();
45        //    document.Add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
46        //    document.Add(new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));
47        //    writer.AddDocument(document);
48        //}
49    }

50}

51

 

 

运行后输出:

 

title:SQL Server 2008 的发布 content:SQL Server 2008 的新特性

 

2.3 疑问

2.1,2.2小节介绍了最简单的建立和搜索索引的方式。虽然代码很短,使用也很简单,但是理解起来却不是太容易。

代码 2.1.1中,先是建立了一个分词器。什么是分词器?为什么要有分词器?分词器是怎么工作的?这些问题真让人头疼。接着建立一个IndexWriter的实例,这个类是负责创建索引的,有很多构造函数,这里使用的是其中的一个。三个参数分别是:索引建立到哪个目录,用什么分词器,还有就是是否创建。如果是否创建为false,那么就是以增量的方式来创建。再下来调用了AddDocument方法,在AddDocument方法中,先组织一个Docuement对象,然后把这个对象交给IndexWriter。然后再调用Optimize优化索引,最后关闭创建过程。这里面又有什么是Document,Document是怎么往存储器里写入的?Optimize方法能干什么?问题真多。

 

代码2.2.1则相对简单,先是创建IndexSearcher对象实例,并指定其搜索的目录,然后构造了一个查询Query,然后查出Hits,这样就得到想要的结果了。但是这个查询的过程是什么样的呢?这个Query代表什么?Hits是怎么得出来的?结果的顺序是怎么决定的?这些又是留下来的问题。

 

这么多问题,不能一次说完,欲知后事如何,下面一一道来。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Lucene.net 是一个开源的全文检索引擎库,它提供了一些基本的 API 来创建和维护索引,并且可以通过这些 API 来搜索索引中的文档。下面是一些使用 Lucene.net 的基本步骤: 1. 创建索引:使用 Lucene.net 的 API,可以创建一个空的索引。可以将文档添加到索引中,以便后续搜索。 2. 添加文档:使用 Lucene.net 的 API,可以将文档添加到索引中。可以为每个文档定义一个或多个字段。 3. 搜索索引:使用 Lucene.net 的 API,可以搜索索引中的文档。可以使用查询对象来指定搜索条件,例如搜索某个字段中包含特定关键字的文档。 4. 处理搜索结果:搜索结果是一组匹配查询条件的文档。可以使用 Lucene.net 的 API 来访问每个文档的字段,以便将搜索结果呈现给用户。 以下是一个简单的示例代码,可用于创建索引、添加文档和搜索索引: ``` // 创建索引 var indexDirectory = FSDirectory.Open(@"C:\myindex"); var analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48); var indexConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer); var writer = new IndexWriter(indexDirectory, indexConfig); // 添加文档 var doc = new Document(); doc.Add(new TextField("title", "Lucene.net tutorial", Field.Store.YES)); doc.Add(new TextField("content", "This is a tutorial on how to use Lucene.net for full text search.", Field.Store.YES)); writer.AddDocument(doc); // 搜索索引 var searcher = new IndexSearcher(writer.GetReader(true)); var queryParser = new QueryParser(LuceneVersion.LUCENE_48, "content", analyzer); var query = queryParser.Parse("full text search"); var topDocs = searcher.Search(query, 10); foreach (var scoreDoc in topDocs.ScoreDocs) { var doc = searcher.Doc(scoreDoc.Doc); Console.WriteLine(doc.Get("title")); } ``` 此示例创建一个名为“myindex”的索引目录,添加一个文档,然后搜索包含“full text search”关键字的文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值