io.yucca.lucene.IndexUtility.java Source code

Java tutorial

Introduction

Here is the source code for io.yucca.lucene.IndexUtility.java

Source

package io.yucca.lucene;

/*
 * Copyright 2014 Rob Sessink
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility to manipulate a Lucene index
 * 
 * @author <a href="mailto:rob@yucca.io">Rob Sessink</a>
 * @version $Id$
 */
public class IndexUtility {

    static final Logger log = LoggerFactory.getLogger(IndexUtility.class);

    private static final String USAGE = "[-h] -s <directory> -d <directory>"
            + "\n\t-s <directory>\tdirectory from which to read the index "
            + "\n\t-d <directory>\tdirectory to which to write the new index"
            + "\n\t[-r <fields>]\tremoves these fields from the index, comma seperated"
            + "\n\t[-v <version>]\tlucene index version, in format: MAJOR.MINOR like '4.2'";

    private static final String HEADER = "Tool to manipulate a Lucene index";

    private static final String FOOTER = "";

    private static File sourceIndexDirectory;

    private static File destIndexDirectory;

    private static Version version;

    public static void main(String[] args) {
        CommandLineParser parser = new PosixParser();
        Options options = new Options();
        initOptions(options);
        try {
            final CommandLine line = parser.parse(options, args);
            if (line.hasOption('h')) {
                usage(options);
            }
            if (line.hasOption('s')) {
                String value = line.getOptionValue('s');
                sourceIndexDirectory = new File(value);
                if (sourceIndexDirectory.exists() == false) {
                    log.error("Index source directory: {} does not exist!", sourceIndexDirectory);
                    System.exit(1);
                }
            } else {
                usage(options);
                System.exit(1);
            }
            if (line.hasOption('d')) {
                String value = line.getOptionValue('d');
                destIndexDirectory = new File(value);
                if (destIndexDirectory.exists() == true) {
                    log.error("Index destination directory: {} already exist", destIndexDirectory);
                    System.exit(1);
                }
            } else {
                usage(options);
                System.exit(1);
            }
            if (line.hasOption('v')) {
                try {
                    String value = line.getOptionValue('v');
                    version = Version.parseLeniently(value);
                } catch (Exception e) {
                    log.error("Unrecognized index version, exiting");
                    usage(options);
                    System.exit(1);
                }
            }
            if (line.hasOption('r')) {
                String value = line.getOptionValue('r');
                String[] fields = value.trim().split(" *, *");
                if (fields == null || fields.length == 0) {
                    log.error("No fields were given, exiting");
                    usage(options);
                    System.exit(1);
                }
                (new FieldRemover()).removeFields(sourceIndexDirectory, destIndexDirectory, fields, version);
                System.exit(0);
            }
        } catch (IndexUtilityException e) {
            log.error("Failed to work on index:", e);
            System.exit(1);
        } catch (MissingOptionException e) {
            log.error("Mandatory options is missing!");
            usage(options);
            System.exit(1);
        } catch (ParseException e) {
            log.error("Failed to parse commandline options!");
            usage(options);
            System.exit(1);
        }
    }

    private static void initOptions(Options options) {
        Option sourceDirectory = new Option("s", "source", true, "source index directory");
        Option destDirectory = new Option("d", "dest", true, "destination index directory");
        Option fields = new Option("r", "fields", true, "fields to remove");
        Option version = new Option("v", "version", true, "Lucene index version, format: 4.2");
        options.addOption(sourceDirectory);
        options.addOption(destDirectory);
        options.addOption(fields);
        options.addOption(version);
    }

    private static void usage(Options options) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp(USAGE, HEADER, options, FOOTER);
    }

}