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

lucene.net tutorial with lucene 2.9.2

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 }

相关文章:

  • 2022-12-23
  • 2022-02-24
  • 2021-09-21
  • 2021-12-29
  • 2021-08-01
  • 2022-01-27
  • 2022-02-01
  • 2022-12-23
猜你喜欢
  • 2021-09-26
  • 2021-09-22
  • 2022-12-23
  • 2021-06-08
  • 2021-11-09
  • 2021-07-22
相关资源
相似解决方案