Class IndexUtils


  • public final class IndexUtils
    extends Object
    Utilities for various raw index operations.

    This is for internal uses, DO NOT call from UI components or applications.

    • Method Detail

      • openIndex

        public static IndexReader openIndex​(String indexPath,
                                            String dirImpl)
                                     throws Exception
        Opens index(es) reader for given index path.
        Parameters:
        indexPath - - path to the index directory
        dirImpl - - class name for the specific directory implementation
        Returns:
        index reader
        Throws:
        Exception - - if there is a low level IO error.
      • openDirectory

        public static Directory openDirectory​(String dirPath,
                                              String dirImpl)
                                       throws IOException
        Opens an index directory for given index path.

        This can be used to open/repair corrupted indexes.

        Parameters:
        dirPath - - index directory path
        dirImpl - - class name for the specific directory implementation
        Returns:
        directory
        Throws:
        IOException - - if there is a low level IO error.
      • close

        public static void close​(Directory dir)
        Close index directory.
        Parameters:
        dir - - index directory to be closed
      • close

        public static void close​(IndexReader reader)
        Close index reader.
        Parameters:
        reader - - index reader to be closed
      • createWriter

        public static IndexWriter createWriter​(Directory dir,
                                               Analyzer analyzer,
                                               boolean useCompound,
                                               boolean keepAllCommits)
                                        throws IOException
        Create an index writer.
        Parameters:
        dir - - index directory
        analyzer - - analyzer used by the index writer
        useCompound - - if true, compound index files are used
        keepAllCommits - - if true, all commit generations are kept
        Returns:
        new index writer
        Throws:
        IOException - - if there is a low level IO error.
      • createWriter

        public static IndexWriter createWriter​(Directory dir,
                                               Analyzer analyzer,
                                               boolean useCompound,
                                               boolean keepAllCommits,
                                               PrintStream ps)
                                        throws IOException
        Create an index writer.
        Parameters:
        dir - - index directory
        analyzer - - analyser used by the index writer
        useCompound - - if true, compound index files are used
        keepAllCommits - - if true, all commit generations are kept
        ps - - information stream
        Returns:
        new index writer
        Throws:
        IOException - - if there is a low level IO error.
      • optimizeIndex

        public static void optimizeIndex​(IndexWriter writer,
                                         boolean expunge,
                                         int maxNumSegments)
                                  throws IOException
        Execute force merge with the index writer.
        Parameters:
        writer - - index writer
        expunge - - if true, only segments having deleted documents are merged
        maxNumSegments - - max number of segments
        Throws:
        IOException - - if there is a low level IO error.
      • tryRepairIndex

        public static void tryRepairIndex​(Directory dir,
                                          CheckIndex.Status st,
                                          PrintStream ps)
                                   throws IOException
        Try to repair the corrupted index using previously returned index status.
        Parameters:
        dir - - index directory for repairing
        st - - index status
        ps - - information stream
        Throws:
        IOException - - if there is a low level IO error.
      • getIndexFormat

        public static String getIndexFormat​(Directory dir)
                                     throws IOException
        Returns the string representation for Lucene codec version when the index was written.
        Parameters:
        dir - - index directory
        Throws:
        IOException - - if there is a low level IO error.
      • getCommitUserData

        public static String getCommitUserData​(IndexCommit ic)
                                        throws IOException
        Returns user data written with the specified commit.
        Parameters:
        ic - - index commit
        Throws:
        IOException - - if there is a low level IO error.
      • countTerms

        public static Map<String,​Long> countTerms​(IndexReader reader,
                                                        Collection<String> fields)
                                                 throws IOException
        Collect all terms and their counts in the specified fields.
        Parameters:
        reader - - index reader
        fields - - field names
        Returns:
        a map contains terms and their occurrence frequencies
        Throws:
        IOException - - if there is a low level IO error.
      • getLiveDocs

        public static Bits getLiveDocs​(IndexReader reader)
        Returns the Bits representing live documents in the index.
        Parameters:
        reader - - index reader
      • getFieldInfos

        public static FieldInfos getFieldInfos​(IndexReader reader)
        Returns field FieldInfos in the index.
        Parameters:
        reader - - index reader
      • getFieldInfo

        public static FieldInfo getFieldInfo​(IndexReader reader,
                                             String fieldName)
        Returns the FieldInfo referenced by the field.
        Parameters:
        reader - - index reader
        fieldName - - field name
      • getFieldNames

        public static Collection<String> getFieldNames​(IndexReader reader)
        Returns all field names in the index.
        Parameters:
        reader - - index reader
      • getTerms

        public static Terms getTerms​(IndexReader reader,
                                     String field)
                              throws IOException
        Returns the Terms for the specified field.
        Parameters:
        reader - - index reader
        field - - field name
        Throws:
        IOException - - if there is a low level IO error.