关于lucene2.2部分代码
1.单字段查询,一个filed对应一个keyword
QueryParser parser = new MultiFieldQueryParser(fieldName, analyzer); parser.setAllowLeadingWildcard(true); Query query = null;
query = parser.parse(keyword);
hits = searcher.search(query);
if (hits != null) {
docList = new ArrayList<Document>();
for (int i = 0; i < hits.length(); i++) {
docList.add(hits.doc(i));
}
}
searcher.close();
2.多字段查询 多个filed对应一个keyword
QueryParser parser = new MultiFieldQueryParser(fieldNames, analyzer); parser.setAllowLeadingWildcard(true); Query query = null;
query = parser.parse(keyword);
hits = searcher.search(query);
3.复合查询 一个filed对应一个keyword 剩下的多个field对应另外一个keyword
BooleanQuery bQuery = new BooleanQuery(); QueryParser queryParserCategory = new QueryParser(fieldNames[0], analyzer); queryParserCategory.setAllowLeadingWildcard(true); Query queryCategory = null;
queryCategory = queryParserCategory.parse(keywords[0]);
bQuery.add(queryCategory, BooleanClause.Occur.MUST);
String[] fieldNamesLeft = new String[fieldNames.length - 1];
for (int i = 1; i < fieldNames.length; i++) {
fieldNamesLeft[i - 1] = fieldNames[i];
}
QueryParser parser = new MultiFieldQueryParser(fieldNamesLeft, analyzer);
parser.setAllowLeadingWildcard(true);
Query query = null;
query = parser.parse(keywords[1]);
bQuery.add(query, BooleanClause.Occur.MUST);
if (bQuery != null) {
hits = searcher.search(bQuery);
if (hits != null) {
docList = new ArrayList<Document>();
for (int i = 0; i < hits.length(); i++) {
docList.add(hits.doc(i));
}
}