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.多字段查詢 多個(gè)filed對(duì)應(yīng)一個(gè)keyword
創(chuàng)新新互聯(lián),憑借10余年的網(wǎng)站設(shè)計(jì)、做網(wǎng)站經(jīng)驗(yàn),本著真心·誠心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計(jì)網(wǎng)站有超過千家案例。做網(wǎng)站建設(shè),選創(chuàng)新互聯(lián)建站。QueryParser parser = new MultiFieldQueryParser(fieldNames, analyzer);
parser.setAllowLeadingWildcard(true);
Query query= null;
query = parser.parse(keyword);
hits = searcher.search(query);
3.復(fù)合查詢 一個(gè)filed對(duì)應(yīng)一個(gè)keyword 剩下的多個(gè)field對(duì)應(yīng)另外一個(gè)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));
}
}