es之java各种查询操作
| matchAllQuery | 匹配所有文档 |
|---|---|
| queryStringQuery | 基于Lucene的字段检索 |
| wildcardQuery | 通配符查询匹配多个字符,?匹配1个字符* |
| termQuery | 词条查询 |
| matchQuery | 字段查询 |
| idsQuery | 标识符查询 |
| fuzzyQuery | 文档相似度查询 |
| includeLower includeUpper | 范围查询 |
| boolQuery | 组合查询(复杂查询) |
| SortOrder | 排序查询 |
插入样例数据:
插入样例数据:
curl -XPOST http://hadoop01:9200/sanguo/dahan/_bulk -d '
{ "index": {}}
{ "studentNo" : 1, "name" : "刘备", "male" : "男", "age" : 24 , "birthday" : "1985-02-03" , "classNo" : 1 , "address" : "湖南省长沙市" , "isLeader" : true}
{ "index": {}}
{ "studentNo" : 2, "name" : "关羽", "male" : "男", "age" : 22 , "birthday" : "1987-08-23" , "classNo" : 2, "address" : "四川省成都市" , "isLeader" : false}
{ "index": {}}
{ "studentNo" : 3, "name" : "糜夫人", "male" : "女", "age" : 19 , "birthday" : "1990-06-12" , "classNo" : 1 , "address" : "上海市" , "isLeader" : false}
{ "index": {}}
{ "studentNo" : 4, "name" : "张飞", "male" : "男", "age" : 20 , "birthday" : "1989-07-30" , "classNo" : 3 , "address" : "北京市" , "isLeader" : false}
{ "index": {}}
{ "studentNo" : 5, "name" : "诸葛亮", "male" : "男", "age" : 18 , "birthday" : "1992-04-27" , "classNo" : 2 , "address" : "江苏省南京市" , "isLeader" : true}
{ "index": {}}
{ "studentNo" : 6, "name" : "孙尚香", "male" : "女", "age" : 16 , "birthday" : "1994-05-21" , "classNo" : 3 , "address" : "广东省深圳市" , "isLeader" : false}
{ "index": {}}
{ "studentNo" : 7, "name" : "马超", "male" : "男", "age" : 19 , "birthday" : "1991-10-20" , "classNo" : 1 , "address" : "黑龙江省哈尔滨市" , "isLeader" : false}
{ "index": {}}
{ "studentNo" : 8, "name" : "赵云", "male" : "男", "age" : 23 , "birthday" : "1986-10-26" , "classNo" : 2 , "address" : "浙江省杭州市" , "isLeader" : false}
'
1:查询所有
matchAllQuery()匹配所有文件
match_all查询是Elasticsearch中最简单的查询之一。它使我们能够匹配索引中的所有文件。
/**
*matchAllQuery()匹配所有文件
match_all查询是Elasticsearch中最简单的查询之一。它使我们能够匹配索引中的所有文件
* */
@Test
public void searchAll(){
SearchResponse searchResponse = client.prepareSearch("sanguo")
.setTypes("dahan").setQuery(QueryBuilders.matchAllQuery())
.get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("===============================================");
}
}
2:解析查询字符串
相比其他可用的查询,query_string查询支持全部的Apache Lucene查询语法
针对多字段的query_string查询
/**
* 相比其他可用的查询,query_string查询支持全部的Apache Lucene查询语法
针对多字段的query_string查询
* */
@Test
public void query_String(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.queryStringQuery("孙尚香")).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("===============================================");
}
}
3:通配符查询(wildcardQuery)
*匹配多个字符,?匹配1个字符
注意:避免* 开始, 会检索大量内容造成效率缓慢
/**
* *匹配多个字符,?匹配1个字符
注意:避免* 开始, 会检索大量内容造成效率缓慢
* */
@Test
public void wildcardQuery(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.wildcardQuery("address", "广东*")).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("===============================================");
}
}
4:词条查询(termQuery)
词条查询是Elasticsearch中的一个简单查询。它仅匹配在给定字段中含有该词条的文档,而且是确切的、未经分析的词条
termQuery("key", obj) 完全匹配
termsQuery("key", obj1, obj2..)
一次匹配多个值,有一个值是正确的,就可以查询出数据
/**
* 词条查询是Elasticsearch中的一个简单查询。它仅匹配在给定字段中含有该词条的文档,而
且是确切的、未经分析的词条
termQuery("key", obj) 完全匹配
termsQuery("key", obj1, obj2..) 一次匹配多个值,只有有一个值是正确的,就可以查询出数据
* */
@Test
public void termQuery(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.termsQuery("name", "张飞","刘备","关羽")).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("===============================================");
}
}
5:字段匹配查询
match**查询把query参数中的值拿出来,加以分析,然后构建相应的查询**。使用match查询时,Elasticsearch将对一个字段选择合适的分析器,所以可以确定,传给match查询的词条将被建立索引时相同的分析器处理。
/**
* match query搜索的时候,首先会解析查询字符串,进行分词,然后查询,
而term query,输入的查询内容是什么,就会按照什么去查询,并不会解析查询内容,对它分词。
* */
@Test
public void MatchQuery(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.matchQuery("address", " 上海")).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("===============================================");
}
}
Matchquery 和termquery的区别:
match:匹配的时候,会将查询的关键字进行分词,然后根据分词后的结果进行查询。
term:直接使用关键字进行查询,不对关键字进行分词。
multiMatchQuery("text", "field1", "field2"..); 匹配多个字段
mutilMatchQuery:
/**
* multiMatchQuery(要搜索的值 , 在哪些字段上)
* */
@Test
public void mutileMatchQuery(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.multiMatchQuery("北京", "address","name")).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("===============================================");
}
}
6:只查询ID(标识符查询)
标识符查询是一个简单的查询,仅用提供的标识符来过滤返回的文档。
/**
* 按照id进行查询,通过id返回我们想要的结果
* */
@Test
public void idsQuery() {
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.idsQuery().addIds("AWNkQSCJzU0_wTuf7egi")).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("===============================================");
}
}
7:相似度查询
插入两条测试数据:
PUT /sanguo/dahan/123
{
"name": "曹操",
"male" : "男",
"age" : 40 ,
"birthday" : "1975-02-03" ,
"classNo" : 1 ,
"address" : "海淀区坐落于北京市" ,
"isLeader" : true
}
PUT /sanguo/dahan/234
{
"name": "董卓",
"male" : "男",
"age" : 35 ,
"birthday" : "1975-02-03" ,
"classNo" : 1 ,
"address" : "北京it程序员" ,
"isLeader" : true
}
fuzzy查询是模糊查询中的第三种类型,它基于编辑距离算法来匹配文档
/**
* 相似度查询:fuzzy查询是模糊查询中的第三种类型,它基于编辑距离算法来匹配文档
* */
@Test
public void fuzzyQuery(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.fuzzyQuery("address", "北京市")).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
System.out.println("查询结果有:" + hits.getTotalHits() + "条");
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("得分:"+next.getScore());
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("===============================================");
}
}
8:范围查询
范围查询使我们能够找到在某一字段值在某个范围里的文档,字段可以是数值型,也可以是
基于字符串的
includeLower(true):包含上界
IncludeUpper(true):包含下界
/**
范围查询使我们能够找到在某一字段值在某个范围里的文档,字段可以是数值型,也可以是
基于字符串的
includeLower(true):包含上界
IncludeUpper(true):包含下界
* */
@Test
public void rangeQuery(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.rangeQuery("age").from(18).to(22)
.includeLower(true)
.includeUpper(false))
.get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("得分:"+next.getScore());
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("age : "+ next.getSource().get("age"));
System.out.println("===============================================");
}
}
9:组合查询(复杂查询)
must(QueryBuilders) : AND
mustNot(QueryBuilders): NOT
should(QueryBuilders):OR

