Lucene.NET

CAST supports Lucene.NET via its com.castsoftware.nosqldotnet extension. Details about how this support is provided for .NET source code is discussed below.

Supported Client Libraries

Library Version Supported
Lucene.NET up to: 3.0.3

(tick)

Supported Operations

Operations Methods Supported
Insert

Lucene.Net.Index.IndexWriter.AddDocument

Select

Lucene.Net.Index.IndexWriter.GetMaxFieldLength

Lucene.Net.Index.IndexWriter.NumRamDocs

Lucene.Net.Index.IndexWriter.GetDocCount

Lucene.Net.Index.IndexWriter.HasDeletions

Lucene.Net.Index.IndexWriter.MaxDoc

Lucene.Net.Index.IndexWriter.NumDeletedDocs

Lucene.Net.Index.IndexWriter.NumDocs

Lucene.Net.Index.IndexReader.Document

Lucene.Net.Index.IndexReader.GetFieldNames

Lucene.Net.Index.IndexReader.IsDeleted

Lucene.Net.Index.IndexReader.HasDeletions

Lucene.Net.Index.IndexReader.ListCommits

Lucene.Net.Index.IndexReader.TermDocs

Lucene.Net.Search.Searcher.Search'

Lucene.Net.Search.Searcher.Doc

Lucene.Net.Search.Searcher.DocFreq

Lucene.Net.Search.Searcher.Explain

Delete

Lucene.Net.Index.IndexWriter.DeleteDocuments

Lucene.Net.Index.IndexReader.DeleteDocument

Lucene.Net.Index.IndexWriter.DeleteAll

Update

Lucene.Net.Search.Searcher.Rewrite

Lucene.Net.Index.IndexWriter.UpdateDocument

Objects

Icon Description

DotNet Lucene Index

DotNet Unknown Lucene Index

All links are created between the caller .NET Method objects and DotNet Lucene Index object:

Link type Methods Supported
useSelectLink

GetMaxFieldLength

NumRamDocs

GetDocCount

HasDeletions

MaxDoc

NumDeletedDocs

NumDocs

Document

GetFieldNames

IsDeleted

ListCommits

TermDocs

Search

Doc

DocFreq

Explain

useUpdateLink

Rewrite

UpdateDocument

useInsertLink

AddDocument

Commit

useDeleteLink

DeleteDocuments

DeleteDocument

DeleteAll

What results can you expect?

Some example scenarios are shown below:

Lucene Index

public class MyLuceneIndex
{
    private Directory indexDirectory;
    private IndexWriterConfig indexWriterConfig;
    private IndexWriter indexWriter;

    public MyLuceneIndex()
    {
        indexDirectory = new RAMDirectory();
        indexWriterConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, new StandardAnalyzer(LuceneVersion.LUCENE_48));
        indexWriter = new IndexWriter(indexDirectory, indexWriterConfig);
    }

public class LuceneCRUDExample
{
    private string productIndexPath = "D:\ProductIndex";
    private string userIndexPath = "D:\UserIndex";
    private Directory productIndexDirectory;
    private Directory userIndexDirectory;
    private IndexWriterConfig indexWriterConfig;
    private IndexWriter productIndexWriter;
    private IndexWriter userIndexWriter;

    public LuceneCRUDExample()
    {
        productIndexDirectory = FSDirectory.Open(new DirectoryInfo(productIndexPath));
        userIndexDirectory = FSDirectory.Open(new DirectoryInfo(userIndexPath));

        indexWriterConfig = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, new StandardAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48));
        productIndexWriter = new IndexWriter(productIndexDirectory, indexWriterConfig);
        userIndexWriter = new IndexWriter(userIndexDirectory, indexWriterConfig);
    }

Select Operation

 public void SearchProductDocuments(string searchTerm)
    {
        var searcher = new IndexSearcher(productIndexDirectory, true);
        var parser = new Lucene.Net.QueryParsers.Classic.QueryParser(Lucene.Net.Util.LuceneVersion.LUCENE_48, "Description", new StandardAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48));
        var query = parser.Parse(searchTerm);

        var topDocs = searcher.Search(query, 10);
        foreach (var scoreDoc in topDocs.ScoreDocs)
        {
            var doc = searcher.Doc(scoreDoc.Doc);
            Console.WriteLine($"Product ID: {doc.Get("Id")}, Name: {doc.Get("Name")}, Description: {doc.Get("Description")}");
        }

        searcher.Dispose();
    }

Insert Operation

public void AddProductDocument(int id, string name, string description)
    {
        var doc = new Document();
        doc.Add(new StringField("Id", id.ToString(), Field.Store.YES));
        doc.Add(new TextField("Name", name, Field.Store.YES));
        doc.Add(new TextField("Description", description, Field.Store.YES));

        productIndexWriter.AddDocument(doc);
        productIndexWriter.Commit();
    }

Update Operation

 public void UpdateProductDocument(int id, string newName, string newDescription)
    {
        var term = new Term("Id", id.ToString());
        var doc = new Document();
        doc.Add(new StringField("Id", id.ToString(), Field.Store.YES));
        doc.Add(new TextField("Name", newName, Field.Store.YES));
        doc.Add(new TextField("Description", newDescription, Field.Store.YES));

        productIndexWriter.UpdateDocument(term, doc);
        productIndexWriter.Commit();
    }

Delete Operation

public void DeleteProductDocument(int id)
{
var term = new Term("Id", id.ToString());
productIndexWriter.DeleteDocuments(term);
productIndexWriter.Commit();
}

Known Limitations

  • If the index location or path is not found in the source application, it will result in an Unknown Lucene index object