lucene问题

    索引已经建立,但就是检索不到数据。hits.length()一直为0

public hits search(string querystring) {
hits hits = null;

try {

file file = new file(indexpath);

directory directory = fsdirectory.getdirectory(file);

indexsearcher searcher = new indexsearcher(directory);

string[] fields = { "customerid", "shipname", "shipcity",
"shipcountry" };
multifieldqueryparser mquerypaser = new multifieldqueryparser(
fields, getanalyzer());

query query = mquerypaser.parse(querystring);

hits = searcher.search(query);

} catch (exception e) {
//do something here
}

return hits;
}

public void doindex(string sql) {

try {

indexwriter writer = new indexwriter(indexpath, getanalyzer(), true);

resultset rs = lucenedb.getresult(sql);

while (rs.next()) {
document doc = new document();
doc.add(new field("customerid", rs.getstring("customerid"),
field.store.yes, field.index.tokenized));
doc.add(new field("shipname", rs.getstring("shipname"),
field.store.yes, field.index.tokenized));
doc.add(new field("shipcity", rs.getstring("shipcity"),
field.store.yes, field.index.tokenized));
doc.add(new field("shipcountry", rs.getstring("shipcountry"),
field.store.yes, field.index.tokenized));

writer.adddocument(doc);
}
writer.optimize();
writer.close();

} catch (corruptindexexception ce) {
system.out.println(ce.getmessage());
} catch (ioexception ie) {
system.out.println(ie.getmessage());
} catch (sqlexception se) {
system.out.println(se.getmessage());
}
}


public analyzer getanalyzer() {
return new standardanalyzer();

你可能感兴趣的:(java,工作)