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?"