Lucene doesn´t find anything in version 4.0
Hi,
I´m very new to lucene and unfortunality there isn´t very much documentation available for lucene 4.0 API.
But to learn it, I created two classes - one to create the index and one to search in it.
Here is my index creating class which works fine I think:
Code:
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class LuceneIndexer
{
public static void main(String[] args)
{
try
{
//database fields
Class.forName ("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@scsp10:1521:s10o11g", "xxx", "xxx");
Statement stm = con.createStatement();
ResultSet rs;
//lucene fields
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
Directory index = FSDirectory.open(new File("C:\\lucene"));
IndexWriterConfig config= new IndexWriterConfig(Version.LUCENE_40, analyzer);
config.setOpenMode(OpenMode.CREATE);
IndexWriter writer = new IndexWriter(index, config);
Field field = null;
// rs = stm.executeQuery("SELECT * from IMPORT_MESSUNG_2_PROOF -- where SAFO_NR = 'F7VT65'");
rs = stm.executeQuery("SELECT * FROM MESSUNG_2_PROOF WHERE TEINTRAGZEIT >= to_date('11.07.2012', 'dd.mm.yyyy') + 1 AND TEINTRAGZEIT < to_date('11.07.2012', 'dd.mm.yyyy') + 2 ORDER BY TEINTRAGZEIT ASC");
System.out.println("LID \t SAFO_NR");
while(rs.next())
{
Document doc = new Document();
//Field field = new TextField("SAFO_NR", rs.getString(2), Field.Store.YES);
field = new StoredField("SAFO_NR", rs.getString(2).trim());
doc.add(field);
System.out.println(rs.getString(1) + "\t" + rs.getString(2));
System.out.println(field.name() + " : " + field.stringValue());
writer.addDocument(doc);
}
con.close();
stm.close();
System.out.println("Anzahl: " + writer.numDocs());
writer.close();
index.close();
}
catch (SQLException e)
{
System.err.println("A SQL exception occured");
e.printStackTrace();
}
catch (IOException e)
{
System.out.println("Error writing document");
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
System.err.println("Error loading JDBC driver");
e.printStackTrace();
}
}
}
There should always be now entries like "SAFO_NR : F7VT65" in the index (how can I check this?).
Then I developed my searcher class:
Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class LuceneSearcher
{
public static void main(String[] args)
{
try
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("SAFO_NR eingeben:");
String querystr = bf.readLine();
System.out.println(querystr);
int hitsPerPage = 10;
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
Directory index = FSDirectory.open(new File("C:\\lucene"));
DirectoryReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
// int num = reader.numDocs();
// for ( int i = 0; i < num; i++)
// {
//
// Document d = reader.document( i);
// System.out.println( "d=" +d);
//
// }
// System.out.println(num);
// Term term = new Term("SAFO_NR",querystr);
// Query termQuery = new TermQuery(term);
// TopDocs topDocs = searcher.search(termQuery,10);
// ScoreDoc[] hits = topDocs.scoreDocs;
System.out.println("Search for " + querystr);
QueryParser parser = new QueryParser(Version.LUCENE_40, "SAFO_NR", analyzer);
Query query = parser.parse(querystr.trim());
// Document tmp = searcher.doc(5);
// Iterator<IndexableField> fields = tmp.iterator();
// while(fields.hasNext())
// {
// IndexableField temp = fields.next();
// System.out.println(temp.name() + ":=" + temp.stringValue());
// }
ScoreDoc[] hits = searcher.search(query, hitsPerPage).scoreDocs;
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i<hits.length;++i)
{
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("SAFO_NR"));
}
reader.close();
index.close();
}
catch (IOException e)
{
System.out.println("Error writing document");
e.printStackTrace();
}
catch (ParseException e)
{
System.err.println("Error reading query string");
e.printStackTrace();
}
}
}
I thought now to get values entering "F7VT65" as querystring. But my result is always:
Quote:
SAFO_NR eingeben:
F7VT65
F7VT65
Search for F7VT65
Found 0 hits.
Has anyone an idea, why I don´t get results?
In C:\lucene I get the files _0.fdt (114KB) and _0.fdx (91KB) - so I think the indexing class works.
Thanks in advance!