ElasticSearch之HTTP REST请求API操作总结
REST API
API文档参考地址:https://www.elastic.co/guide/en/elasticsearch/reference/7.4/index.html
节点信息查询
1.查看所有节点
Get http://IP:9200/_cat/node
127.0.0.1 33 95 4 0.08 0.08 0.12 dilm * 777714f4567a
2.查看es健康状况
GET http://IP:9200/_cat/health
1662553864 12:31:04 elasticsearch green 1 1 3 3 0 0 0 0 - 100.0%
3.查看主节点
GET http://IP:9200/_cat/master
_oveJKAYQqKCFg1hsbz-qw 127.0.0.1 127.0.0.1 777714f4567a
4.查看所有索引
GET http://IP:9200/_cat/indices
green open .kibana_task_manager_1 H0cxvynZQzC77QnysVj_LQ 1 0 2 0 30.5kb 30.5kb
green open .apm-agent-configuration u5zvDAiERfmZgEKvD9BhKw 1 0 0 0 283b 283b
green open .kibana_1 fHaH9NEBROCiPX_vF9CvHg 1 0 8 0 28.6kb 28.6kb
映射操作
索引库(index)中的映射,类似于数据库(database)中的表结构(table)。创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)。
Mapping 是用来定义一个文档document,以及它所包含的属性field是如何存储和索引的。
ES是基于Lucene 开发的搜索引擎,而ES中不同type下名称相同的filed 最终在Lucene中的处理方式是一样的。因此:不同type 中的相同字段名称就会在处理中出现冲突的情况,导致Lucene处理效率下降。去掉type就是为了提高ES处理数据的效率。
ES7中请求参数的type 参数为可选,ES8将不再支持。
常用类型
Elasticsearch中支持的数据类型非常丰富
核心类型 | 具体类型 |
---|---|
字符串(string) | text:可分词
keyword:不可分词,数据会作为完整字段进行匹配 |
数字类型(Numerical) | 基本数据类型:long、integer、short、byte、double、float、half_float
浮点数的高精度类型:scaled_float |
日期类型(Date) | date |
布尔类型(Boolean) | boolean |
二进制类型(binary) | binary |
复杂类型 | 具体类型 |
---|---|
数组类型(Aray) | Array支持不针对特定的类型 |
对象类型(Object) | object用于单JSON对象 |
嵌套类型(Nested) | nested用于JSON对象数组 |
地理类型 | 具体类型 |
---|---|
地理坐标(Geo-points) | geo_point用于描述经纬度坐标 |
地理图形(Geo-Shape) | geo_shape用于描述复杂形状,如多边形 |
特定类型 | 具体类型 |
---|---|
IP类型 | ip用于描述ipv4和ipv6地址 |
补全类型(Completion) | completion提供自动完成提示 |
令牌计数类型(Token count) | token count用于统计字符串中的词条数量 |
附件类型(attachment) | 参考mapper-attachements插件,支持将附件如Microsoft Office格式,Open Documentt格式,ePub,HTML等索引为attachment数据类型 |
抽取类型(Percolator) | 接受特定领域查询语言(quey-dsl)的查询 |
创建映射
index:是否索引,默认为true,所有字段都会被索引
true:字段会被索引,则可以用来进行搜索
false:字段不会被索引,不能用来搜索
store:是否将数据进行独立存储,默认为false
原始的文本会存储在_source里面,默认情况下其他提取出来的字段都不是独立存储的,是从_source里面提取出来的。当然也可以独立的存储某个字段,只要设置”store”: true即可,获取独立存储的字段要比从_source中解析快得多,但是也会占用更多的空间,所以要根据实际业务需求来设置。
http://127.0.0.1:9200/user
Put http://127.0.0.1:9200/user/_mapping
{
"properties": {
"username": {
"type": "text",
"index": true
},
"sex": {
"type": "keyword",
"index": true
},
"age": {
"type": "long",
"index": false
}
}
}
查看映射
Get http://127.0.0.1:9200/user/_mapping
{
"user": {
"mappings": {
"properties": {
"age": {
"type": "long",
"index": false
},
"sex": {
"type": "keyword"
},
"username": {
"type": "text"
}
}
}
}
}
添加新字段映射
PUT http://127.0.0.1:9200/user/_mapping
{
"properties": {
"email": {
"type": "keyword",
"index": false
}
}
}
索引直接映射关联
Put http://127.0.0.1:9200/user1
{
"settings": {},
"mappings": {
"properties": {
"username": {
"type": "text",
"index": true
},
"sex": {
"type": "keyword",
"index": true
},
"age": {
"type": "long",
"index": false
}
}
}
}
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "user1"
}
更新映射
对于已经存在的映射字段,不能更新。更新必须创建新的索引进行数据迁移
创建索引
PUT http://127.0.0.1:9200/new_user
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "new_user"
}
创建新索引的正确映射
PUT http://127.0.0.1:9200/new_user/_mapping
{
"properties": {
"username": {
"type": "text",
"index": true
},
"sex": {
"type": "keyword",
"index": true
},
"age": {
"type": "integer",
"index": false
}
}
}
将旧索引的type下的数据进行迁移
POST http://127.0.0.1:9200/_reindex
{
"source": {
"index": "user"
},
"dest": {
"index": "new_user"
}
}
索引操作
创建索引
向ES服务器发PUT请求
http://127.0.0.1:9200/demo
响应:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "demo"
}
查看单个索引
向ES服务器发GET请求
http://127.0.0.1:9200/demo
响应:
{
"demo": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1632061699362",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "PMskybxnT6WHoA2hWHFETw",
"version": {
"created": "7080099"
},
"provided_name": "demo"
}
}
}
}
查看所有索引
向ES服务器发GET请求
http://127.0.0.1:9200/_cat/indices?v
响应:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open demo PMskybxnT6WHoA2hWHFETw 1 1 0 0 208b 208b
表头 | 含义 |
---|---|
health | 当前服务器健康状态: green: 集群完整 yellow: 单点正常、集群不完整 red: 单点不正常 |
status | 索引打开、关闭状态 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主分片数量 |
rep | 副本数量 |
docs.count | 可用文档数量 |
docs.deleted | 文档删除状态(逻辑删除) |
store.size | 主分片和副分片整体占空间大小 |
pri.store.size | 主分片占空间大小 |
删除索引
向ES服务器发DELETE请求
http://127.0.0.1:9200/demo
{
"acknowledged": true
}
文档操作
创建文档
发POST请求
http://127.0.0.1:9200/demo/_doc
请求数据:
{
"username":"小白白",
"age":22,
"address":"中国上海"
}
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
由于没有指定数据唯一性标识(ID),默认情况下,ES服务器会随机生成一个。
自定义唯一性标识,需要在创建时指定
http://127.0.0.1:9200/demo/_doc/1
http://127.0.0.1:9200/demo/_create/2
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
注意:增加数据,明确数据主键,可以使用POST与PUT请求
查询文档
发GET请求:
http://127.0.0.1:9200/demo/_doc/1
响应:
{
"_index": "demo", // 那个索引
"_type": "_doc", // 那个类型
"_id": "1", // 数据ID
"_version": 1, // 版本号
"_seq_no": 1, // 并发控制字段,每次更新就会+1,用来做乐观锁
"_primary_term": 1, // 同上,主分片重新分配,如重启,就会变化
"found": true, // 是否找到
"_source": { // 文档内容
"username": "小白2",
"age": 22,
"address": "中国上海2"
}
}
修改文档
发POST、PUT请求 :
http://127.0.0.1:9200/demo/_doc/1
请求参数:
{
"username":"修改文档",
"age":22,
"address":"中国上海2"
}
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
POST操作会对比源文档数据,如果相同不会有什么操作,文档version 不增加
PUT操作总会将数据重新保存并增加version版本
带_update对比元数据如果一样就不进行任何操作
若大并发更新,不带update
若大并发查询偶尔更新,带update,对比更新,重新计算分配规则
修改字段
修改数据时只修改数据的局部信息,更新同时可以增加属性。PUT和POST不带_update也可以
发POST、PUT请求 :
http://127.0.0.1:9200/demo/_doc/1/_update
请求数据:
{
"doc": {
"username":"update field"
}
}
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 2
}
并发控制修改
并发控制修改只需要在发起修改请求时添加if_seq_no=1&if_primary_term=1
2个参数即可
http://127.0.0.1:9200/demo/_doc/1?if_seq_no=1&if_primary_term=1
删除文档
删除文档不会立即从磁盘上移除,只是被标记成已删除(逻辑删除)
根据文档的唯一性标识进行删除
根据条件对多条数据进行删除
发DELETE请求 :
http://127.0.0.1:9200/demo/_doc/4
响应:
{
"_index": "demo",
"_type": "_doc",
"_id": "4",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 2
}
发送POST请求:
http://127.0.0.1:9200/demo/_delete_by_query
请求数据:
{
"query": {
"match": {
"username": "update field"
}
}
}
响应:
{
"took": 81,
"timed_out": false,
"total": 1,
"deleted": 1,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
查询所有
发送Get请求:
http://127.0.0.1:9200/demo/_search
响应:
{
"took": 632,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1,
"_source": {
"username": "小白白",
"age": 22,
"address": "中国上海"
}
},
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"username": "update field",
"age": 22,
"address": "中国上海2"
}
}
]
}
}
批量操作
bulk API以顺序执行所有的action动作。如果一个单个的动作因任何原因而失败,它将继续处理它后面剩余的动作。当bulk API 返回时,它将提供每个动作的状态(与发送的顺序相同),所以可以检查一个指定的动作是不是失败了。
语法格式
{ action: { metadata }}
{ request body }
{ action: { metadata }}
{ request body }
批量创建
POST请求
POST http://127.0.0.1:9200/demo/_doc/_bulk
请求数据
{"index":{"_id":"1"}}
{"username": "Jack","age":"20" }
{"index":{"_id":"2"}}
{"username": "Jane","age":"25"}
响应结果
{
"took" : 7,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "demo",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "demo",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 7,
"_primary_term" : 1,
"status" : 201
}
}
]
}
POST请求
POST http://127.0.0.1:9200/_bulk
请求数据
{ "create": { "_index": "demo", "_type": "_doc", "_id": "1" }}
{"age" : "20"}
{ "create": { "_index": "demo", "_type": "_doc", "_id": "2" }}
{"age" : "10"}
批量修改
POST请求
POST http://127.0.0.1:9200/demo/_doc/_bulk
请求数据
{ "update": { "_id": "1"} }
{ "doc" : {"age" : "50"} }
{ "update": { "_id": "2"} }
{ "doc" : {"age" : "60"} }
响应结果
{
"took" : 5,
"errors" : false,
"items" : [
{
"update" : {
"_index" : "demo",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 10,
"_primary_term" : 1,
"status" : 200
}
},
{
"update" : {
"_index" : "demo",
"_type" : "_doc",
"_id" : "2",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 11,
"_primary_term" : 1,
"status" : 200
}
}
]
}
POST请求
POST http://127.0.0.1:9200/_bulk
请求数据
{ "update": { "_index": "demo", "_type": "_doc", "_id": "1"} }
{ "doc" : {"age" : "30"} }
{ "update": { "_index": "demo", "_type": "_doc", "_id": "2"} }
{ "doc" : {"age" : "40"} }
响应结果
{
"took" : 10,
"errors" : false,
"items" : [
{
"update" : {
"_index" : "demo",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 1,
"status" : 200
}
},
{
"update" : {
"_index" : "demo",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 9,
"_primary_term" : 1,
"status" : 200
}
}
]
}
批量删除
请求接口
POST http://127.0.0.1:9200/_bulk
请求参数
{ "delete": { "_index": "demo", "_type": "_doc", "_id": "1" }}
{ "delete": { "_index": "demo", "_type": "_doc", "_id": "2" }}
响应结果
{
"took" : 5,
"errors" : false,
"items" : [
{
"delete" : {
"_index" : "demo",
"_type" : "_doc",
"_id" : "1",
"_version" : 4,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 12,
"_primary_term" : 1,
"status" : 200
}
},
{
"delete" : {
"_index" : "demo",
"_type" : "_doc",
"_id" : "2",
"_version" : 4,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 13,
"_primary_term" : 1,
"status" : 200
}
}
]
}
高级查询
查询所有
query代表一个查询对象,里面可以有不同的查询属性
Get http://127.0.0.1:9200/demo/_search
请求参数:
{
"query": { // 定义如何查询
"match_all": {} // 查询类型,代表查询所有的所有;可以在query 中组合非常多的查询类型完成复杂查询
},
// 传递其它查询参数,如sort,size
"from":0,
"size":2,// from+size限定,完成分页功能
"sort":{} // sort 排序,多字段排序,会在前序字段相等时后续字段内部排序,否则以前序为准
}
字段匹配查询
Get http://127.0.0.1:9200/demo/_search?q=age:22
响应:
{
"took": 40, // Elasticsearch 执行搜索的时间(毫秒)
"timed_out": false, // 搜索是否超时
"_shards": { // 多少个分片被搜索,以及统计成功/失败的搜索分片
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": { // 搜索结果
"total": { // 搜索结果总数
"value": 3,
"relation": "eq"
},
"max_score": 1, // 相关性得分和最高得分
"hits": [ // 实际的搜索结果数组(默认为前10的文档)
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1,
"_source": {
"username": "小白白",
"age": 22,
"address": "中国上海"
}
},
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"username": "update field",
"age": 22,
"address": "中国上海2"
}
}
]
}
}
Get http://127.0.0.1:9200/demo/_search
请求参数:
{
"query": {
"match": {
"age": 22
}
}
}
响应:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1,
"_source": {
"username": "小白白",
"age": 22,
"address": "中国上海"
}
},
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"username": "update field",
"age": 22,
"address": "中国上海2"
}
}
]
}
}
multi_match与match类似,不同的是它可以在多个字段中查询
{
"query": {
"multi_match": {
"query": "2",
"fields": [
"username",
"address"
]
}
}
}
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.6682933,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "2",
"_score": 0.6682933,
"_source": {
"username": "小白2",
"age": 22,
"address": "中国上海2"
}
},
{
"_index": "demo",
"_type": "_doc",
"_id": "9",
"_score": 0.6682933,
"_source": {
"username": "小白2",
"age": 25,
"address": "中国上海2"
}
}
]
}
}
短语匹配查询
将需要匹配的值当成一个整体单词(不分词)进行检索
GET demo/_search
{
"query": {
"match_phrase": {
"username": "小白 你好"
}
}
}
精确匹配查询
term查询,精确的关键词匹配查询,不对查询条件进行分词。匹配某个属性的值。全文检索字段用match,其他非text 字段匹配用term
{
"query": {
"term": {
"username": {
"value": "小白2"
}
}
}
}
多关键字精确查询
terms 查询和 term 查询一样,允许指定多值进行匹配。
如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于mysql的in
{
"query": {
"terms": {
"username": [
"小白白",
"小白"
]
}
}
}
查询指定字段
默认情况下,ES在搜索的结果中,会把文档中保存在_source的所有字段都返回。如果只想获取其中的部分字段,可以添加_source的过滤
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"query": {
"match_all": {
},
},
"_source":['username'] // 返回部分字段
}
响应:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1,
"_source": {
"username": "小白白"
}
},
{
"_index": "demo",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"username": "小白2"
}
}
]
}
}
字段过滤
includes:来指定想要显示的字段
excludes:来指定不想要显示的字段
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"_source": {
"includes": ["age","username"]
},
"query": {
"match_all": {
},
}
}
分页查询
from:当前页的起始索引,默认从0开始。 from = (pageNum - 1) * size
size:每页显示多少条
Get http://127.0.0.1:9200/demo/_search
请求参数:
{
"query": {
"match_all": {
},
},
"from":0,
"size":2
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1,
"_source": {
"username": "小白白",
"age": 22,
"address": "中国上海"
}
},
{
"_index": "demo",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"username": "update field",
"age": 22,
"address": "中国上海2"
}
}
]
}
}
单字段排序
通过order指定排序的方式。desc降序,asc升序。
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"query": {
"match_all": {
},
},
"from":0,
"size":2,
"_source":['username'],
"sort":{
"age":{
"order":"desc"
}
}
}
响应:
{
"took": 469,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "9",
"_score": null,
"_source": {
"username": "小白2"
},
"sort": [
25
]
},
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": null,
"_source": {
"username": "小白白"
},
"sort": [
22
]
}
]
}
}
多字段排序
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"query": {
"match_all": {
},
},
"from":0,
"size":2,
"_source":['username'],
"sort":[
{
"age":{
"order":"desc"
}
},
{
"_score":{
"order": "desc"
}
}
]
}
响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "9",
"_score": 1,
"_source": {
"username": "小白2"
},
"sort": [
25,
1
]
},
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1,
"_source": {
"username": "小白白"
},
"sort": [
22,
1
]
}
]
}
}
模糊查询
返回包含与搜索字词相似的字词的文档。
编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括:
更改字符(box → fox)
删除字符(black → lack)
插入字符(sic → sick)
转置两个相邻字符(act → cat)
为了找到相似的术语,fuzzy查询会在指定的编辑距离内创建一组搜索词的所有可能的变体或扩展。然后查询返回每个扩展的完全匹配。
通过fuzziness修改编辑距离。一般使用默认值AUTO,根据术语的长度生成编辑距离。
Get http://127.0.0.1:9200/demo/_search
{
"query": {
"fuzzy": {
"username": {
"value": "白",
"fuzziness": 2
}
}
}
}
组合查询
bool用来做复合查询,复合语句可以合并任何其它查询语句,包括复合语句。意味复合语句之间可以互相嵌套,可以表达非常复杂的逻辑。
bool
把各种其它查询通过
must(必须 )、
must_not(必须不)、
should`(应该)的方式进行组合
必须达到must列举的所有条件
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"query": {
"bool": {
"must": [
{
"match": {
"age": 22
}
},
{
"match": {
"username": "小白"
}
}
]
}
}
}
响应数据:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.8220872,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1.8220872,
"_source": {
"username": "小白白",
"age": 22,
"address": "中国上海"
}
},
{
"_index": "demo",
"_type": "_doc",
"_id": "2",
"_score": 1.6877716,
"_source": {
"username": "小白2",
"age": 22,
"address": "中国上海2"
}
}
]
}
}
应该达到should列举的条件
{
"query": {
"bool": {
"should": [
{
"match": {
"age": 22
}
},
{
"match": {
"username": "123"
}
}
]
}
}
}
结果过滤查询
并不是所有的查询都需要产生分数,为了不计算分数Elasticsearch 会自动检查场景并且优化查询的执行。
{
"query": {
"bool": {
"must": [
{"match": { "address": "shanghai"}}
],
"filter": {
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
}
}
}
范围查询
range查询找出那些落在指定区间内的数字或者时间,支持一下字符
操作符 | 说明 | 符号 |
---|---|---|
gt | 大于 | > |
gte | 大于等于 | >= |
lt | 小于 | < |
lte | 小于等于 | <= |
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"query": {
"bool": {
"should": [
{
"match": {
"age": 22
}
},
{
"match": {
"username": "123"
}
}
],
"filter":{
"range":{
"age":{
"gt":23
}
}
}
}
}
}
响应:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "9",
"_score": 0,
"_source": {
"username": "小白2",
"age": 25,
"address": "中国上海2"
}
}
]
}
}
完全匹配查询
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"query":{
"match_phrase":{
"username":"小白白"
}
}
}
响应:
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0316575,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1.0316575,
"_source": {
"username": "小白白",
"age": 22,
"address": "中国上海"
}
}
]
}
}
高亮查询
Get http://127.0.0.1:9200/demo/_search
{
"query": {
"match_phrase": {
"username": "小白白"
}
},
"highlight": {
"pre_tags": "<font color='red'>",
"post_tags": "</font>",
"fields": {
"username": {}
}
}
}
响应:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0316575,
"hits": [
{
"_index": "demo",
"_type": "_doc",
"_id": "wwKD_nsBW1VSDLfjekOE",
"_score": 1.0316575,
"_source": {
"username": "小白白",
"age": 22,
"address": "中国上海"
},
"highlight": {
"username": [
"<font color='red'>小</font><font color='red'>白</font><font color='red'>白</font>"
]
}
}
]
}
}
聚合查询
聚合提供了从数据中分组和提取数据的能力。聚合方法大致等于SQL GROUPBY 和SQL 聚合函数。
在Elasticsearch中,执行搜索返回hits(命中结果),并且同时返回聚合结果,把一个响应中的所有hits(命中结果)分隔开的能力。
可以执行查询和多个聚合,并且在一次使用中得到各自的(任何一个的)返回结果,使用一次简洁和简化的API 来避免网络往返。
Get http://127.0.0.1:9200/demo/_search
对某个字段取最大值max
{
"aggs":{
"max_age":{
"max":{"field":"age"}
}
},
"size":0
}
对某个字段取最大值min
{
"aggs":{
"min_age":{
"min":{"field":"age"}
}
},
"size":0
}
对某个字段求和sum
{
"aggs":{
"sum_age":{
"sum":{"field":"age"}
}
},
"size":0
}
对某个字段取平均值avg
{
"aggs":{
"avg_age":{
"avg":{"field":"age"}
}
},
"size":0
}
对某个字段的值进行去重之后再取总数
{
"aggs":{
"distinct_age":{
"cardinality":{"field":"age"}
}
},
"size":0
}
桶聚合查询
桶聚和相当于sql中的group by语句,terms聚合,分组统计
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"aggs": {
"group_age": {
"terms": {
"field": "age"
}
}
},
"size": 0
}
响应:
{
"took": 165,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"group_age": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 22,
"doc_count": 2
},
{
"key": 25,
"doc_count": 1
}
]
}
}
}
Stats聚合
stats聚合,对某个字段一次性返回count,max,min,avg和sum五个指标
Get http://127.0.0.1:9200/demo/_search
请求数据:
{
"aggs": {
"stats_age": {
"stats": {
"field": "age"
}
}
},
"size": 0
}
响应:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"stats_age": {
"count": 3,
"min": 22,
"max": 25,
"avg": 23,
"sum": 69
}
}
}
样本数据集测试
导入官方提供的文档accounts.json样本数据集。使用这些样本数据进行测试。
下载地址:https://github.com/elastic/elasticsearch/blob/7.4/docs/src/test/resources/accounts.json
向请求地址提交accounts.json
文件中的数据
POST http://127.0.0.1:9200/demo/_doc/_bulk
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137020.html