ES7和低版本的差异:
ES6及低版本支持_type属性,和数据库的对应关系可以理解为_index对应库、_type对应表,而ES7及以上版本不再支持_type属性,在ES7中_index对应表,在ES7中创建索引时无需指定_type、ES底层固定为_type="_doc"。
Elasticsearch 7 的官方文档参考:
https://www.elastic.co/guide/en/elasticsearch/reference/7.10/rest-apis.html
#删除索引
DELETE http://192.168.100.250:9200/index_fk_test01
#删除文档(数据)
DELETE http://192.168.100.250:9200/index_fk_test01/_doc/1
删除不存在的文档时会返回 not_found
#带条件删除文档(数据)
一般是根据文档ID(_id)进行删除,如果需要根据文档内容字段进行删除,则可以用带过滤条件删除文档的方式。条件删除时,需要使用POST而不是DELETE。
POST http://192.168.100.250:9200/index_fk_test01/_delete_by_query
{
"query":{
"match":{
"info":"uu"
}
}
}
#创建索引规则(仅创建索引、字段和类型,不写入数据)
ES7官方文档--Mapping 介绍参考:https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping.html
PUT http://192.168.100.250:9200/index_fk_test01
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"id": { "type": "integer" },
"info": { "type": "text" },
"cnt": { "type": "integer" },
"update": { "type": "date" }
}
}
}
#添加数据、索引不存在时会自动创建索引并写入数据(指定_id,如果_id已存在,多次修改时_version递增)
POST http://192.168.100.250:9200/index_fk_test03/_doc/1
{
"id":"1",
"info":"sss",
"cnt":"95",
"update":"2024-10-21 19:43:30"
}
#创建/修改索引数据
PUT http://192.168.100.250:9200/index_fk_test03/_doc/1
{
"id":"1",
"info":"sss",
"cnt":"95",
"update":"2024-10-21 19:43:30"
}
#添加数据、索引不存在时会自动创建索引并写入数据(不指定_id,每次随机生成_id)
POST http://192.168.100.250:9200/index_fk_test03/_doc
{
"id":"1",
"info":"sss",
"cnt":"95",
"update":"2024-10-21 19:43:30"
}
#查询索引规则及其他信息
GET http://192.168.100.250:9200/index_fk_test03
#查看配置
GET http://192.168.100.250:9200/_all/_settings
#查看所有别名(aliase)
GET http://192.168.100.250:9200/_cat/aliases
#查询索引数据(所有数据/文档)
GET http://192.168.100.250:9200/index_fk_test01/_search
或
POST http://192.168.100.250:9200/index_fk_test01/_search
#分页查询索引数据(所有数据/文档)
GET http://192.168.100.250:9200/index_fk_test01/_search
或
POST http://192.168.100.250:9200/index_fk_test01/_search
注意:请GET请求带Body参数,可能会遇到Body参数无效的情况、抓包分析没有带上参数,postman没有这个问题。遇到类似问题,需要带Body参数时,改用POST方法。
根据"id"升序排序、分页的查询第一页10条数据,查询条件:
{
"query": {
"match_all": {}
},
"from": 0,
"size": 10,
"sort": {
"id": {"order": "asc"}
}
}
根据"id"升序排序、分页的查询第一页10条数据、只查询/返回"id"和"info"两个字段,查询条件:
{
"query": {
"match_all": {}
},
"from": 0,
"size": 10,
"_source":["id","info"],
"sort": {
"id": {"order": "asc"}
}
}
#查询索引数据(指定文档id、即_id,ES7虽然不支持_type、但底层创建索引_type固定为"_doc")
GET http://192.168.100.250:9200/index_fk_test01/_doc/1
#查询索引数据(带过滤查询、带筛选条件)
GET http://192.168.100.250:9200/index_fk_test01/_doc/_search
GET http://192.168.100.250:9200/index_fk_test01/_search
{
"query":{
"match":{
"info":"uu"
}
}
}
针对这两条数据:
{
"cnt": 12,
"update": "2024-10-21T13:40:41+0800",
"id": 3,
"info": "uu"
}
{
"cnt": 500,
"update": "2024-10-21T13:40:41+0800",
"id": 7,
"info": "uuu: hello, test!"
}
进行过滤查询:
GET http://192.168.100.250:9200/index_fk_test01/_search
{
"query":{
"bool": {
"must": [
{"match": {"info":"uu"}}
],
"should": [
{"term": {"cnt": 12}},
{"term": {"cnt": 500}}
],
"minimum_should_match": 1
}
}
}
过滤查询满足条件: info必须包含"uu"、且cnt值为12或者500的数据。
ES的条件查询查询:
match: 查询匹配。全文搜索匹配。
match_phrase: 匹配精确值查询。精确匹配短语。
term: 终端查询。精确匹配值。
range: 范围查询。查找指定范围的数值或时间。
bool: 布尔查询。可用于合并多个查询,布尔查询支持 must、must_not、should、filter。
#其他更多查询
ES7官方文档--Query DSL 介绍参考: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/query-dsl.html
ES7官方文档--Search your data 介绍参考: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/search-your-data.html
nested: 嵌套查询,查询检索嵌套JSON对象中的字段。
针对数据:
{
"id": 12,
"name":"Jack",
"info":{
"code":"330100",
"city":"hangzhou",
"phone":"13711112222"
}
}
嵌套查询:
{
"query": {
"nested": {
"path": "xzqh",
"query": {
"bool": {
"must": [
{ "match": { "info.code": "330100" } },
{ "match": { "info.phone": "13711112222" } }
]
}
}
}
}
}
multi_match: 查询允许在多个字段中搜索相同的关键词:
{
"query": {
"multi_match": {
"query": "value",
"fields": ["field1", "field2"]
}
}
}
wildcard: 查询允许使用通配符进行模式匹配。
查询将匹配 field1 中以 uu 开头的所有值:
{
"query": {
"wildcard": {
"field1": "uu*"
}
}
}
prefix: 查询用于匹配以特定前缀开头的字段值。
fuzzy: 查询用于? ヅ洌市硪欢ǔ潭鹊钠葱创砦螅檠浦怠?
regexp: 查询允许使用正则表达式进行模式匹配。
{
"query": {
"regexp": {
"field1": "uu.*"
}
}
}
match_phrase: 查询用于匹配包含特定短语的文档。
ids: 查询用于根据文档 ID 匹配文档(_id)。
{
"query": {
"ids": {
"values": ["1", "2", "3"]
}
}
}
parent_id: 查询用于查找具有特定父文档 ID 的子文档。
has_child: 查询用于查找具有特定子文档的父文档。
has_parent: 查询用于查找具有特定父文档的子文档。
wrapper: 查询用于包装一个 Base64 编码的查询。
exists: 查询用于匹配包含特定字段的文档。
{
"query": {
"exists": {
"field": "field1"
}
}
}
type: 查询用于匹配特定类型的文档。
{
"query": {
"type": {
"value": "type1"
}
}
}
wildcard: 查询用于匹配包含通配符的字段值。
{
"query": {
"wildcard": {
"field1": "uu*"
}
}
}
#创建/修改索引数据【修改索引的旧方式、不推荐,_doc方式为全量更新或创建】
PUT http://192.168.100.250:9200/index_fk_test03/_doc/1
{
"id":"1",
"info":"abc",
"cnt":"100",
"update":"2024-10-21 20:43:30"
}
#修改索引数据(只修改一部分字段、即增量更新)【修改索引的新方式、推荐方式,_update方式为增量更新】
POST http://192.168.100.250:9200/index_fk_test03/_update/1
或者
POST http://192.168.100.250:9200/index_fk_test03/_doc/1/_update
{
"doc":{
"info":"yyy"
}
}
#批量获取
GET http://192.168.100.250:9200/_mget
{
"docs": [
{"_index":"index_fk_test01", "_id":1},
{"_index":"index_fk_test01", "_id":2},
{"_index":"index_fk_test01", "_id":3}
]
}
#批量操作
POST _bulk
注意:
1)_bulk提交的的payload数据,每个操作json必须一行,多个操作json之间必须换行。
2)_bulk的每个操作必须要一对JSON串(delete语法除外),即每个操作的下一行需要紧跟其payload的JSON串。
3)_bulk的整个完整的payload内容最后需要有一个换行、即最后一条JSON的末尾必须要有一个换行。
4)_bulk批量操作方式,如果不分写入失败,会继续完成后续的数据插入。
5)_bulk批量操作的操作类型支持如下:
create: 如果文档不存在就创建,但如果文档存在就返回错误
index: 如果文档不存在就创建,如果文档存在就更新
update: 更新一个文档,如果文档不存在就返回错误
delete: 删除一个文档,如果要删除的文档id不存在,就返回错误
POST http://192.168.100.250:9200/_bulk
{"create":{"_index": "index_fk_test05","_id":1}}
{"id":1,"info":"aaa","cnt":95,"updatetime":"2024-10-22 09:50:01"}
{"index":{"_index":"index_fk_test05","_id":1}}
{"id":1,"info":"aaa","cnt":98,"updatetime":"2024-10-22 09:52:02"}
{"update":{"_index": "index_fk_test05","_id":1}}
{"id":1,"info":"aaa","cnt":99,"updatetime":"2024-10-22 09:52:03"}
{"create":{"_index": "index_fk_test06","_id":1}}
{"id":1,"info":"xxx","cnt":200,"updatetime":"2024-10-22 09:52:10"}
{"delete":{"_index": "index_fk_test06","_id":1}}
每条执行结果都会返回。
利用_bulk一次性批量插入多套数据:
POST http://192.168.100.250:9200/_bulk
{"index":{"_index": "index_fk_test07","_id":1}}
{"id":1,"info":"aaa","cnt":95,"updatetime":"2024-10-22 09:50:01"}
{"index":{"_index": "index_fk_test07","_id":2}}
{"id":2,"info":"bbb","cnt":80,"updatetime":"2024-10-22 09:51:01"}
{"index":{"_index": "index_fk_test07","_id":3}}
{"id":3,"info":"ccc","cnt":83,"updatetime":"2024-10-22 09:53:01"}
{"index":{"_index": "index_fk_test07","_id":4}}
{"id":4,"info":"ddd","cnt":84,"updatetime":"2024-10-22 09:54:01"}
{"index":{"_index": "index_fk_test07","_id":5}}
{"id":5,"info":"sss005","cnt":85,"updatetime":"2024-10-22 09:55:01"}
{"index":{"_index": "index_fk_test07","_id":6}}
{"id":6,"info":"sss006","cnt":86,"updatetime":"2024-10-22 09:56:01"}
{"index":{"_index": "index_fk_test07","_id":7}}
{"id":7,"info":"sss007","cnt":87,"updatetime":"2024-10-22 09:57:01"}
{"index":{"_index": "index_fk_test07","_id":8}}
{"id":8,"info":"sss008","cnt":88,"updatetime":"2024-10-22 09:58:01"}
{"index":{"_index": "index_fk_test07","_id":9}}
{"id":9,"info":"sss008","cnt":90,"updatetime":"2024-10-22 09:59:01"}
{"index":{"_index": "index_fk_test07","_id":10}}
{"id":10,"info":"sss008","cnt":92,"updatetime":"2024-10-22 10:21:01"}
{"index":{"_index": "index_fk_test07","_id":11}}
{"id":11,"info":"ssabc","cnt":92,"updatetime":"2024-10-22 10:22:01"}
{"index":{"_index": "index_fk_test07","_id":12}}
{"id":12,"info":"ssabc12","cnt":200,"updatetime":"2024-10-22 10:23:01"}
{"index":{"_index": "index_fk_test07","_id":13}}
{"id":13,"info":"ssabc13","cnt":200,"updatetime":"2024-10-22 10:24:01"}
索引使用通配符查询
索引支持*通配符,匹配前缀相同的所有index。例如下面这种查询方式,能查询出index_fk_test01、index_fk_test02、index_fk_test03等相关的index的所有数据:
GET http://192.168.100.250:9200/index_fk_test*/_search
滚动查询(scroll):
请求返回一个单一的结果页,scroll API 可以被用来检索大量的结果(甚至所有的结果),类似于传统数据库中使用的游标 cursor。滚动并不是为了实时的用户响应,而是为了处理大量的数据。
从 scroll 请求返回的结果反映了 search 发生时刻的索引状态,类似一个快照。后续的改动(索引、更新或者删除)都只会影响后面的搜索请求。
scroll 用法是在初始搜索请求应该在查询中指定 scroll 请求参数,指定一个上下文环境的保留时长,例如:scroll=1m、scroll=1d
GET http://192.168.100.250:9200/index_fk_test0*/_search?scroll=1d
{
"query": {
"match_all": {}
}
}
会返回_scroll_id,后续再根据最新的_scroll_id查询即可,每次都会返回一个最新的_scroll_id。
时间戳过滤/时间戳增量查询:
查询昨天至今天的数据:
GET http://192.168.100.250:9200/index_fk_test01/_search
{
"query": {
"bool": {
"filter": {
"range": {
"update": {
"gte": "now-1d/d",
"lt": "now/d"
}
}
}
}
}
}
只查询昨天的数据(不包括今天):
GET http://192.168.100.250:9200/index_fk_test01/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"update": "now-1d/d"
}
}
]
}
}
}