Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:48:53+00:00 2026-06-17T19:48:53+00:00

I’m using Lucene.net 3.0.3.0 (the latest version right now). I would like to know

  • 0

I’m using Lucene.net 3.0.3.0(the latest version right now). I would like to know if using a single instance of IndexWriter and IndexSearcher is a good choice in production environment(considering thread safety). I read in the documentation that creating new objects of these instances is an expensive operation and internally lucene handles concurrent requests pretty well.

This is a question to the people who have used lucene.net in production environment; please let me know if this will work out!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Lucene.Net.Search;
    using Lucene.Net.Store;
    using System.IO;

    using Directory = Lucene.Net.Store.Directory;
    using Version = Lucene.Net.Util.Version;
    using Lucene.Net.Index;
    using Lucene.Net.Analysis.Standard;
    using Lucene.Net.Analysis;

    namespace MXLuceneConsoleApp
    {
        /// <summary>
        /// This helper class applies a singleton pattern to create Searcher and Writer objects as it's recommended to create them only once.
        /// Also the searcher gets reinitialized, if any write happens.    
        /// </summary>
        public class MXLuceneIndexHelper
        {
            private static IndexSearcher _searcher;        
            private static Directory _directory;
            private static Lazy<IndexWriter> _writer = new Lazy<IndexWriter>(() => new IndexWriter(_directory, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED));

            //this private constructor makes it a singleton now.
            private MXLuceneIndexHelper() { }

            //Static constructor, opening the directory once for all.
            static MXLuceneIndexHelper()
            {
                _directory = FSDirectory.Open(new DirectoryInfo(Environment.CurrentDirectory + "\\LuceneIndexDir"));
            }

            public static IndexSearcher IndexSearcher
            {
                get
                {
                    if (_searcher == null)
                    {
                        InitializeSearcher();
                    }
                    else if (!_searcher.IndexReader.IsCurrent())
                    {                    
                        //_searcher.IndexReader.Reopen(); 
                        //refreshing the underlying Reader above doesn't do the trick, so I'm reinitializing the Searcher.
                        _searcher.Dispose();
                        InitializeSearcher();
                    }

                    return _searcher;
                }
            }

            public static IndexWriter IndexWriter
            {
                get 
                {                
                    //_writer.SetRAMBufferSizeMB(30.0);
                    return _writer.Value; 
                }
            }

            private static void InitializeSearcher()
            {
                _searcher = new IndexSearcher(_directory, false);

            }
        }//End of class
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:48:54+00:00Added an answer on June 17, 2026 at 7:48 pm

    My understanding is that there should only ever be 1 instance of IndexWriter per index. There is built in locking that is enabled in Lucene by default to ensure this condition. As far as a singleton pattern, I think you should look at the new Lazy class in .NET 4. It handles all the locking and null checking for free.

    As for IndexSearcher, you are free to have as many instances as you want. However, I think there are performance gains if you reuse the existing searcher. I can tell from your code that you know that reopening an existing searcher is much cheaper than creating a new one. Also there is built in caching in the searcher that is initilaized when you do the first search. You can also implement an IndexReaderWarmer to control when the searcher is warmed (like before the first search).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.