成都创新互联网站制作重庆分公司

关于lucene2.2部分代码-创新互联

1.单字段查询,一个filed对应一个keyword关于lucene2.2部分代码
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));
               }
            }


文章题目:关于lucene2.2部分代码-创新互联
分享路径:http://cxhlcq.com/article/djdcpi.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部