目录
记录elasticsearch日常在kibana平台使用中的常见基础业务类型dsl语句,包含索引管理(创建、删除、修改设置、查看索引状态)和数据管理(插入、删除、更新、查询)两大部分
1.索引管理
1.1 创建索引
#创建索引(并且指定分片数量)
PUT test_index
{
"aliases": {
"test_index_read": {},
"test_index_write": {}
},
"mappings": {
"properties": {
"id":{
"type": "integer"
},
"name":{
"type": "keyword"
},
"content":{
"type": "text"
}
}
},
"settings": {
"number_of_shards": 2,
"number_of_replicas": 0
}
}
2.2 删除索引
DELETE test_index
2.3 修改索引设置/映射
#修改副本数和刷新频率
PUT test_index/_settings
{
"number_of_replicas": 1,
"refresh_interval": "1s"
}
#在已经创建好的索引中新增mapping映射
POST test_index/_mappings
{
"properties": {
"address":{
"type": "keyword"
}
}
}
注:只能新增字段,不能删除字段,一旦设置type类型,不可更改。
2.4 查看索引情况
#查看某个索引
GET test_index
#查看指定索引文档总数
GET test_index/_count
#查看指定索引分片信息
GET _cat/shards/test_index?v
#查看所有索引状态并按占用内存大小倒序
GET _cat/indices?v&s=store.size:desc
#查看状态为黄的索引
GET _cat/indices?v&health=yellow
2.数据crud
2.1 向索引中插入数据
#方法1、手动设置ID (如果已经存在相同的ID,则执行更新操作)
# PUT 索引名称/_doc/id值
PUT test_index/_doc/1
{
"name":"zhangsan",
"address":"street"
}
#方法2、自动设置ID 使用POST提交
#POST 索引名称/_doc
POST test_index/_doc
{
"name":"zhangsan2",
"age":"street2"
}
2.2 删除索引中的数据
#可以根据id指定产出一条数据或多条数据
#删除索引数据 DELETE 索引名称/_doc/ID值
DELETE test_index/_doc/1
#删除多条数据 ID值用逗号隔开(,)
DELETE test_index/_doc/1,2,3,4
#也可以根据查询条件删除数据
POST test_index/_delete_by_query
{
"query":{
"match":{
"name":"zhangsan2"
}
}
}
#也可以根据条件删除某个字段的数据
POST test_index/_update_by_query
{
"script":{
"lang":"painless",
"source":"ctx._source.remove(\"address\")"
}
}
2.3 数据更新
#单个数据部分字段更新
POST test_index/_update/5
{
"doc": {
"address" : "street55"
}
}
#单个数据文档覆盖更新
POST test_index/_doc/3
{
"name" : "wangwu3"
}
#使用脚本来更新文档
POST test_index/_update/6
{
"script" : {
"source": "ctx._source.id += params.count",
"lang": "painless",
"params" : {
"count" : 3
}
}
}
POST test_index/_update_by_query
{
"script": {
"source": "ctx._source.id++",
"lang": "painless"
},
"query": {
"term": {
"address": "street"
}
}
}
2.4 查询DSL
简单查询
#查询所有数据
GET test_index/_search
#match 中设置相关的字段与数据
#match在匹配时会对所查找的关键词进行分词,然后按分词匹配查找
GET test_index/_search
{
"query": {
"match": {
"name": "wangwu3"
}
}
}
#term 会直接对关键词进行查找
GET test_index/_search
{
"query": {
"term": {
"name": "wangwu2"
}
}
}
#多个字段根据query中的内容进行匹配
#如下:
# 分别查询 name字段和address字段中包含 query查询中的内容 (内容作分词处理)
GET test_index/_search
{
"query": {
"multi_match": {
"query": "wangwu2",
"fields": ["name","address"]
}
}
}
# match_phrase 将数据分词后进行搜索的
# 1、目标文档需要包含分词后的所有词
# 2、目标文档还要保持这些词的相对顺序和文档中的一致
# 3、只有当这三个条件满足,才会命中文档
GET test_index/_search
{
"query": {
"match_phrase": {
"name": "zhang san"
}
}
}
#范围查询 range query
GET test_index/_search
{
"query": {
"range": {
"id": {
"gte": 1,
"lte": 10
}
}
}
}
#查询指定字段非空值的文档
GET test_index/_search
{
"query": {
"exists": {
"field": "id"
}
}
}
排序
#sort 排序
GET test_index/_search
{
"sort": [
{
"id": {
"order": "desc"
}
}
]
}
返回指定字段
GET test_index/_search
{
"_source": {
"includes": [
"name"
],
"excludes": [
"id"
]
}
}
分页查询
#from,起始条数
#size,总共多少条数据
GET test_index/_search
{
"from": 0,
"size": 10
}
高亮
GET test_index/_search
{
"query": {
"match": {
"name": "zhang"
}
},
"highlight": {
"fields": {
"name": {
"pre_tags": [
"<h1>"
],
"post_tags": [
"</h1>"
]
}
}
}
}
组合查询
bool query(组合查询)是把任意多个简单查询组合在一起,使用 must 、 should 、 must_not 、 filter 选项来表示简单查询之间的逻辑,每个选项都可以出现 0 次到多次。 它是为了满足现实中比较复杂的查询需求,如需要在多个字段上查询多种多样的文本,并且根据一系列的标准来过滤。
-
must: 必须满足子句(查询)必须出现在匹配的文档中,并将有助于得分。
-
filter: 过滤器不计算相关分数 子句(查询)必须出现在匹配的文档中。但是不像 must查询的分数将被忽略。Filter子句在filter上下文中执行,这意味着计分被忽略,并且子句被考虑用于缓存。
-
should: 可能满足 子句(查询)应出现在匹配的文档中。
-
must_not: 必须不满足 不计算相关度分数 子句(查询)不得出现在匹配的文档中。子句在过滤器上下文中执行,这意味着计分被忽略,并且子句被视为用于缓存。由于忽略计分,0因此将返回所有文档的分数。
-
minimum_should_match: 参数指定should返回的文档必须匹配的子句的数量或百分比。如果bool查询包含至少一个should子句,而没有must或 filter子句,则默认值为1。否则,默认值为0
例:
#首先筛选name包含“xiaomi phone”并且价格大于1999的数据(不排序),然后搜索name包含“xiaomi”and desc 包含“shouji”
GET test_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "xiaomi"
}
},
{
"match": {
"desc": "shouji"
}
}
],
"filter": [
{
"match_phrase":{
"name":"xiaomi phone"
}
},
{
"range": {
"price": {
"gte": 1999
}
}
}
]
}
}
}
#筛选name包含xiaomi 不包含erji 描述里包不包含nfc都可以,价钱要大于等于499
GET test_index/_search
{
"query": {
"bool":{
"must": [
{"match": { "name": "xiaomi"}}
],
"must_not": [
{"match": { "name": "erji"}}
],
"should": [
{"match": {
"desc": "nfc"
}}
],
"filter": [
{"range": {
"price": {
"gt": 4999
}
}}
]
}
}
}
3.参考文档
[Elasticsearch: 权威指南 | Elastic] Elasticsearch: 权威指南 | Elastic
[Query and filter context | Elasticsearch Guide [8.7] | Elastic] Query and filter context | Elasticsearch Guide [8.14] | Elastic