FileDocCategorySizeDatePackage
ParallelReader.javaAPI DocApache Lucene 2.2.013622Sat Jun 16 22:20:36 BST 2007org.apache.lucene.index

ParallelReader

public class ParallelReader extends IndexReader
An IndexReader which reads multiple, parallel indexes. Each index added must have the same number of documents, but typically each contains different fields. Each document contains the union of the fields of all documents with the same document number. When searching, matches for a query term are from the first index added that has the field.

This is useful, e.g., with collections that have large fields which change rarely and small fields that change more frequently. The smaller fields may be re-indexed in a new index and both indexes may be searched together.

Warning: It is up to you to make sure all indexes are created and modified the same way. For example, if you add documents to one index, you need to add the same documents in the same order to the other indexes. Failure to do so will result in undefined behavior.

Fields Summary
private List
readers
private SortedMap
fieldToReader
private Map
readerToFields
private List
storedFieldReaders
private int
maxDoc
private int
numDocs
private boolean
hasDeletions
Constructors Summary
public ParallelReader()
Construct a ParallelReader.


     
       super(null); 
Methods Summary
public voidadd(org.apache.lucene.index.IndexReader reader)
Add an IndexReader.

throws
IOException if there is a low-level IO error

    ensureOpen();
    add(reader, false);
  
public voidadd(org.apache.lucene.index.IndexReader reader, boolean ignoreStoredFields)
Add an IndexReader whose stored fields will not be returned. This can accellerate search when stored fields are only needed from a subset of the IndexReaders.

