lucene.net简单tutorial

本文详细介绍了如何使用Lucene.NET在C#中创建全文搜索索引,包括从Apache Lucene.Net-2.9.2-incubating.bin.zip文件中提取库,创建文档并将其写入索引。

Step 1 - Create a new Console Application

Then extract the Lucene.Net.dll from the Apache-Lucene.Net-2.9.2-incubating.bin.zip file into yourlib folder.

You'll notice lots of other bits in  this zip file. Especially of interest to you later might be the stuff in thecontrib folder. I might get to that in a later tutorial, but for now lets keep it simple.

Step 2 - Add a Reference to the Lucene.net.dll

Your references should look like this

Step 3 - Create a Document

Ok the next step is to create a simple Document with the appropriatefields which you'll want to search on. In our case we're going to create 3 cars. a Ford Fiesta, a Ford Focus and a Vauxhall Astra.

01using System;
02using System.IO;
03using Lucene.Net.Analysis;
04using Lucene.Net.Analysis.Standard;
05using Lucene.Net.Documents;
06using Lucene.Net.Index;
07using Lucene.Net.Store;
08using Directory = Lucene.Net.Store.Directory;
09using Version = Lucene.Net.Util.Version;
10  
11namespace LuceneNet.App
12{
13    classProgram
14    {
15        staticvoid Main(string[] args)
16        {
17            var fordFiesta =new Document();
18            fordFiesta.Add(newField("Id","1", Field.Store.YES, Field.Index.NOT_ANALYZED));
19            fordFiesta.Add(newField("Make","Ford", Field.Store.YES, Field.Index.ANALYZED));
20            fordFiesta.Add(newField("Model","Fiesta", Field.Store.YES, Field.Index.ANALYZED));
21  
22            var fordFocus =new Document();
23            fordFocus.Add(newField("Id","2", Field.Store.YES, Field.Index.NOT_ANALYZED));
24            fordFocus.Add(newField("Make","Ford", Field.Store.YES, Field.Index.ANALYZED));
25            fordFocus.Add(newField("Model","Focus", Field.Store.YES, Field.Index.ANALYZED));
26  
27            var vauxhallAstra =new Document();
28            vauxhallAstra.Add(newField("Id","3", Field.Store.YES, Field.Index.NOT_ANALYZED));
29            vauxhallAstra.Add(newField("Make","Vauxhall", Field.Store.YES, Field.Index.ANALYZED));
30            vauxhallAstra.Add(newField("Model","Astra", Field.Store.YES, Field.Index.ANALYZED));
31  
32  
33  
34            Directory directory = FSDirectory.Open(newDirectoryInfo(Environment.CurrentDirectory + "\\LuceneIndex"));
35            Analyzer analyzer =new StandardAnalyzer(Version.LUCENE_29);
36  
37  
38            var writer =new IndexWriter(directory, analyzer,true, IndexWriter.MaxFieldLength.LIMITED);
39            writer.AddDocument(fordFiesta);
40            writer.AddDocument(fordFocus);
41            writer.AddDocument(vauxhallAstra);
42  
43            writer.Optimize();                        
44            writer.Close();
45              
46  
47              
48        }
49    }
50}

In the code above you can see we firstly create a series of Document types. For each document we then define a series ofFields. You might think this looks similar to SQL where you create the table schema and eachDocument is like a row in the table! There's an important difference here, documents do not have a schema. If we'd wanted to I could of given the 2 Ford documents an extra field calledSpecialFordField which wouldn't have existed at all on the Vauxhall documents.

Step 4 - Create a Directory

Now we need to sort out where we're going to store our index:

1Directory directory = FSDirectory.Open(newDirectoryInfo(Environment.CurrentDirectory + "\\LuceneIndex"));

The above code creates a Directory. this is the place in which we store orwrite our Index to. In this case we use the FSDirectory.Open() factory method to create an Lucene Index on the FileSystem in a folder/directory calledLuceneIndex. We could of equally created new RamDirectory() and just stored our index in RAM for super high performance but Lucene is so fast, that for the most part this is unecessary.

Step 5 - The Analyzer

1Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_29);

We next create a new Analyzer which essentially turns our text intoTokens which are stored in our Index.

