检索es信息
GET /_cat/nodes:查看所有节点
GET /_cat/health:查看es健康状况
GET /_cat/master:查看主节点
GET/_cat/indicies:查看所有索引
如:http://192.168.56.10:9200/_cat/nodes
新增文档
PUT customer/external/1
# # 在customer索引下的external类型下保存1号数据
PUT customer/external/1
# POSTMAN输入
http://192.168.56.10:9200/customer/external/1
{
"name":"John Doe"
}
查询文档
GET /customer/external/1
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 10,
"_seq_no": 18,//并发控制字段,每次更新都会+1,用来做乐观锁
"_primary_term": 6,//同上,主分片重新分配,如重启,就会变化
"found": true,
"_source": {
"name": "John Doe"
}
}
乐观锁用法:通过“
if_seq_no=1&if_primary_term=1
”,当序列号匹配的时候,才进行修改,否则不修改。
更新文档_update
POST customer/externel/1/_update
{
"doc":{
"name":"111"
}
}
或者
POST customer/externel/1
{
"doc":{
"name":"222"
}
}
或者
PUT customer/externel/1
{
"doc":{
"name":"222"
}
}
put 总是更新
post 会对比文档,不一致再更新
删除文档或索引
DELETE customer/external/1
DELETE customer
elasticsearch 没有提供删除类型/表的操作
ES的批量操作——bulk
POST /customer/external/_bulk
执行多条数据
两行为一个整体
{"index":{"_id":"1"}}
{"name":"a"}
{"index":{"_id":"2"}}
{"name":"b"}
注意格式json和text均不可,要去kibana里Dev Tools
对于整个索引执行批量操作
POST /_bulk
{"delete":{"_index":"website","_type":"blog","_id":"123"}}
{"create":{"_index":"website","_type":"blog","_id":"123"}}
{"title":"my first blog post"}
{"index":{"_index":"website","_type":"blog"}}
{"title":"my second blog post"}
{"update":{"_index":"website","_type":"blog","_id":"123"}}
{"doc":{"title":"my updated blog post"}}
search检索文档
ES支持两种基本方式检索;
- 通过REST request uri 发送搜索参数 (uri +检索参数);
- 通过REST request body 来发送它们(uri+请求体);
请求参数方式检索
GET bank/_search?q=*&sort=account_number:asc
说明:
q=* # 查询所有
sort # 排序字段
asc #升序
URI + 请求体
GET /bank/_search
{
"query": { "match_all": {} },
"sort": [ #排序
{ "account_number": "asc" },
{ "balance":"desc"}
],
"from":0, #分页
"size":5,
"_source": ["balance","age"] #返回字段
}
检索bank下所有信息,包括type和docs
GET bank/_search
返回内容:
- took – 花费多少ms搜索
- timed_out – 是否超时
- _shards – 多少分片被搜索了,以及多少成功/失败的搜索分片
- max_score –文档相关性最高得分
- hits.total.value - 多少匹配文档被找到
- hits.sort - 结果的排序key(列),没有的话按照score排序
- hits._score - 相关得分 (not applicable when using match_all)
query/match匹配查询
GET bank/_search
{
"query": {
"match": {
"account_number": "20"
}
}
}
全文检索按照评分排序,会对检索条件分词匹配
query/match_phrase
[不拆分匹配]
GET bank/_search
{
"query": {
"match_phrase": {
"address": "mill road" # 匹配mill road一整个子串
}
}
}
query/multi_math【多字段匹配】
GET bank/_search
{
"query": {
"multi_match": { # 前面的match仅指定了一个字段。
"query": "mill",
"fields": [ # state和address有mill子串 不要求都有
"state",
"address"
]
}
}
}
query/bool/must复合查询
- must:必须达到must所列举的所有条件
- must_not:必须不匹配must_not所列举的所有条件。
- should:应该满足should所列举的条件。满足条件最好,不满足也可以,满足得分更高
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": "18"
}
}
],
"should": [
{
"match": {
"lastname": "Wallace"
}
}
]
}
}
}
query/filter【结果过滤】
- must 贡献得分
- should 贡献得分
- must_not 不贡献得分
- filter 不贡献得分
GET bank/_search
{
"query": {
"bool": {
"must": [
{ "match": {"address": "mill" } }
],
"filter": { # query.bool.filter
"range": {
"balance": {
"gte": "10000",
"lte": "20000"
}
}
}
}
}
}
query/term 精确查找
不能使用term来进行文本字段查询
非文本精确查找使用 term
GET bank/_search
{
"query": {
"term": {
"age": "28"
}
}
}
文本精确查找使用
GET bank/_search
{
"query": {
"match": {
"address.keyword": "789 Madison Street"
}
}
}
GET bank/_search
{
"query": {
"match_phrase": {
"address": "mill Road"
}
}
}
aggs 聚合查询
# 分别为包含mill、,平均年龄、
GET bank/_search
{
"query": { # 查询出包含mill的
"match": {
"address": "Mill"
}
},
"aggs": { #基于查询聚合
"ageAgg": { # 聚合的名字,随便起
"terms": { # 看值的可能性分布
"field": "age",
"size": 10
}
},
"ageAvg": {
"avg": { # 看age值的平均
"field": "age"
}
},
"balanceAvg": {
"avg": { # 看balance的平均
"field": "balance"
}
}
},
"size": 0 # 不看详情
}
aggs子聚合
按照年龄聚合,并且求这些年龄段的这些人的平均薪资
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": { # 看分布
"field": "age",
"size": 100
},
"aggs": { # 与terms并列
"ageAvg": { #平均
"avg": {
"field": "balance"
}
}
}
}
},
"size": 0
}
复杂子聚合:查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": { # 看age分布
"field": "age",
"size": 100
},
"aggs": { # 子聚合
"genderAgg": {
"terms": { # 看gender分布
"field": "gender.keyword" # 注意这里,文本字段应该用.keyword
},
"aggs": { # 子聚合
"balanceAvg": {
"avg": { # 男性的平均
"field": "balance"
}
}
}
},
"ageBalanceAvg": {
"avg": { #age分布的平均(男女)
"field": "balance"
}
}
}
}
},
"size": 0
}
参考资料:blog.csdn.net/hancoder/article/details/113922398