• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Searching in Distributed Lucene

Searches are performed after indexing the data. In order to search the data, NCacheDirectory is passed to the IndexReader. Multiple Index Readers can be opened against an Index either through an IndexWriter or an NCacheDirectory. The instance of the IndexReader is passed to the IndexSearcher. The IndexSearcher is responsible for searching the data according to the given queries. Lucene provides a wide range of queries, and the Distributed Lucene supports all the Lucene queries.

Lucene.Net.Search.TopDocs.TotalHits represents the number of top results achieved as a result of the searching performed. In the case of the Distributed Lucene, this value is converted from an integer to long. Similarly, the values of Lucene.Net.Search.ScoreDocs.Doc, Lucene.Net.Index.IndexReader.NumDoc, and Lucene.Net.Index.IndexReader.MaxDocs are also converted from an integer to long.

In the Distributed Lucene, if you delete a Document and call commit, you'll still be able to read the contents of the deleted Document through an instance of an IndexReader, provided that this IndexReader was opened before you made the commit call (after deletion). If you dispose this IndexReader after your commit call and then reopen it, you will not be able to read the contents of the deleted Document.

You can retrieve the results even when the cluster is partial. NCache Distributed Lucene provides AllowPartialResults flag which can be set to TRUE for retrieving the partial data. By default, its value is FALSE.

Prerequisites

  • .NET/.NET Core
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • Make sure that you have created and started a Lucene cache through the NCache Web Manager or PowerShell.
  • Make sure that Client Notifications are enabled.
  • Make sure that your application is not using any native Lucene DLL/Reference.
  • For API details refer to: IndexReader, IndexSearcher, IndexWriter, NCacheDirectory, GetReader, LuceneVersion, Analyzer, WhitespaceAnalyzer, QueryParser, IndexWriterConfig.

Searching Data

The code sample below demonstrates how you can search your indexed documents with the Distributed Lucene.

  • .NET/.NET Core
IndexReader reader = null;
IndexSearcher indexSearcher = null;
try
{
    // Create a directory and open it on the cache and the index path
    NCacheDirectory ncacheDirectory = NCacheDirectory.Open("demoCache", 
    "luceneIndex");

    // Specify lucene version and analyzer type 
    LuceneVersion version = LuceneVersion.LUCENE_48;
    Analyzer analyzer = new WhitespaceAnalyzer(version);

    // Create an indexWriterConfig which holds all the configurations to create an instance of the writer
    IndexWriterConfig config = new IndexWriterConfig(LuceneVersion.
    LUCENE_48, analyzer);

    // Create an instance of the writer
    IndexWriter indexWriter = new IndexWriter(ncacheDirectory, new 
    IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer));

    // Open a new reader instance
    // The 'applyAllDeletes' is set to true which means all enqueued deletes will be applied on the writer
    reader = indexWriter.GetReader(true);
    // OR
    //  reader = DirectoryReader.Open(indexWriter, true);

    // OR
    //  reader = DirectoryReader.Open(ncacheDirectory, 1);

    // Allow partial results in case of partial connectivity with cluster
    reader.AllowPartialResults = true;

    // A searcher is opened to perform searching
    indexSearcher = new IndexSearcher(reader);

    // Specify the searchTerm and the fieldName
    string searchTerm = "Beverages";
    string fieldName = "Category";

    // Specify lucene version and analyzer type 
    LuceneVersion version = LuceneVersion.LUCENE_48;
    Analyzer analyzer = new WhitespaceAnalyzer(version);

    // Create a query parser and parse the query with the parser 
    QueryParser parser = new QueryParser(version, fieldName, 
    analyzer);
    Query query = parser.Parse(searchTerm);

    // Returns the top 10000 hits from the result set
    ScoreDoc[] docsFound = indexSearcher.Search(query, 10000).
    ScoreDocs;

    // Please make sure that no operations should be performed after calling this

}
catch (Exception ex)
{
    // Handle Lucene exceptions
}
finally
{
    if (indexSearcher != null) indexSearcher.Dispose();
    if (reader != null) reader.Dispose();
}
Note

You should call Dispose at the end of every reader instance, otherwise it lives in-memory and causes the memory leakages.

Additional Resources

NCache provides a sample application for searching the documents in the Distributed Lucene on GitHub.

See Also

Distributed Lucene Overview
Distributed Lucene Geo-Spatial API
Distributed Lucene Facets
Distributed Lucene Indexing

Back to top Copyright © 2017 Alachisoft