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();
for (int i = 0; i < hits.length(); i++) {
docList.add(hits.doc(i));
}
}
searcher.close();
2.多字段查询 多个filed对应一个keyword
创新新互联,凭借10余年的网站设计、做网站经验,本着真心·诚心服务的企业理念服务于成都中小企业设计网站有超过千家案例。做网站建设,选创新互联建站。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();
for (int i = 0; i < hits.length(); i++) {
docList.add(hits.doc(i));
}
}