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

  • SEARCH
  • Home
  • 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 338505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:26:08+00:00 2026-05-12T10:26:08+00:00

I have a problem with Lucene 2.4, the situation being as follows: I have

  • 0

I have a problem with Lucene 2.4, the situation being as follows:

I have to deal with the possibility that there are 2 seperate processes operating on the same Index directory and they need to have the same data. This means that when one Instance adds a Document to the Index, the other application instances shall find the added Documents on their next search. According to the Lucene Documentation, IndexReader.reopen is what I need.

So I invented the following testcase:

package de.samedi.searcher;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

public class LuceneReload {

    private IndexSearcher searcher1;
    private IndexSearcher searcher2;
    private FSDirectory directory1, directory2;
    private IndexWriter writer1, writer2;


    @Test
    public void testReload() throws Exception {
        String home = System.getProperty("user.home");
        this.directory1 = FSDirectory.getDirectory(home + "/testIndex");
        this.directory2 = FSDirectory.getDirectory(home + "/testIndex");
        this.writer1 = new IndexWriter(this.directory1, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
        this.writer2 = new IndexWriter(this.directory2, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);

        // assert that we're empty
        assertFound(getSearcher1(), "test", 0);
        assertFound(getSearcher2(), "test", 0);

        add(this.writer1, "test");
        assertFound(getSearcher1(), "test", 1);
        assertFound(getSearcher2(), "test", 1);

        add(this.writer2, "foobar");
        assertFound(getSearcher1(), "foobar", 1);
        assertFound(getSearcher2(), "foobar", 1);
    }

    public void assertFound(IndexSearcher searcher, String q, int expected_number) {
        try {
            QueryParser parser = new QueryParser("name", new StandardAnalyzer());
            Query query = parser.parse(q);
            TopDocs t = searcher.search(query, null, 50);
            assertEquals(expected_number, t.totalHits);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }

    }

    public IndexSearcher getSearcher1() throws CorruptIndexException, IOException {
        if (this.searcher1 == null) {
            this.searcher1 = new IndexSearcher(IndexReader.open(this.directory1));
        } else {
            IndexReader new_reader, old_reader;

            old_reader = this.searcher1.getIndexReader();
            new_reader = old_reader.reopen();

            if (new_reader != old_reader) {
                System.err.println("index1 changed");
                this.searcher1.close();
                old_reader.close();
                this.searcher1 = new IndexSearcher(new_reader);
            }
        }

        return this.searcher1;
    }

    public IndexSearcher getSearcher2() throws CorruptIndexException, IOException {
        if (this.searcher2 == null) {
            this.searcher2 = new IndexSearcher(this.directory2);
        } else {
            IndexReader new_reader, old_reader;

            old_reader = this.searcher2.getIndexReader();
            new_reader = old_reader.reopen();

            if (new_reader != old_reader) {
                System.err.println("index2 changed");
                this.searcher2.close();
                old_reader.close();
                this.searcher2 = new IndexSearcher(new_reader);
            }
        }

        return this.searcher2;
    }

    public void add(IndexWriter writer, String name) throws CorruptIndexException, IOException {
        Document d = new Document();
        d.add(new Field("name", name, Field.Store.YES, Field.Index.ANALYZED));
        writer.addDocument(d);
        writer.commit();
        IndexWriter.unlock(writer.getDirectory());
    }
}

When I instead of the reopen() calls use

new_reader = IndexReader.open(this.directory1);

the tests go green.

Did I miss any important points

  • 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-05-12T10:26:09+00:00Added an answer on May 12, 2026 at 10:26 am

    see the IndexWriter#unlock javadoc:

      /**
       * Forcibly unlocks the index in the named directory.
       * <P>
       * Caution: this should only be used by failure recovery code,
       * when it is known that no other process nor thread is in fact
       * currently accessing this index.
       */
    

    I wouldn’t use that for normal operations.

    Instead, open a new writer and close it. That will work correctly – though its best to use only a single IndexWriter.

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

Sidebar

Related Questions

I have a problem with Lucene's scoring function that I can't figure out. So
Situation:I have an ASP .NET application that will search through docs using Lucene. I
I have a system that analyzes logs. It already uses lucene to index data.
I have a live Lucene index that is updated throughout the day. When several
Situation I have the following Sitecore Lucene config: New index, type=Sitecore.Search.Index, Sitecore.Kernel Contains two
I have a field that I am indexing with Lucene like so: @Field(name=hungerState, index=Index.TOKENIZED,
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a python script to simply index unicode sentences into a lucene index.
I have a problem creating an index with Zend_Search_Lucene. Now, everything works fine on
I have an index that contains the values abc and def in the analyzers

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.