throws
IllegalArgumentException if not all indexes contain the same number of documents
throws
IllegalArgumentException if not all indexes have the same value of {@link IndexReader#maxDoc()}
throws
IOException if there is a low-level IO error


    ensureOpen();
    if (readers.size() == 0) {
      this.maxDoc = reader.maxDoc();
      this.numDocs = reader.numDocs();
      this.hasDeletions = reader.hasDeletions();
    }

    if (reader.maxDoc() != maxDoc)                // check compatibility
      throw new IllegalArgumentException
        ("All readers must have same maxDoc: "+maxDoc+"!="+reader.maxDoc());
    if (reader.numDocs() != numDocs)
      throw new IllegalArgumentException
        ("All readers must have same numDocs: "+numDocs+"!="+reader.numDocs());

    Collection fields = reader.getFieldNames(IndexReader.FieldOption.ALL);
    readerToFields.put(reader, fields);
    Iterator i = fields.iterator();
    while (i.hasNext()) {                         // update fieldToReader map
      String field = (String)i.next();
      if (fieldToReader.get(field) == null)
        fieldToReader.put(field, reader);
    }

    if (!ignoreStoredFields)
      storedFieldReaders.add(reader);             // add to storedFieldReaders
    readers.add(reader);
  
protected synchronized voiddoClose()

    for (int i = 0; i < readers.size(); i++)
      ((IndexReader)readers.get(i)).close();
  
protected voiddoCommit()

    for (int i = 0; i < readers.size(); i++)
      ((IndexReader)readers.get(i)).commit();
  
protected voiddoDelete(int n)

    for (int i = 0; i < readers.size(); i++) {
      ((IndexReader)readers.get(i)).deleteDocument(n);
    }
    hasDeletions = true;
  
protected voiddoSetNorm(int n, java.lang.String field, byte value)

    IndexReader reader = ((IndexReader)fieldToReader.get(field));
    if (reader!=null)
      reader.doSetNorm(n, field, value);
  
protected voiddoUndeleteAll()

    for (int i = 0; i < readers.size(); i++) {
      ((IndexReader)readers.get(i)).undeleteAll();
    }
    hasDeletions = false;
  
public intdocFreq(org.apache.lucene.index.Term term)

    ensureOpen();
    IndexReader reader = ((IndexReader)fieldToReader.get(term.field()));
    return reader==null ? 0 : reader.docFreq(term);
  
public org.apache.lucene.document.Documentdocument(int n, org.apache.lucene.document.FieldSelector fieldSelector)

    ensureOpen();
    Document result = new Document();
    for (int i = 0; i < storedFieldReaders.size(); i++) {
      IndexReader reader = (IndexReader)storedFieldReaders.get(i);

      boolean include = (fieldSelector==null);
      if (!include) {
        Iterator it = ((Collection) readerToFields.get(reader)).iterator();
        while (it.hasNext())
          if (fieldSelector.accept((String)it.next())!=FieldSelectorResult.NO_LOAD) {
            include = true;
            break;
          }
      }
      if (include) {
        Iterator fieldIterator = reader.document(n, fieldSelector).getFields().iterator();
        while (fieldIterator.hasNext()) {
          result.add((Fieldable)fieldIterator.next());
        }
      }
    }
    return result;
  
public java.util.CollectiongetFieldNames(org.apache.lucene.index.IndexReader$FieldOption fieldNames)

    ensureOpen();
    Set fieldSet = new HashSet();
    for (int i = 0; i < readers.size(); i++) {
      IndexReader reader = ((IndexReader)readers.get(i));
      Collection names = reader.getFieldNames(fieldNames);
      fieldSet.addAll(names);
    }
    return fieldSet;
  
public org.apache.lucene.index.TermFreqVectorgetTermFreqVector(int n, java.lang.String field)

    ensureOpen();
    IndexReader reader = ((IndexReader)fieldToReader.get(field));
    return reader==null ? null : reader.getTermFreqVector(n, field);
  
public org.apache.lucene.index.TermFreqVector[]getTermFreqVectors(int n)

    ensureOpen();
    ArrayList results = new ArrayList();
    Iterator i = fieldToReader.entrySet().iterator();
    while (i.hasNext()) {
      Map.Entry e = (Map.Entry)i.next();
      String field = (String)e.getKey();
      IndexReader reader = (IndexReader)e.getValue();
      TermFreqVector vector = reader.getTermFreqVector(n, field);
      if (vector != null)
        results.add(vector);
    }
    return (TermFreqVector[])
      results.toArray(new TermFreqVector[results.size()]);
  
public booleanhasDeletions()

    // Don't call ensureOpen() here (it could affect performance)
    return hasDeletions;
  
public booleanhasNorms(java.lang.String field)

    ensureOpen();
    IndexReader reader = ((IndexReader)fieldToReader.get(field));
    return reader==null ? false : reader.hasNorms(field);
  
public booleanisDeleted(int n)

    // Don't call ensureOpen() here (it could affect performance)
    if (readers.size() > 0)
      return ((IndexReader)readers.get(0)).isDeleted(n);
    return false;
  
public intmaxDoc()

    // Don't call ensureOpen() here (it could affect performance)
    return maxDoc;
  
public byte[]norms(java.lang.String field)

    ensureOpen();
    IndexReader reader = ((IndexReader)fieldToReader.get(field));
    return reader==null ? null : reader.norms(field);
  
public voidnorms(java.lang.String field, byte[] result, int offset)

    ensureOpen();
    IndexReader reader = ((IndexReader)fieldToReader.get(field));
    if (reader!=null)
      reader.norms(field, result, offset);
  
public intnumDocs()

    // Don't call ensureOpen() here (it could affect performance)
    return numDocs;
  
public org.apache.lucene.index.TermDocstermDocs(org.apache.lucene.index.Term term)

    ensureOpen();
    return new ParallelTermDocs(term);
  
public org.apache.lucene.index.TermDocstermDocs()

    ensureOpen();
    return new ParallelTermDocs();
  
public org.apache.lucene.index.TermPositionstermPositions(org.apache.lucene.index.Term term)

    ensureOpen();
    return new ParallelTermPositions(term);
  
public org.apache.lucene.index.TermPositionstermPositions()

    ensureOpen();
    return new ParallelTermPositions();
  
public org.apache.lucene.index.TermEnumterms()

    ensureOpen();
    return new ParallelTermEnum();
  
public org.apache.lucene.index.TermEnumterms(org.apache.lucene.index.Term term)

    ensureOpen();
    return new ParallelTermEnum(term);