lucene入门-搜索方式
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
lucene入门-搜索方式的更多相关文章
- Lucene 搜索方式
Lucene 的搜索方式包括:词项查询(TermQuery) / 布尔查询(BooleanQuery) / 短语查询(PhraseQuery) / 范围查询(RangeQuery) / 百搭查询(Wi ...
- Lucene搜索方式大合集
package junit; import java.io.File; import java.io.IOException; import java.text.ParseException; imp ...
- lucene搜索方式(query类型)
Lucene有多种搜索方式,可以根据需要选择不同的方式. 1.词条搜索(单个关键字查找) 主要对象是TermQuery 调用方式如下: Term term=new Term(字段名,搜索关键字);Qu ...
- lucene入门
一.lucene简介 Lucene是apache下的一个靠性能的.功能全面的用纯java开发的一个全文搜索引擎库.它几乎适合任何需要全文搜索应用程序,尤其是跨平台.lucene是开源的免费的工程.lu ...
- Lucene入门的基本知识(四)
刚才在写创建索引和搜索类的时候发现非常多类的概念还不是非常清楚,这里我总结了一下. 1 lucene简单介绍 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不 ...
- Lucene入门教程
Lucene教程 1 lucene简介 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么 ...
- Lucene入门教程(转载)
http://blog.csdn.net/tianlincao/article/details/6867127 Lucene教程 1 lucene简介 1.1 什么是lucene Lucene ...
- Lucene入门简介
一 Lucene产生的背景 数据库中的搜索很容易实现,通常都是使用sql语句进行查询,而且能很快的得到查询结果. 为什么数据库搜索很容易? 因为数据库中的数据存储是有规律的,有行有列而且数据格式.数 ...
- 十九种Elasticsearch字符串搜索方式终极介绍
前言 刚开始接触Elasticsearch的时候被Elasticsearch的搜索功能搞得晕头转向,每次想在Kibana里面查询某个字段的时候,查出来的结果经常不是自己想要的,然而又不知道问题出在了哪 ...
随机推荐
- Python练习题中做错题目
1,一下代码执行的结果为 a = b = "julyedu.com" a = 'AI 教育' print(b) 答案: julyedu.com 要点: 在python中, 不可变对 ...
- 在一台机上搭建多个MYSQL并设置主从
安装 cd /usr/local/src/ -linux2.-x86_64.tar.gz -linux2.-x86_64 /usr/local/mysql grep mysql /etc/passwd ...
- 3、Cookie与Session之间有哪些区别或者是优缺点?
Cookie与Session之间有哪些区别或者是优缺点? 知道了Cookie与Session,我们来做一些简单的总结: 1.Cookie可以存储在浏览器或者本地,session只能存在服务器 ...
- css3继承
不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...
- 使用target属性跳转到指定位置
先上代码,使用frameset将网页分割为三个窗口,上,左和右. 1 <html> 2 <head> 3 <title>Main</title> 4 & ...
- hbase启动的时候报:cat: /home/hadoop/hbase-0.94.6-cdh4.5.0/target/cached_classpath.txt: 没有那个文件或目录
启动hbase的时候: -cdh4.5.0/bin$ hbase shell cat: /home/hadoop/hbase--cdh4.5.0/target/cached_classpath.txt ...
- leetcode.字符串.242有效的字母异位词-Java
1. 具体题目 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 注:判断两个字符串包含的字母是否完全一样. 示例 1: 输入: s = "anagram&q ...
- git 常用命令 mv rm checkout revert reset
关于上节讲的git add 时需要添加注释信息,也可以在git commit时再添加 laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/gi ...
- 要素选择变化事件 IActiveViewEvents_SelectionChanged
void IDockableWindowDef.OnCreate(object hook) { m_application = hook as IApplication; m_hookHelper = ...
- extern static和函数
#include <stdio.h> int sum(int a, int b); int main() { /************************************** ...