集群健康检查
#查看集群状态GET /_cat/health?v# 返回结果epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent1566637870 02:11:10 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%#status#- green:每个索引的 primary shard 和 replica shard 都是 active 状态的#- yellow:每个索引的 primary shard 都是 active 状态的,但是部分 replica shard 不是 active 状态,处于不可用的状态#- red:不是所有索引的 primary shard 都是 active 状态的,部分索引有数据丢失了# unassign:未分配数量# active_shards_percent:可用 shards 百分比# 查看集群节点状态GET /_cat/nodes?vip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name127.0.0.1 4 90 20 0.94 0.72 0.30 mdi * b1uYKac#master列下带`*`的符号表示为主# 查看分片状态GET /_cat/shards?vindex shard prirep state docs store ip nodeperson 1 p STARTED 0 191b 127.0.0.1 b1uYKacperson 1 r UNASSIGNEDperson 2 p STARTED 1 3.7kb 127.0.0.1 b1uYKacperson 2 r UNASSIGNEDperson 0 p STARTED 0 191b 127.0.0.1 b1uYKacperson 0 r UNASSIGNED
es索引命令
# 查看当前es存在哪些索引GET /_cat/indices?v# 返回结果health status index uuid pri rep docs.count docs.deleted store.size pri.store.sizeyellow open .kibana ys5virQxRU6y8VRKuyymgA 1 1 1 0 3.2kb 3.2kb# 删除索引DELETE /${index}?pretty
常见查询
全文检索&短语搜索
match
是全文检索,因此如果查询条件如"name":"tinys kai"
,则会分词出tinys
,kai
来查询.
若想只查询短语tinys kai
,则可使用match_phrase
来查询.
# 查询语句GET /${index}/${type}/_search{"query": {"match_all": {}}}# 查询索引结构GET ${index}/_mapping# 分页查询GET /${index}/${type}/_search{"query": {"match_all": {}},"from": 0, //0表示第一条记录开始"size": 20 //查询20条}# 筛选返回字段GET /${index}/${type}/_search{"query": {"match_all": {}},"_source": ["${field1}","${field2}"] # source筛选字段可使用通配符`*`,如`name.*`}# 筛选返回字段GET /${index}/${type}/_search{"query": {"match_all": {}},"_source":{"include" : ["${field1}","${field2}"] ,"exclude" : ["${field1}","${field2}"] ,}}#查询并过滤后排序GET /${index}/${type}/_search{"query": {"match_all": {}},"sort":[{"${field}": "desc"}]}# 不分词的短语搜索,如下查询则只会查询"tinys kai"的匹配项,而不会单独去查询"tinys"或者"kai"GET /${index}/${type}/_search{"query": {"match_phrase": {"${field}" : "tinys kai"}}}# 多个字段同时查询GET /${index}/${type}/_search{"query": {"multi_match": {"query" : "${val}","fields" : ["${field_name1}","${field_name2}"] # 指定搜索的字段}}}
存在查询
# 查询某某字段存在值的记录GET /${index}/${type}/_search{"query": {"exists": {"field": "${feild_name}"}}}# 查询不存在某字段的记录GET /${index}/${type}/_search{"query": {"bool": {"must_not": {"exists": {"field": "${field_name}"}}}}}
单filter查询
单独使用filter查询需配合使用constant_score
(恒定分数,所有分数都是 1)来实现
GET /${index}/${type}/_search{"query": {"constant_score": {"filter": {"range": {"${field}": {"gte": ${val}}}}}}}
query&filter复合查询
# es新版本推荐使用这种复合查询,`query filtered方式已淘汰`# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.htmlGET /${index}/${type}/_search{"query": {"bool": {"must": {"match": {"${field}": "${val}"}},"filter": {"term": {"${field1}": "${val1}"}}}}}
不分词查询
# 搜索文本不分词查询GET /${index}/${type}/_search{"query": {"term": {"${field}": "${val}"}}}# 多词不分词搜索GET /_search{"query": {"terms": {"${filed}": [ "${val1}", "${val2}", "${val3}" ]# "minimum_should_match" : ${min_num} #最小匹配数,即最少匹配几个term才算符合}}}
聚合查询
//聚合函数语法GET /${index}/${type}/_search{"aggs": {"NAME": {"AGG_TYPE": {}}}}//- aggs:聚合函数,固定值//- NAME:给这个操作取一个名字//- AGG_TYPE:聚合类型//聚合查询GET /${index}/${type}/_search{"aggs": {"${agg_name}": {"terms": {"field": "${field}"}}},"size": 0 //加上返回的size为0可只返回聚合数据}//查询过滤后判重GET /${index}/${type}/_search{"query": {"match_all": {}},"aggs": {"${agg_name}": {"terms": {"field": "${field}"}}},"size": 0 //加上返回的size为0可只返回聚合数据}
更新语句
# 全部更新PUT /${index}/${type}/id?pretty{"${key}": ${value}}# 部分更新POST /${index}/${type}/id/_update{"doc" : {"${key1}" : ${val1},"${key2}" : ${val2}}}# 部分更新不存在则insertPOST /${index}/${type}/id/_update{"doc" : {"${key1}" : val1},# 文档id不存在时,upsert会触发来创建"upsert":{"${key1}" : val1,"${key2}" : val2,"${key3}" : val3}}# 删除索引下某个id的记录DELETE /${index}/${type}/id?pretty# 根据查询出来的值来删除POST /${index}/${type}/_delete_by_query?conflicts=proceed # 删除时有冲突也删除{"query": {"match": {"${field}": "${val}"}}}# 删除不存在某些字段的记录POST ${index}/_delete_by_query?conflicts=proceed # 删除时有冲突也删除{"query": {"bool": {"must_not": {"exists": {"field": "${field_name}"}}}}}
复合布尔查询
bool 中可以放:must,must_not,should,filter
# and查询例子GET /${index}/${type}/_search{"query": {"bool": {"must": [{ "match": { "name": "tinys" } },{ "match": { "age": 20 } }]}}}# or查询例子GET /${index}/${type}/_search{"query": {"bool": {"should": [{ "match": { "name": "tinys" } },{ "match": { "age": 210 } }]}}}# neither not都不符合的例子GET /${index}/${type}/_search{"query": {"bool": {"must_not": [{ "match": { "name": "tinys" } },{ "match": { "age": 21 } }]}}}# 多条件布尔查询: 筛选出名字为tinys但年龄不等于21的记录GET /${index}/${type}/_search{"query": {"bool": {"must": [{ "match": { "name": "tinys" } }],"must_not": [{ "match": { "age": 21 } }]}}}# 范围查询GET /${index}/${type}/_search{"query": {"bool": {"must": { "match_all": {} },"filter": {"range": {"age": {"gte": 20,"lte": 30}}}}}}
别名操作
# 添加别名POST /_aliases{"actions": [{"add": {"index": "${index}","alias": "${index}_aliases"}}]}# 删除别名POST /_aliases{"actions": [{"remove": {"index": "${index}","alias": "${index}_aliases"}}]}
Mapping映射
只能创建 index 时手动建立 mapping,或者新增 field mapping,但是不能 update field mapping
//查询某索引映射GET ${index}/_mapping//给新创建的索引创建映射PUT /${index}{"${index}": {"${type}":{"properties": {"${field}":{"type": "text", // 数据类型"index":"not_analyzed", // 索引类型"analyzer": "english" // 分词类型//"format" : "YYYY-MM-DD hh:mm:ss" //日期格式// "fielddata" : {// "loading" : "eager" --通过fielddata字段设置提前加载到缓存,此缓存常用于排序,聚合等// }}}}}}}//索引类型有如下值://- analyzed : 全文 full text,默认的选项//- not_analyzed : 精准匹配 exact value//- no :不索引//给已创建完的索引修改MappingPUT /${index}/${type}/_mapping{"properties" : {"${new_field}" : {"type" : "string","index": "not_analyzed"}}}
创建索引
PUT /${index}{"settings": {"number_of_shards": 1,"number_of_replicas": 0},"mappings": {"${type}": {"properties": {"${field}": {"type": "${field_type}"}}}}}
分词器
分词器需要在 mapping 中指定,而且一经指定就不能再修改,若要修改必须新建索引。
# 分析index索引下的fieldName属性的val值是怎么分词的POST ${index}/_analyze{"field": "${fieldName}","text": "${val}"}
路由
路由公式 : shard_num = hash(_routing) % num_primary_shards
.默认的路由键为_id
.
# 设置某索引必须带路由才能进行查询PUT ${index}{"mappings": {"_routing": {"required": true}}}# 只在val1和val2所在的分片上进行搜索GET ${index}/_search?routing=${val1},${val2}{"query": {"match": {"${field}": "${val}"}}}# 当你开始使用自定义路由进行赋值时,其对应的查询,更新,删除也需带上该路由值PUT ${index}/_doc/${id}?routing=${filed_val}&refresh=true{"${field}": "${val}"}# 对上面的记录根据路由查找GET ${index}/_doc/${id}?routing=${filed_val}# https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html
文章转载自小D学Java,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




