8、学习Lucene3.5索引搜索之TermRange

网友投稿 658 2022-11-25

8、学习Lucene3.5索引搜索之TermRange

8、学习Lucene3.5索引搜索之TermRange

1、具体的查询语句

/** * 执行具体的查询 */private void excuteQuery(IndexSearcher indexSearcher, Query query) { try { TopDocs topDocs = indexSearcher.search(query, 2); System.out.println("查询出来的总条数:"+topDocs.totalHits); ScoreDoc[] scoreDocs = topDocs.scoreDocs; if (isNotEmpty(scoreDocs)){ for (ScoreDoc scoreDoc:scoreDocs){ Document document = indexSearcher.doc(scoreDoc.doc); System.out.println("("+scoreDoc.doc+")"+document.get("name")+"["+document.get("email")+"]-->"+document.get("id") + "," +document.get("attach") ); } } } catch (IOException e) { e.printStackTrace(); } finally { closeIndexSearcher(indexSearcher); }}

2、精确查询

/** * Lucene搜索之TermQuery */public void searchByTermQuery(String field, String value){ Query query = new TermQuery(new Term(field, value)); IndexSearcher indexSearcher = createIndexSearcher(); excuteQuery(indexSearcher,query);}

3、文本范围查询

/** * Lucene搜索之TermRangeQuery */public void searchByTermRangeQuery(String field, String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper){ IndexSearcher indexSearcher = createIndexSearcher(); /** * TermRangeQuery(String field, String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper): * 参数一:域 * 参数二:开始字符 * 参数三:结束字符 * 参数四:是否包含开头 * 参数五:是否包含结尾 */ Query query = new TermRangeQuery(field, lowerTerm, upperTerm, includeLower, includeUpper); excuteQuery(indexSearcher,query);}

4、数字范围查询

/** * Lucene搜索之NumericRangeQuery(查询数字范围) */public void searchByNumericRangeQuery(String field, Integer min, Integer max, boolean minInclusive, boolean maxInclusive){ IndexSearcher indexSearcher = createIndexSearcher(); /** * TermRangeQuery(String field, Integer min, Integer max, boolean minInclusive, boolean maxInclusive): * 参数一:域 * 参数二:最小值 * 参数三:最大值 * 参数四:是否包含最小值 * 参数五:是否包含最大值 */ Query query = NumericRangeQuery.newIntRange(field,min,max,minInclusive,maxInclusive); excuteQuery(indexSearcher, query);}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:4、学习Lucene3.5之索引删除、更新
下一篇:17、学习Lucene3.5索引之使用Filter(过滤器)搜索
相关文章

 发表评论

暂时没有评论,来抢沙发吧~