关于lucene2.2部分代码-创新互联-成都快上网建站

关于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://kswjz.com/article/djdcpi.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流