Step 6 - Writing the Documents to the Index

Finally we actually have to write the Documents to our Index

1var writer = newIndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
2            writer.AddDocument(fordFiesta);
3            writer.AddDocument(fordFocus);
4            writer.AddDocument(vauxhallAstra);
5  
6            writer.Optimize();                        
7            writer.Close();

We define an IndexWriter telling it to use our Directory and Analyzer defined above. We then add all the documents to theIndexWriter call the writer.Optimize() which essentally does the semi-equivilent of a defrag on the index and then finallywriter.Close(). At this point we have a perfectly good index to search.

Step 7 - Opening the Index for Searching

Ok now let's actually search our newly created index.

 

1IndexReader indexReader = IndexReader.Open(directory,true);
2            Searcher indexSearch =new IndexSearcher(indexReader);

 

We firstly need to open an IndexReader here we're passing in theDirectory we created above and wrote our Documents into. AnIndexReader simply reads the index it doesn't do the magic search part. For that we need aSearcher in our case a IndexSearcher which obviously needs to read our index internally.

Step 8 - Creating our Search Query

Prepare to search with a simple search query. The reality is that it's not quite as simple as typing in a Google query, though it's not far off.

 

1var queryParser = newQueryParser(Version.LUCENE_29, "Make", analyzer);
2            var query = queryParser.Parse("Ford");

 

Here we're creating a QueryParser and specifying that we want to search the Make field of our Index. It is totally possible to search more than one field but for simplicity I'm not going to do that here. We also use the sameStandardAnalyzer we use above to apply the same tokenization to our query.

Finally we parse our raw query Ford. So hopefully we'll be able to find some Ford cars in our Index specifically in theMake field.

Step 9 - Performing the Search

 

1Console.WriteLine("Searching for: "+ query.ToString());
2            TopDocs resultDocs = indexSearch.Search(query, indexReader.MaxDoc());
3              
4            Console.WriteLine("Results Found: "+ resultDocs.totalHits);

 

Above we perform our search and return the TopDocs that match. Hopefully this should be2 results if yours is anything like mine. There are indeed 2 Fords in our index.

Step 10 - Displaying our Search Results

Last part. we just quickly loop through and print out the cars that match.

 

1var hits = resultDocs.scoreDocs;
2            foreach(var hit inhits)
3            {                
4                var documentFromSearcher = indexSearch.Doc(hit.doc);
5                Console.WriteLine(documentFromSearcher.Get("Make") +" " + documentFromSearcher.Get("Model"));
6            }
7  
8            indexSearch.Close();
9            directory.Close();

 

That's it, good luck, hope you find what you're looking for.

Full Download of the Lucene.Net tutorial

提供了一个基于51单片机的RFID门禁系统的完整资源文件,包括PCB图、原理图、论文以及源程序。该系统设计由单片机、RFID-RC522频射卡模块、LCD显示、灯控电路、蜂鸣器报警电路、存储模块和按键组成。系统支持通过密码和刷卡两种方式进行门禁控制,灯亮表示开门成功,蜂鸣器响表示开门失败。 资源内容 PCB图:包含系统的PCB设计图,方便用户进行硬件电路的制作和调试。 原理图:详细展示了系统的电路连接和模块布局,帮助用户理解系统的工作原理。 论文:提供了系统的详细设计思路、实现方法以及测试结果,适合学习和研究使用。 源程序:包含系统的全部源代码,用户可以根据需要进行修改和优化。 系统功能 刷卡开门:用户可以通过刷RFID卡进行门禁控制,系统会自动识别卡片并判断是否允许开门。 密码开门:用户可以通过输入预设密码进行门禁控制,系统会验证密码的正确性。 状态显示:系统通过LCD显示屏显示当前状态,如刷卡成功、密码错误等。 灯光提示:灯亮表示开门成功,灯灭表示开门失败或未操作。 蜂鸣器报警:当刷卡或密码输入错误时,蜂鸣器会发出报警声,提示用户操作失败。 适用人群 电子工程、自动化等相关专业的学生和研究人员。 对单片机和RFID技术感兴趣的爱好者。 需要开发类似门禁系统的工程师和开发者。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值