Search code examples
javalucenesearch-engine

Lucene : How to get LongField from index


I add a LongField into index.

The method

IndexSearcher.doc() 

returns a document, within which all fields are either

org.apache.lucene.document.Field or org.apache.lucene.document.StoredField

How do I get a document from index with same field type as I put into. So then I copy this document to another index with the same field type.


Solution

  • At last , I find another method IndexSearcher.doc(int, StoredFieldVisitor) (I am using lucene 4.3)

    And create a custom StoredFieldVisitor

    public class StaySameFieldVisitor extends DocumentStoredFieldVisitor {
    @Override
    public void intField(FieldInfo fieldInfo, int value) {
        getDocument().add(new IntField(fieldInfo.name, value, Store.YES));
    }
    
    @Override
    public void longField(FieldInfo fieldInfo, long value) {
        getDocument().add(new LongField(fieldInfo.name, value, Store.YES));
    }
    
    @Override
    public void doubleField(FieldInfo fieldInfo, double value) {
        getDocument().add(new DoubleField(fieldInfo.name, value, Store.YES));
    }
    
    @Override
    public void floatField(FieldInfo fieldInfo, float value) {
        getDocument().add(new FloatField(fieldInfo.name, value, Store.YES));
    }
    

    }