Re: database search using Lucene
Hi,
I am getting compile time error symbol not found on line
doc.add(Field.Text("contents", new FileReader(f)));
doc.add(Field.Keyword("filename", f.getCanonicalPath()));
of method indexFile(IndexWriter writer, File f)
when i look into class Field i didn't found any thing about Text and
Keyword symbol.
what i have to do to resolve above compile time error...
/**
*
*/
import java.sql.*;
import java.io.*;
import org.apache.lucene.index.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.*;
/**
* @author apurohit
*
*/
/**
* This code was originally written for
* Erik's Lucene intro java.net article
*/
public class FileIndex {
public static void main(String[] args) throws Exception {
File indexDir = new File("fileindex");
File dataDir = new File("txt_file");
int numIndexed = index(indexDir, dataDir);
}
// open an index and start file directory traversal
public static int index(File indexDir, File dataDir)throws
IOException {
//Listing 1.1 Indexer: traverses a file system and
indexes .txt files
//Create Lucene index in this directory Index files in this
directory
//Lucene in action: a sample application 13
if (!dataDir.exists() || !dataDir.isDirectory()) {
throw new IOException(dataDir+ " does not exist or is not a
directory");
}
IndexWriter writer = new IndexWriter(indexDir,new
StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
indexDirectory(writer, dataDir);
int numIndexed = writer.docCount();
writer.optimize();
writer.close();
return numIndexed;
}
// recursive method that calls itself when it finds a directory
private static void indexDirectory(IndexWriter writer, File
dir)throws IOException {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
indexDirectory(writer, f);
} else if (f.getName().endsWith(".txt")) {
indexFile(writer, f);
}
}
}
// method to actually index a file using Lucene
private static void indexFile(IndexWriter writer, File f)throws
IOException {
if (f.isHidden() || !f.exists() || !f.canRead()) {
return;
}
System.out.println("Indexing " + f.getCanonicalPath());
Document doc = new Document();
doc.add(Field.Text("contents", new FileReader(f)));
doc.add(Field.Keyword("filename", f.getCanonicalPath()));
writer.addDocument(doc);
}
}
Thanks...