查询[ES]
查询ES信息
代码语言:javascript复制GET /
查询集群健康状态
代码语言:javascript复制GET /_cluster/health
增删改索引
创建索引并指定主分片和副本数
代码语言:javascript复制PUT /my_doc
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
创建索引并指定映射
代码语言:javascript复制PUT /index_mappings
{
"mappings": {
"properties": {
# 字段名称
"realname":{
# 字段类型
"type": "text",
# 是否被索引, 默认为true
"index": true
},
"username":{
"type": "keyword",
"index": false
}
}
}
}
修改索引Mappings
不支持
增加索引Mappings字段
代码语言:javascript复制POST /index_mappings/_mapping
{
"properties": {
"id": {
"type": "long"
},
"age": {
"type": "integer"
}
}
}
自动设置Mappings
新增index
代码语言:javascript复制PUT /my_doc
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
新增索引记录
代码语言:javascript复制# 可以指定ID, 也可以不指定
POST /my_doc/_doc/
{
"id": 1001,
"name": "imooc-1",
"desc": "imoocisverygood,慕课网非常牛!",
"create_date": "2019-12-24"
}
新增记录有如果index没有mapping就会自动映射
修改索引记录
代码语言:javascript复制# 增量修改
POST /my_doc/_update/Fxu1NYMBTxHwTTCGbOwO
{
"doc":{
"name":"我是慕课网2"
}
}
# 全量修改
PUT /my_doc/_doc/Fxu1NYMBTxHwTTCGbOwO
{
"name":"我是delete"
}
基于乐观锁修改记录
代码语言:javascript复制# 查询数据
GET /my_doc/_doc/zBu3NYMBTxHwTTCGLewF
# 返回结果
{
"_index" : "my_doc",
"_type" : "_doc",
"_id" : "zBu3NYMBTxHwTTCGLewF",
"_version" : 1,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"id" : 1003,
"name" : "imooc-3",
"desc" : "imooc is niubility, 慕课网很好很强大!",
"create_date" : "2019-12-26"
}
}
# 基于版本修改 正确的版本
POST /my_doc/_update/zBu3NYMBTxHwTTCGLewF?if_seq_no=2&if_primary_term=1
{
"doc":{
"name":"imooc-31"
}
}
# 返回结果 修改成功 版本升级
{
"_index" : "my_doc",
"_type" : "_doc",
"_id" : "zBu3NYMBTxHwTTCGLewF",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 13,
"_primary_term" : 2
}
# 基于版本修改 错误的版本
POST /my_doc/_update/zBu3NYMBTxHwTTCGLewF?if_seq_no=2&if_primary_term=2
{
"doc":{
"name":"imooc-31"
}
}
# 返回结果 修改失败 版本不一致
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[zBu3NYMBTxHwTTCGLewF]: version conflict, required seqNo [2], primary term [2]. current document has seqNo [13] and primary term [2]",
"index_uuid": "OQtxdWrTQRCVsOZFKLrPqA",
"shard": "0",
"index": "my_doc"
}
],
"type": "version_conflict_engine_exception",
"reason": "[zBu3NYMBTxHwTTCGLewF]: version conflict, required seqNo [2], primary term [2]. current document has seqNo [13] and primary term [2]",
"index_uuid": "OQtxdWrTQRCVsOZFKLrPqA",
"shard": "0",
"index": "my_doc"
},
"status": 409
}
删除索引记录
代码语言:javascript复制# 根据_id删除记录, 只是逻辑删除, 后续ES会自动清理
DELETE /my_doc/_doc/{_id}
删除索引
代码语言:javascript复制DELETE /索引名称
字段类型汇总
查询索引
索引管理里面查看
查询索引信息
代码语言:javascript复制GET /索引名称
查询索引列表
代码语言:javascript复制# 不携带列头
GET /_cat/indices
# 携带列头
GET /_cat/indices?v
索引字段分词
代码语言:javascript复制GET /index_mappings/_analyze
{
"field": "realname",
"text": ["imooc is very good!"]
}
在增加索引时, 字段index
为TRUE时才会被分词
根据ID查询记录
代码语言:javascript复制# 根据_id查询记录
GET /my_doc/_doc/{_id}
查询全部记录
代码语言:javascript复制GET /my_doc/_search
查询记录是否存在
代码语言:javascript复制HEAD /my_doc/_doc/{_id}