Lucene

Brief:

We have seen integration of lucene in the last article. This article we will look into implementation of lucene indexes using lucene.net. Lucene.net is a open source project created in java that helps integration of lucene in .net.

Need : 

If you application requires a full text search implemented. Lucene is an exclusive library that can help in this. It is pretty simple to implement and is also open source. It performs  efficient indexing and searching operation on large number of documents.

Implementation :


Lucene can also be used to search through entity framework. Lucene.net code is available on github at https://github.com/apache/lucene.net. This code can be complied and used. You can also add lucene nugget packages to the code using Install-Package Lucene.Net.

Before performing indexing let us look into the different concepts and terms used in lucene:

Field: This a dictionary or hashtable consisting of name and value pair. The values of a field can be stored, indexed and analyzed.

Document: Lucene consist of documents. This document consists sequence of fields.

Directory: Lucene store the indexes here.

IndexWriter: This helps to create lucene indexes and analyse them and store the documents to the indexes.

Analyzer: This helps in retrieving the text or keys from the dictionary.

IndexReader: This help in easy search of index documents.

Now let us look into the indexing part of lucene:

The components that are required for lucene indexing are directory, analyser, Indexwiter, document and field.

public class Indexer 
{

   Directory directory = FSDirectory.Open(“path of directory to be analyzed”);
   Analyzer analyzer = new   StandardAnalyzer(LuceneUtil.Version.LUCENE_29);
   IndexWriter writer = new IndexWriter(directory, analyzer ,true, IndexWriter.MaxFieldLength.UNLIMITED);
   Document doc = new Document();
   doc.Add(new Field(“fieldName”,
                  “fieldValue”,
                  Field.Store.NO,
                  Field.Index.ANALYZED));

   writer.AddDocument(doc);
   writer.Optimize();
   writer.Close();
}


The above code will help you index the application for full text search. If you want analyze all the documents within a directory you will need to loop through all the documents within the directory to add it to the index.

Boston Byte Grabs a Spot in Clutch’s List of Top Software Developers in Massachusetts

Boston Byte is a collective of highly-skilled and highly-professional developers dedicated to solving your technological ...