Search in sources :

Example 1 with DocumentStoredFieldVisitor

use of org.apache.lucene.document.DocumentStoredFieldVisitor in project lucene-solr by apache.

the class SearchTravRetLoadFieldSelectorTask method retrieveDoc.

@Override
protected Document retrieveDoc(IndexReader ir, int id) throws IOException {
    if (fieldsToLoad == null) {
        return ir.document(id);
    } else {
        DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(fieldsToLoad);
        ir.document(id, visitor);
        return visitor.getDocument();
    }
}
Also used : DocumentStoredFieldVisitor(org.apache.lucene.document.DocumentStoredFieldVisitor)

Example 2 with DocumentStoredFieldVisitor

use of org.apache.lucene.document.DocumentStoredFieldVisitor in project lucene-solr by apache.

the class TestFieldsReader method test.

public void test() throws IOException {
    assertTrue(dir != null);
    assertTrue(fieldInfos != null);
    IndexReader reader = DirectoryReader.open(dir);
    Document doc = reader.document(0);
    assertTrue(doc != null);
    assertTrue(doc.getField(DocHelper.TEXT_FIELD_1_KEY) != null);
    Field field = (Field) doc.getField(DocHelper.TEXT_FIELD_2_KEY);
    assertTrue(field != null);
    assertTrue(field.fieldType().storeTermVectors());
    assertFalse(field.fieldType().omitNorms());
    assertTrue(field.fieldType().indexOptions() == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
    field = (Field) doc.getField(DocHelper.TEXT_FIELD_3_KEY);
    assertTrue(field != null);
    assertFalse(field.fieldType().storeTermVectors());
    assertTrue(field.fieldType().omitNorms());
    assertTrue(field.fieldType().indexOptions() == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
    field = (Field) doc.getField(DocHelper.NO_TF_KEY);
    assertTrue(field != null);
    assertFalse(field.fieldType().storeTermVectors());
    assertFalse(field.fieldType().omitNorms());
    assertTrue(field.fieldType().indexOptions() == IndexOptions.DOCS);
    DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(DocHelper.TEXT_FIELD_3_KEY);
    reader.document(0, visitor);
    final List<IndexableField> fields = visitor.getDocument().getFields();
    assertEquals(1, fields.size());
    assertEquals(DocHelper.TEXT_FIELD_3_KEY, fields.get(0).name());
    reader.close();
}
Also used : Field(org.apache.lucene.document.Field) DocumentStoredFieldVisitor(org.apache.lucene.document.DocumentStoredFieldVisitor) Document(org.apache.lucene.document.Document)

Example 3 with DocumentStoredFieldVisitor

use of org.apache.lucene.document.DocumentStoredFieldVisitor in project lucene-solr by apache.

the class CheckIndex method testStoredFields.

/**
   * Test stored fields.
   * @lucene.experimental
   */
public static Status.StoredFieldStatus testStoredFields(CodecReader reader, PrintStream infoStream, boolean failFast) throws IOException {
    long startNS = System.nanoTime();
    final Status.StoredFieldStatus status = new Status.StoredFieldStatus();
    try {
        if (infoStream != null) {
            infoStream.print("    test: stored fields.......");
        }
        // Scan stored fields for all documents
        final Bits liveDocs = reader.getLiveDocs();
        StoredFieldsReader storedFields = reader.getFieldsReader().getMergeInstance();
        for (int j = 0; j < reader.maxDoc(); ++j) {
            // Intentionally pull even deleted documents to
            // make sure they too are not corrupt:
            DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
            storedFields.visitDocument(j, visitor);
            Document doc = visitor.getDocument();
            if (liveDocs == null || liveDocs.get(j)) {
                status.docCount++;
                status.totFields += doc.getFields().size();
            }
        }
        // Validate docCount
        if (status.docCount != reader.numDocs()) {
            throw new RuntimeException("docCount=" + status.docCount + " but saw " + status.docCount + " undeleted docs");
        }
        msg(infoStream, String.format(Locale.ROOT, "OK [%d total field count; avg %.1f fields per doc] [took %.3f sec]", status.totFields, (((float) status.totFields) / status.docCount), nsToSec(System.nanoTime() - startNS)));
    } catch (Throwable e) {
        if (failFast) {
            throw IOUtils.rethrowAlways(e);
        }
        msg(infoStream, "ERROR [" + String.valueOf(e.getMessage()) + "]");
        status.error = e;
        if (infoStream != null) {
            e.printStackTrace(infoStream);
        }
    }
    return status;
}
Also used : DocValuesStatus(org.apache.lucene.index.CheckIndex.Status.DocValuesStatus) StoredFieldsReader(org.apache.lucene.codecs.StoredFieldsReader) DocumentStoredFieldVisitor(org.apache.lucene.document.DocumentStoredFieldVisitor) Bits(org.apache.lucene.util.Bits) Document(org.apache.lucene.document.Document)

Example 4 with DocumentStoredFieldVisitor

use of org.apache.lucene.document.DocumentStoredFieldVisitor in project lucene-solr by apache.

the class IndexReader method document.

/**
   * Like {@link #document(int)} but only loads the specified
   * fields.  Note that this is simply sugar for {@link
   * DocumentStoredFieldVisitor#DocumentStoredFieldVisitor(Set)}.
   */
public final Document document(int docID, Set<String> fieldsToLoad) throws IOException {
    final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(fieldsToLoad);
    document(docID, visitor);
    return visitor.getDocument();
}
Also used : DocumentStoredFieldVisitor(org.apache.lucene.document.DocumentStoredFieldVisitor)

Example 5 with DocumentStoredFieldVisitor

use of org.apache.lucene.document.DocumentStoredFieldVisitor in project lucene-solr by apache.

the class IndexReader method document.

/**
   * Returns the stored fields of the <code>n</code><sup>th</sup>
   * <code>Document</code> in this index.  This is just
   * sugar for using {@link DocumentStoredFieldVisitor}.
   * <p>
   * <b>NOTE:</b> for performance reasons, this method does not check if the
   * requested document is deleted, and therefore asking for a deleted document
   * may yield unspecified results. Usually this is not required, however you
   * can test if the doc is deleted by checking the {@link
   * Bits} returned from {@link MultiFields#getLiveDocs}.
   *
   * <b>NOTE:</b> only the content of a field is returned,
   * if that field was stored during indexing.  Metadata
   * like boost, omitNorm, IndexOptions, tokenized, etc.,
   * are not preserved.
   * 
   * @throws CorruptIndexException if the index is corrupt
   * @throws IOException if there is a low-level IO error
   */
// TODO: we need a separate StoredField, so that the
// Document returned here contains that class not
// IndexableField
public final Document document(int docID) throws IOException {
    final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
    document(docID, visitor);
    return visitor.getDocument();
}
Also used : DocumentStoredFieldVisitor(org.apache.lucene.document.DocumentStoredFieldVisitor)

Aggregations

DocumentStoredFieldVisitor (org.apache.lucene.document.DocumentStoredFieldVisitor)6 Document (org.apache.lucene.document.Document)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 StoredFieldsReader (org.apache.lucene.codecs.StoredFieldsReader)1 Field (org.apache.lucene.document.Field)1 DocValuesStatus (org.apache.lucene.index.CheckIndex.Status.DocValuesStatus)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Query (org.apache.lucene.search.Query)1 ScoreDoc (org.apache.lucene.search.ScoreDoc)1 TermQuery (org.apache.lucene.search.TermQuery)1 Bits (org.apache.lucene.util.Bits)1