lucene入门-搜索方式
2024-09-25 23:45:24
1
package com.home.utils; import java.util.ArrayList;
import java.util.List; import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.junit.Test; /**
* 1、关键词查询 2、查询所有文档 3、范围查询 4、通配符查询 5、短语查询 6、Boolean查询
*
* @author Administrator
*
*/
public class QueryTest { private void showData(Query query) throws Exception {
IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
TopDocs topDocs = indexSearcher.search(query, 25);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articleList = new ArrayList<Article>();
for (ScoreDoc scoreDoc : scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
Article article = DocumentUtils.document2Article(document);
articleList.add(article);
} for (Article article : articleList) {
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
} /**
* 关键词查询 因为没有分词器,所以区分大小写
*/
@Test
public void testTermQuery() throws Exception {
Term term = new Term("title", "lucene");
Query query = new TermQuery(term);
this.showData(query);
} /**
* 查询所有的文档
*/
@Test
public void testAllDocQuery() throws Exception {
Query query = new MatchAllDocsQuery();
this.showData(query);
} /**
* 通配符查询 * 代表任意多个任意字符 ? 任意一个任意字符
*/
@Test
public void testWildCartQuery() throws Exception {
Term term = new Term("title", "*.java");
Query query = new WildcardQuery(term);
this.showData(query);
} /**
* 短语查询 所有的关键词对象必须针对同一个属性
*
* @param query
* @throws Exception
*/
@Test
public void testPharseQuery() throws Exception {
Term term = new Term("title", "lucene");
Term term2 = new Term("title", "搜索");
PhraseQuery query = new PhraseQuery();
query.add(term);
query.add(term2, 4);
this.showData(query); } /**
* boolean查询 各种关键词的组合
*
* @param query
* @throws Exception
*/
@Test
public void testBooleanQuery() throws Exception {
Term term = new Term("title", "北京");
TermQuery termQuery = new TermQuery(term);
Term term2 = new Term("title", "美女");
TermQuery termQuery2 = new TermQuery(term2);
Term term3 = new Term("title", "北京美女");
TermQuery termQuery3 = new TermQuery(term3); BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(termQuery, Occur.SHOULD);
booleanQuery.add(termQuery2, Occur.SHOULD);
booleanQuery.add(termQuery3, Occur.SHOULD);
this.showData(booleanQuery);
} /**
* 范围查询
* @param query
* @throws Exception
*/
@Test
public void testRangeQuery() throws Exception{
Query query = NumericRangeQuery.newLongRange("id", 5L, 10L, true, true);
this.showData(query);
} }
原文地址:https://www.cnblogs.com/sharpest/p/5992564.html
最新文章
- css如何实现多行文本时,内容溢出,出现省略号
- 详解shape标签
- 超级链接a+ confirm用法
- GDB下查看内存命令(x命令)
- css滚动条样式
- PMP考试的过与只是
- PreparedStatement 和 Statment区别
- Python之路第十一天,高级(3)-Python操作 Memcached、Redis
- windows phone 三种数据共享的方式(8)
- Windows任务计划程序起始于参数自动修改
- 【iOS】swift-获取webView的高度
- IO流(二)
- bootstrap简单使用实例
- Node.js学习看这里:基础、进阶、文章
- rem问题
- 687. Longest Univalue Path
- word漏洞分析与利用
- RF实现多次失败重跑结果合并的基础方法和优化方法
- ruby 数组操作
- javascript正则表达式获取控制
热门文章
- error C3861: “L”: 找不到标识符
- ssm+mysql 时间显示相差了12小时的问题 Gson
- hive元数据格式化 在hive中执行sql语句:SemanticException org.apache.hadoop.hive.ql.metadata.HiveException:
- JBoss、Tomcat、JBoss EAP、JBoss AS、wildfly,JBoss EAP安装部署,JBoss各个版本下载,JBoss允许远程访问
- linux网卡驱动更新方法
- 基于Python玩转人工智能最火框架 TensorFlow应用实践✍✍✍
- 高程(三)----数组Array
- json传参报错
- ros语音交互(五)移植科大讯飞语音识别到ros
- Mongodb安装遇到的问题