How to determine the lucene index version?

Viewed 2195

I am writing a shell script (csh) that has to determine the lucene index version and then based on that it has to upgrade the index to next version. So, if the lucene indices are on 2.x, I have to upgrade the indices to 3.x Finally the indices need to be upgraded to 6.x.

Since upgrading indices is a sequential process(2.x->3.x->4.x->5.x->6.x), I have to know the indices version before hand so that I can set the classpath properly and upgrade.

Please help me on this.

2 Answers

This is a piece of code I wrote to print the index version.

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexFormatTooNewException;
import org.apache.lucene.index.IndexFormatTooOldException;
import org.apache.lucene.index.StandardDirectoryReader;
import org.apache.lucene.store.SimpleFSDirectory;
import org.junit.Test;

public class TestReindex {

    public void testVersion() throws IOException{
        Path path = Paths.get("<Path_to_index_files>");

        try (DirectoryReader reader = StandardDirectoryReader.open(new SimpleFSDirectory(path))){
            Pattern pattern = Pattern.compile("lucene.version=(.*?),");

            Matcher matcher = pattern.matcher(reader.toString());
            if (matcher.find()) {
                System.out.println("Current version: " + matcher.group(1));
            }
        } catch(IndexFormatTooOldException ex) {
            System.out.println("Current version: " + ex.getVersion());
            System.out.println("Min Version: " + ex.getMinVersion());
            System.out.println("Max Version: " + ex.getMaxVersion());
        } catch (IndexFormatTooNewException ex) {
            System.out.println("Current version: " + ex.getVersion());
            System.out.println("Min Version: " + ex.getMinVersion());
            System.out.println("Max Version: " + ex.getMaxVersion());
        }
    }
}

If you are trying to read an index that is too new or too old with respect to the version of Lucene being used, an exception will be thrown. The exceptions have info about the version which could be leveraged accordingly.

Related