前言
elasticseach常用api汇总,持续更新。
系统
分词
使用statdard
分词器对"hi john"分词
GET _analyze{ "analyzer": "standard", "text": "hi john"}复制代码
查询集群健康信息
GET /_cluster/health复制代码
索引
查询index 信息
GET /test_order_detail/复制代码
创建索引,3个主分片,1个副本
PUT /blogs{ "settings": { "number_of_replicas": 1, "number_of_shards": 3 }}复制代码
新增/修改/删除数据
新增/修改数据,修改数据时,会将旧文档标价为删除,并新增一个全线的文档
POST /test_order_detail/_doc/23{ "column1": 2, "column21": "john com", "column22": "john com.cn"}复制代码
部分更新
POST /test_order_detail/_update/1{ "doc": { "column2" : "hi john" }}复制代码
通过Groovy脚本更新
刚开始不存会执行"upsert"语句将column2设为5,再次执行此语句11已存在会执行"script"
POST /test_order_detail/_update/11{ "script": "ctx._source.column2+=1", "upsert": { "column2": 5 }}复制代码
指定id和_create,保证只能执行一次,返回409状态码代表id已存在
PUT /test_order_detail/_doc/1/_create{ "column1": 1, "column2": 11}复制代码
乐观锁并发控制,返回409状态码更新失败
PUT /test_order_detail/_doc/1?if_seq_no=100005&if_primary_term=1{ "column1":2, "column2":22}复制代码
删除文档,返回404没有找到
DELETE /test_order_detail/_doc/1复制代码
查询
搜索,默认返回10条数据
GET /test_order_detail/_search复制代码
搜索column1=1
GET /test_order_detail/_search?q=column1:1复制代码
行数
GET /test_order_detail/_count复制代码
获取一行数据
GET /test_order_detail/_doc/1复制代码
优化json格式数据显示
GET /test_order_detail/_doc/1?pretty复制代码
返回部分字段
GET /test_order_detail/_doc/1?_source=column1复制代码
只返回列字段信息
GET /test_order_detail/_doc/1/_source复制代码
sort 排序
GET /test_order_detail/_search{ "query": { "bool": { "must": [ { "term": { "column20": "12_3_101_ijklmnopqrstuvwxyzABCDEFGH" } }, { "term": { "column23": 395110 } } ] } }, "sort": [ { "column40": { "order": "desc" } } ]}复制代码
检索多个文档
GET /_mget{ "docs": [ { "_index": "test_order_detail", "_id": 1 }, { "_index": "test_order_detail", "_id": 118 } ]}复制代码
查询id等于1或者119的
GET /test_order_detail/_mget{ "ids": [1, 119]}复制代码
高亮显示
- match_phrase短语搜索
- match 模糊搜索
- term 完全匹配
- highlight 高亮显示
GET /test_order_detail/_search{ "query": { "match": { "column22": "hi john" } }, "highlight": { "fields": { "column22": {} } }}复制代码
匹配到返回结果会加上<em>
标签
"highlight" : { "column22" : [ "<em>hi john</em>" ] }复制代码
聚合查询
histogram按间隔汇总
column1每间隔10000进行汇总
GET /test_order_detail/_search{ "aggs": { "group_by_column2":{ "histogram": { "field": "column1", "interval": 10000 }, "aggs": { "amount": { "sum": { "field": "column30" } } } } }}复制代码
date_histogram日期汇总
*1.按日期字段column40分钟汇总,sum字段column30*2.再按column2汇总
GET /test_order_detail/_search{ "aggs": { "column2":{ "terms": { "field": "column2" }, "aggs": { "column40":{ "date_histogram": { "field": "column40", "interval": "minute", "format": "yyyy-MM-dd HH:mm", "min_doc_count": 0 }, "aggs": { "amount": { "sum": { "field": "column30" } }, "avg_column29": { "avg": { "field": "column29" } } } } } } }}复制代码
min_doc_count填充不存在的日期bucket
- extended_bounds限定填充日期区间
- size = 0只返回聚合结果
- 查询条件,column2>=1
- gt >,gte >=,lt >,lte <=
GET /test_order_detail/_search{"size": 5,"query": {"range": {"column2": {"gte": 1}}},"aggs": {"column40":{"date_histogram": {"field": "column40","interval": "minute","format": "yyyy-MM-dd HH:mm","min_doc_count": 0,"extended_bounds": {"min": "2020-08-15 13:45","max": "2020-08-15 13:59"}},"aggs": {"amount": {"sum": {"field": "column30"}}}}}}
排序
- _key 按column2值数值排序(在terms、histogram、data_histogram使用)
- _count 按文档数排序(在histogram、data_histogram使用)
- _term 按column2字符串字母顺序排序(在terms使用)
GET /test_order_detail/_search{ "aggs": { "column2" : { "terms": { "field": "column2", "size": 10, "order": { "_key": "asc", "_count": "desc" } } } }}复制代码
对聚合结果进行排序
- 按column2聚合,按column30平均值正序排序
GET /test_order_detail/_search{ "aggs": { "column2" : { "terms": { "field": "column2", "size": 10, "order": { "avg_column30": "asc" } }, "aggs": { "avg_column30": { "avg": { "field": "column30" } } } } }}复制代码
对多个度量值进行排序
- stats.variance:按方差顺序排序
- stats.sum:按总和顺序排序
- stats.sum_of_squares:平方差
- stats.min:最小值
- stats.max:最大值
- stats.count:数量
- stats.avg:平均值
- stats.std_deviation:标准偏差
GET /test_order_detail/_search{ "aggs": { "column2" : { "terms": { "field": "column2", "size": 10, "order": { "stats.variance": "asc", "stats.sum": "desc" } }, "aggs": { "stats": { "extended_stats": { "field": "column30" } } } } }}复制代码
前言
elasticseach常用api汇总,持续更新。
系统
分词
使用statdard
分词器对"hi john"分词
GET _analyze
{
"analyzer": "standard",
"text": "hi john"
}
复制代码
查询集群健康信息
GET /_cluster/health
复制代码
索引
查询index 信息
GET /test_order_detail/
复制代码
创建索引,3个主分片,1个副本
PUT /blogs
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 3
}
}
复制代码
新增/修改/删除数据
新增/修改数据,修改数据时,会将旧文档标价为删除,并新增一个全线的文档
POST /test_order_detail/_doc/23
{
"column1": 2,
"column21": "john com",
"column22": "john com.cn"
}
复制代码
部分更新
POST /test_order_detail/_update/1
{
"doc": {
"column2" : "hi john"
}
}
复制代码
通过Groovy脚本更新
刚开始不存会执行"upsert"语句将column2设为5,再次执行此语句11已存在会执行"script"
POST /test_order_detail/_update/11
{
"script": "ctx._source.column2+=1",
"upsert": {
"column2": 5
}
}
复制代码
指定id和_create,保证只能执行一次,返回409状态码代表id已存在
PUT /test_order_detail/_doc/1/_create
{
"column1": 1,
"column2": 11
}
复制代码
乐观锁并发控制,返回409状态码更新失败
PUT /test_order_detail/_doc/1?if_seq_no=100005&if_primary_term=1
{
"column1":2,
"column2":22
}
复制代码
删除文档,返回404没有找到
DELETE /test_order_detail/_doc/1
复制代码
查询
搜索,默认返回10条数据
GET /test_order_detail/_search
复制代码
搜索column1=1
GET /test_order_detail/_search?q=column1:1
复制代码
行数
GET /test_order_detail/_count
复制代码
获取一行数据
GET /test_order_detail/_doc/1
复制代码
优化json格式数据显示
GET /test_order_detail/_doc/1?pretty
复制代码
返回部分字段
GET /test_order_detail/_doc/1?_source=column1
复制代码
只返回列字段信息
GET /test_order_detail/_doc/1/_source
复制代码
sort 排序
GET /test_order_detail/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"column20": "12_3_101_ijklmnopqrstuvwxyzABCDEFGH"
}
},
{
"term": {
"column23": 395110
}
}
]
}
},
"sort": [
{
"column40": {
"order": "desc"
}
}
]
}
复制代码
检索多个文档
GET /_mget
{
"docs": [
{
"_index": "test_order_detail",
"_id": 1
},
{
"_index": "test_order_detail",
"_id": 118
}
]
}
复制代码
查询id等于1或者119的
GET /test_order_detail/_mget
{
"ids": [1, 119]
}
复制代码
高亮显示
- match_phrase短语搜索
- match 模糊搜索
- term 完全匹配
- highlight 高亮显示
GET /test_order_detail/_search
{
"query": {
"match": {
"column22": "hi john"
}
},
"highlight": {
"fields": {
"column22": {}
}
}
}
复制代码
匹配到返回结果会加上<em>
标签
"highlight" : {
"column22" : [
"<em>hi john</em>"
]
}
复制代码
聚合查询
histogram按间隔汇总
column1每间隔10000进行汇总
GET /test_order_detail/_search
{
"aggs": {
"group_by_column2":{
"histogram": {
"field": "column1",
"interval": 10000
},
"aggs": {
"amount": {
"sum": {
"field": "column30"
}
}
}
}
}
}
复制代码
date_histogram日期汇总
*1.按日期字段column40分钟汇总,sum字段column30*2.再按column2汇总
GET /test_order_detail/_search
{
"aggs": {
"column2":{
"terms": {
"field": "column2"
},
"aggs": {
"column40":{
"date_histogram": {
"field": "column40",
"interval": "minute",
"format": "yyyy-MM-dd HH:mm",
"min_doc_count": 0
},
"aggs": {
"amount": {
"sum": {
"field": "column30"
}
},
"avg_column29": {
"avg": {
"field": "column29"
}
}
}
}
}
}
}
}
复制代码
min_doc_count填充不存在的日期bucket
- extended_bounds限定填充日期区间
- size = 0只返回聚合结果
- 查询条件,column2>=1
- gt >,gte >=,lt >,lte <=
GET /test_order_detail/_search{ "size": 5, "query": { "range": { "column2": { "gte": 1 } } }, "aggs": { "column40":{ "date_histogram": { "field": "column40", "interval": "minute", "format": "yyyy-MM-dd HH:mm", "min_doc_count": 0, "extended_bounds": { "min": "2020-08-15 13:45", "max": "2020-08-15 13:59" } }, "aggs": { "amount": { "sum": { "field": "column30" } } } } } }
排序
- _key 按column2值数值排序(在terms、histogram、data_histogram使用)
- _count 按文档数排序(在histogram、data_histogram使用)
- _term 按column2字符串字母顺序排序(在terms使用)
GET /test_order_detail/_search
{
"aggs": {
"column2" : {
"terms": {
"field": "column2",
"size": 10,
"order": {
"_key": "asc",
"_count": "desc"
}
}
}
}
}
复制代码
对聚合结果进行排序
- 按column2聚合,按column30平均值正序排序
GET /test_order_detail/_search
{
"aggs": {
"column2" : {
"terms": {
"field": "column2",
"size": 10,
"order": {
"avg_column30": "asc"
}
},
"aggs": {
"avg_column30": {
"avg": {
"field": "column30"
}
}
}
}
}
}
复制代码
对多个度量值进行排序
- stats.variance:按方差顺序排序
- stats.sum:按总和顺序排序
- stats.sum_of_squares:平方差
- stats.min:最小值
- stats.max:最大值
- stats.count:数量
- stats.avg:平均值
- stats.std_deviation:标准偏差
GET /test_order_detail/_search
{
"aggs": {
"column2" : {
"terms": {
"field": "column2",
"size": 10,
"order": {
"stats.variance": "asc",
"stats.sum": "desc"
}
},
"aggs": {
"stats": {
"extended_stats": {
"field": "column30"
}
}
}
}
}
}