插入一条数据:
PUT /sanguo/dahan/345
{
"name": "貂蝉",
"male" : "女",
"age" : 16 ,
"birthday" : "2000-02-03" ,
"classNo" : 1 ,
"address" : "北京市女it程序员" ,
"isLeader" : true
}
/*
* 组合查询:
*
must(QueryBuilders) : AND
mustNot(QueryBuilders): NOT
should(QueryBuilders):OR
* */
@Test
public void boolQuery(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("address", "程序员"))
.must(QueryBuilders.termQuery("male", "女"))).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("得分:"+next.getScore());
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("studentNo : "+ next.getSource().get("studentNo"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("birthday : "+ next.getSource().get("birthday"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("age : "+ next.getSource().get("age"));
System.out.println("===============================================");
}
}
10:排序查询
/**
* ASC : 正序(从小到大)
* DESC: 倒序(从大到小)
* */
@Test
public void SortOrderQuery(){
SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.matchAllQuery())
.addSort("age", SortOrder.ASC).get();
SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
Iterator<SearchHit> iterator = hits.iterator();
while (iterator.hasNext()) {
SearchHit next = iterator.next();
System.out.println("name : "+ next.getSource().get("name"));
System.out.println("male : "+ next.getSource().get("male"));
System.out.println("classNo : "+ next.getSource().get("classNo"));
System.out.println("address : "+ next.getSource().get("address"));
System.out.println("age : "+ next.getSource().get("age"));
System.out.println("===============================================");
}
}
es之java各种查询操作的更多相关文章
- ES的java端API操作
首先简单介绍下写这篇博文的背景,最近负责的一个聚合型的新项目要大量使用ES的检索功能,之前对es的了解还只是纯理论最多加个基于postman的索引创建操作,所以这次我得了解在java端如何编码实现:网 ...
- es使用java的api操作
基本环境的创建 pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...
- es之java删除文档操作
删除文档操作 @Test public void deleteDocument(){ DeleteResponse response = client.prepareDelete("twit ...
- Java的JDBC操作
Java的JDBC操作 [TOC] 1.JDBC入门 1.1.什么是JDBC JDBC从物理结构上来说就是java语言访问数据库的一套接口集合,本质上是java语言根数据库之间的协议.JDBC提供一组 ...
- Java使用Jdbc操作MySql数据库(一)
这个示例是Java操作MySql的基本方法. 在这个示例之前,要安装好MySql,并且配置好账户密码,创建一个logininfo数据库,在数据库中创建userinfo数据表.并且在表中添加示例数据. ...
- MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 基于jsp+servlet图书管理系统之后台用户信息查询操作
上一篇的博客写的是插入操作,且附有源码和数据库,这篇博客写的是查询操作,附有从头至尾写的代码(详细的注释)和数据库! 此次查询操作的源码和数据库:http://download.csdn.net/de ...
- Statement执行DQL语句(查询操作)
import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import org.junit.T ...
- SQL语言(二) java怎样连接操作数据库中的数据
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.S ...
随机推荐
- 解析xml的4种方法详解(转)
http://blog.csdn.net/jzhf2012/article/details/8532873 1. 介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和 ...
- [JS] 鼠标点击文本框清空默认值,离开文本框恢复默认值
在使用文本框的时候,若设定了初始值,选择文本框进行输入的时候要将本来的内容进行删除,会显得非常麻烦 可以在文本框属性定义触发onfocus和onblur两个事件时对应的js功能 下面以asp.net代 ...
- 转 appium grid分布式环境搭建
https://blog.csdn.net/ljl6158999/article/details/80803239 说起grid,了解selenium的人肯定知道,他就是分布式的核心.原理是简历中心h ...
- php批量POST修改
这是一个thinkphp中的批量修改的案例: 如需要删除多项,或者同时修改多项记录 要点: 前端表单中name要加[],如:<input type="hidden" name ...
- TApplication,TForm,TControl,TComponent,TWinControl研究(博客索引)good
TApplication,TForm,TControl,TComponent,TWinControl研究 http://blog.csdn.net/suiyunonghen/article/detai ...
- 使用PHPWord生成word文档
有时我们需要把网页内容保存为Word文档格式,以供其他人员查看和编辑.PHPWord是一个用纯PHP编写的库,使用PHPWord可以轻松处理word文档内容,生成你想要的word文档. 下载源码 安装 ...
- vue创建项目配置脚手架vue-cli环境出错
1.at process._tickCallback (internal/process/next_tick.js:188:7) npm ERR! message: 'request to http ...
- Django扩展内置User类
内置User类 使用内置User可以方便实现登录验证,利用Admin管理界面还可以方便添加.删除.修改用户. 一个内置的User类定义了以下字段: username: 用户名 password: 密码 ...
- PAT Advanced 1005 Spell It Right (20 分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- IsDate(expression)函数
IsDate 函数 返回 Boolean 值指明某表达式是否可以转换为日期. IsDate(expression) expression 参数可以是任意可被识别为日期和时间的日期表达式或字符串表达式. ...