lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vinicius Carvalho" <viniciusccarva...@gmail.com>
Subject [noobie question] Can't index :(
Date Wed, 19 Mar 2008 22:21:25 GMT
Hello there! This is really a dumb question, but I just need to get things
started :( I'm just trying to get things working here, and I'm not being
able to index :(. Here's my code:

public abstract class AbstractLuceneIndexer implements LuceneIndexer{

    protected String INDEX_DIR = "";


    public void add(Document document) {
        try {
            if(!IndexReader.isLocked(INDEX_DIR)){
                getIndexWriter().setMaxFieldLength(Integer.MAX_VALUE);
                getIndexWriter().addDocument(document);
                optimize();
                System.out.println(getIndexWriter().docCount());
                getIndexWriter().close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void delete(Document document) {
        try {
            if(!IndexReader.isLocked(INDEX_DIR)){
                getIndexReader().deleteDocument(findDocumentNum(document));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private int findDocumentNum(Document doc){
        return 0;
    }

    public void optimize() {

    }

    public Hits query() {
        return null;
    }

    protected abstract IndexWriter getIndexWriter();

    protected abstract IndexReader getIndexReader();

    protected abstract Analyzer getAnalyzer();


}

public class LocalLuceneIndexer extends AbstractLuceneIndexer {
    private Directory dir;
    public LocalLuceneIndexer() {
        INDEX_DIR = "/java/lucene/index";
        try {
            this.dir = FSDirectory.getDirectory(INDEX_DIR);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected IndexReader getIndexReader() {
        IndexReader reader = null;
        try {
            reader = IndexReader.open(dir);
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reader;
    }

    @Override
    protected IndexWriter getIndexWriter() {
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(INDEX_DIR,new StandardAnalyzer(),true);
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return writer;
    }

    @Override
    protected Analyzer getAnalyzer() {
        return new StandardAnalyzer();
    }

}

@Test
    public void testAddPDF() throws Exception{
        LocalLuceneIndexer indexer = new LocalLuceneIndexer();
        DocumentHandler handler = new PDFDocumentHandler();
        File file = new File("/home/vinicius/samplePDF.pdf");
        FileInputStream fin = new FileInputStream(file);
        Document doc = handler.getDocument(fin);
        indexer.add(doc);
      }

Well, after running the test above, I have only 2 files stored at disk :
segments.gen and segments_5. Luke can not open the index dir. What am I
missing here?

Regards

-- 
"In a world without fences and walls, who needs Gates and Windows?"

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message