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

In the code above you can see we firstly create a series of Document types. For each document we then define a series of Fields. You might think this looks similar to SQL where you create the table schema and each Document 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 called SpecialFordField which wouldn't have existed at all on the Vauxhall documents.
The above code creates a Directory. this is the place in which we store or write 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 called LuceneIndex. 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.
We next create a new Analyzer which essentially turns our text into Tokens which are stored in our Index.
We define an IndexWriter telling it to use our Directory and Analyzer defined above. We then add all the documents to the IndexWriter call the writer.Optimize() which essentally does the semi-equivilent of a defrag on the index and then finally writer.Close(). At this point we have a perfectly good index to search.
We firstly need to open an IndexReader here we're passing in the Directory we created above and wrote our Documents into. An IndexReader simply reads the index it doesn't do the magic search part. For that we need a Searcher in our case a IndexSearcher which obviously needs to read our index internally.
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 same StandardAnalyzer 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 the Make field.
Above we perform our search and return the TopDocs that match. Hopefully this should be 2 results if yours is anything like mine. There are indeed 2 Fords in our index.
That's it, good luck, hope you find what you're looking for.
Full Download of the Lucene.Net tutorial
SimpleLuceneNetTutorial.zip (1.07 mb)
is a really good in-depth book, that'll teach you possibly more than you ever wanted to know about Lucene.


http://www.d80.co.uk/post/2011/03/29/LuceneNet-Tutorial.aspx
- VS2010 - Express edition will do. (we're going to be writing a Console Application)
- SQL Server 2008 - Express edition should do
- Lucene.Net 2.9.2 binary download
- Luke - See the insides of your Lucene Index
- Full Tutorial Source Download
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 your lib folder.You'll notice lots of other bits in this zip file. Especially of interest to you later might be the stuff in the contrib 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 appropriate fields 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.01 | using System; |
02 | using System.IO; |
03 | using Lucene.Net.Analysis; |
04 | using Lucene.Net.Analysis.Standard; |
05 | using Lucene.Net.Documents; |
06 | using Lucene.Net.Index; |
07 | using Lucene.Net.Store; |
08 | using Directory = Lucene.Net.Store.Directory; |
09 | using Version = Lucene.Net.Util.Version; |
10 | |
11 | namespace LuceneNet.App |
12 | { |
13 | class Program |
14 | { |
15 | static void Main( string [] args) |
16 | { |
17 | var fordFiesta = new Document(); |
18 | fordFiesta.Add( new Field( "Id" , "1" , Field.Store.YES, Field.Index.NOT_ANALYZED)); |
19 | fordFiesta.Add( new Field( "Make" , "Ford" , Field.Store.YES, Field.Index.ANALYZED)); |
20 | fordFiesta.Add( new Field( "Model" , "Fiesta" , Field.Store.YES, Field.Index.ANALYZED)); |
21 | |
22 | var fordFocus = new Document(); |
23 | fordFocus.Add( new Field( "Id" , "2" , Field.Store.YES, Field.Index.NOT_ANALYZED)); |
24 | fordFocus.Add( new Field( "Make" , "Ford" , Field.Store.YES, Field.Index.ANALYZED)); |
25 | fordFocus.Add( new Field( "Model" , "Focus" , Field.Store.YES, Field.Index.ANALYZED)); |
26 | |
27 | var vauxhallAstra = new Document(); |
28 | vauxhallAstra.Add( new Field( "Id" , "3" , Field.Store.YES, Field.Index.NOT_ANALYZED)); |
29 | vauxhallAstra.Add( new Field( "Make" , "Vauxhall" , Field.Store.YES, Field.Index.ANALYZED)); |
30 | vauxhallAstra.Add( new Field( "Model" , "Astra" , Field.Store.YES, Field.Index.ANALYZED)); |
31 | |
32 | |
33 | |
34 | Directory directory = FSDirectory.Open( new DirectoryInfo(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 | } |
Step 4 - Create a Directory
Now we need to sort out where we're going to store our index:1 | Directory directory = FSDirectory.Open( new DirectoryInfo(Environment.CurrentDirectory + "\\LuceneIndex" )); |
Step 5 - The Analyzer
1 | Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29); |
Step 6 - Writing the Documents to the Index
Finally we actually have to write the Documents to our Index1 | var writer = new IndexWriter(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(); |
Step 7 - Opening the Index for Searching
Ok now let's actually search our newly created index.1 | IndexReader indexReader = IndexReader.Open(directory, true ); |
2 | Searcher indexSearch = new IndexSearcher(indexReader); |
We firstly need to open an IndexReader here we're passing in the Directory we created above and wrote our Documents into. An IndexReader simply reads the index it doesn't do the magic search part. For that we need a Searcher 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.1 | var queryParser = new QueryParser(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 same StandardAnalyzer 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 the Make field.
Step 9 - Performing the Search
1 | Console.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 be 2 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.1 | var hits = resultDocs.scoreDocs; |
2 | foreach (var hit in hits) |
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
SimpleLuceneNetTutorial.zip (1.07 mb)
Recommended Reading
Although this book is aimed at the Java verion Lucene in Actionhttp://www.d80.co.uk/post/2011/03/29/LuceneNet-Tutorial.aspx
Comments
Post a Comment