concept Directory in category lucene

appears as: Directory, Directory
Lucene in Action, Second Edition

This is an excerpt from Manning's book Lucene in Action, Second Edition.

The Directory class represents the location of a Lucene index. It’s an abstract class that allows its subclasses to store the index as they see fit. In our Indexer example, we used FSDirectory.open to get a suitable concrete FSDirectory implementation that stores real files in a directory on the file system, and passed that in turn to IndexWriter’s constructor.

Lucene includes a number of interesting Directory implementations, covered in section 2.10. IndexWriter can’t index text unless it’s first been broken into separate words, using an analyzer.

  • Directory, where the index is stored.
  • Recall from chapter 1 that the purpose of Lucene’s abstract Directory class is to present a simple file-like storage API, hiding away the details of what underlying mechanism is performing the storage. Whenever Lucene needs to write to or read from files in the index, it uses the Directory methods to do so. Table 2.2 lists the five core Directory implementations available in Lucene 3.0.

    Table 2.2. Lucene’s several core Directory implementations

    Directory

    Description

    SimpleFSDirectory A simplistic Directory that stores files in the file system, using java.io.* APIs. It doesn’t scale well with many threads.
    NIOFSDirectory A Directory that stores files in the file system, using java.nio.* APIs. This does scale well with threads on all platforms except Microsoft Windows, due to a longstanding issue with Sun’s Java Runtime Environment (JRE).
    MMapDirectory A Directory that uses memory-mapped I/O to access files. This is a good choice on 64-bit JREs, or on 32-bit JREs where the size of the index is relatively small.
    RAMDirectory A Directory that stores all files in RAM.
    FileSwitchDirectory A Directory that takes two directories in, and switches between these directories based on file extension.

    Of these classes, three are concrete Directory implementations to read and write files from the file system. They’re all subclasses of the abstract FSDirectory base class. Unfortunately, there’s no single best FSDirectory implementation. Each has potentially serious limitations in certain situations:

  • SimpleFSDirectory uses java.io.* APIs for access. Unfortunately, this Directory implementation doesn’t scale during reading when multiple threads are in use because it must do internal locking to work around the lack of positional reads in java.io.*.
  • IndexWriter writer = new IndexWriter(otherDir, analyzer,
                                         IndexWriter.MaxFieldLength.UNLIMITED);
    writer.addIndexesNoOptimize(new Directory[] {ramDir});