查询集群状态
GET localhost:9200/_cat/health?v
基于status来判断集群集群状态 green、yellow、red
green:每个索引的primary shard 和replica shard 都是active 状态的
yellow: 每个索引的primary shard 是active状态的,但部分replica shard不是active状态的。(当只有一个Node, replica shard 没有第二台机器进行分配)
red: 不是所有primary shard 都是active状态,说明部分数据丢失了
查询索引信息
curl --location 'localhost:9200/_cat/indices?v=null'
当返回为:health status index uuid pri rep docs.count docs.deleted store.size pri.store.size。 说明当前没有创建索引
创建索引
PUT localhost:9200/product?pretty
正常返回:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "product"
}
查询索引Field结构
curl --location --request GET 'localhost:9200/product/_mapping'
新增数据
curl --location --request PUT 'localhost:9200/product/_doc/1' \
--header 'Content-Type: application/json' \
--data-raw '{ "name" : "可乐"}'
{
"_index": "product",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2, // 总共有2个shard
"successful": 1, // 但只写成功一个,因为只有一个Node
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查询数据
curl --location --request GET 'localhost:9200/product/_doc/1'
{
"_index": "product",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"name": "可乐"
}
}
修改数据(替换)
curl --location --request PUT 'localhost:9200/product/_doc/1' \
--header 'Content-Type: application/json' \
--data-raw '{ "name" : "可乐11"}'
修改时必须带上所有Field数据,才能修改。
修改数据 (只修改传入数据)
curl --location --request POST 'localhost:9200/product/_update/1/' \
--header 'Content-Type: application/json' \
--data-raw '{"doc":{ "name" : "可乐22"}}'
删除数据
curl --location --request DELETE 'localhost:9200/product/_doc/1'
ES Query DSL (ES 特定的查询语法)
查询所有
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query":{
"match_all":{}
}
}'
hits.total : 检索总数
hits.max_score: 对于一个search的相关度匹配分数,越匹配,分数越高
hits.hits : 包括document的详细数据
按条件查询
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query":{
"match":{
"name": "kele"
}
}
}'
基于query中的单词,进行and 或or查询
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match" : {
"name" : {
"query" : "banana kele",
"operator" : "or"
}
}
}
}'
查询所有并排序
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query":{
"match_all":{
}
},
"sort":[{"sort":"asc"}]
}'
分页查询数据
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query":{
"match_all":{
}
},
"from":1,
"size":2
}'
返回指定Field的结果
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query":{
"match_all":{
}
},
"_source" : ["name"]
}'
精准匹配
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_phrase" : {
"name" : {
"query" : "banana",
"analyzer" : "standard"
}
}
}
}'
前缀匹配
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query" : {
"match_phrase_prefix" : {
"name" : "b"
}
}
}'
多字段匹配
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query" : {
"multi_match": {
"query": "apple",
"fields": ["name", "ename"]
}
}
}'
查询指定字段的倒排索引包含某个确切值的记录
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"term" : { "ename" : "apple" }
}
}'
与term类似。相当于SQL IN
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"terms" : { "name" : ["banana", "kele"] }
}
}'
terms 的 filter 版本
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"constant_score": {
"filter": { "terms" : { "name" : ["banana", "kele"] }}
}
}
}'
查询字段不为Null的数据
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"exists" : { "field" : "ename" }
}
}'
前缀匹配查询
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"prefix" : { "ename" : "e" }
}
}'
通配符查询
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"wildcard" : { "ename" : "*l*" }
}
}'
正则查询
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"regexp" : { "ename" : "a.*" }
}
}'
范围查询
gt: > 大于(greater than)
lt: < 小于(less than)
gte: >= 大于或等于(greater than or equal to)
lte: <= 小于或等于(less than or equal to)
boost 参数可用于调整相关性评分(score)的值
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"range" : {
"sort" : {
"gte" : 1,
"lte" : 2,
"boost" : 2.0
}
}
}
}'
Query Filter (查询过滤) Filter不会计算score分值,所以效率会更高一些
curl --location --request GET 'localhost:9200/product/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query":{
"bool":{
"must":{
"match":{
"name":"kele",
"id":1
}
},
"filter":{
"range":{
"sort":{
"gt":0
}
}
}
}
}
}'
总结:
must 必须匹配。
should 至少匹配一个文档。
filter 必须匹配,忽略相关性评分。
must_not 必须不匹配,忽略相关性评分。
说明: must 和 should 在查询上下文中执行;must_not 和 filter 在过滤上下文中执行。
聚合(aggregations)
参考:https://www.jianshu.com/p/aa221be93cc7
index 复制
curl --location 'localhost:9200/_reindex/' \
--header 'Content-Type: application/json' \
--data '{
"source": {
"index": "source"
},
"dest": {
"index": "dest",
"op_type": "create"
}
}
'
index数据清空
curl --location 'localhost:9200/index/_delete_by_query' \
--header 'Content-Type: application/json' \
--data '{
"query": {
"match_all": {}
}
}'