ElasticSearch 使用

ElasticSearch 使用

_cat

# 查看所有节点
http://192.168.188.128:9200/_cat/nodes

# 查看 es 健康状况
http://192.168.188.128:9200/_cat/health

    # 查看主节点
http://192.168.188.128:9200/_cat/master

# 查看所有索引
http://192.168.188.128:9200/_cat/indices

索引

PUT customer/external/1;在 customer 索引下的 external 类型下保存 1 号数据为

# put 保存数据
http://192.168.188.128:9200/customer/external/1  

{
    //元数据
    "_index": "customer",   
    
    //类型
    "_type": "external",    
    
    //id
    "_id": "1",             
    
    //版本信息
    "_version": 1,          
    
    //create:新建的数据 updated:更新数据
    "result": "updated",    
    
    //分片
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    
    "_seq_no": 2,
    "_primary_term": 5
}

# post保存数据
http://192.168.188.128:9200/customer/external/
  • post和put保存区别
POST 新增:
  如果不指定 id,会自动生成 id。
  指定 id 就会修改这个数据,并新增版本号
  
PUT 可以新增可以修改。
  PUT 必须指定 id;
  由于 PUT 需要指定id,我们一般都用来做修改操作,不指定 id 会报错。

查询

# 获取信息
http://192.168.188.128:9200/customer/external/1
{
    //索引
    "_index": "customer",
    
    //类型
    "_type": "external",
    
    //记录 id
    "_id": "1",
    
    //版本号
    "_version": 2,
    
    //并发控制字段,每次更新就会+1,用来做乐观锁
    "_seq_no": 2,
    
    //同上,主分片重新分配,如重启,就会变化
    "_primary_term": 5,
    
    "found": true,
    
    //保存的信息
    "_source": {
        "name": "Jack Deon"
    }
}

更新

# POST更新
//进行更新前,会比较新数据和老数据的区别;若无改变则不进行更新 version、_seq_no不发生改变
http://192.168.188.128:9200/customer/external/1/_update
请求的JSON信息:
{
    "doc":{
        "name": "John"
    }
}

# POST更新
//进行更新前,不会比较新、老数据。
//若想比较 则使用doc括起来
http://192.168.188.128:9200/customer/external/1
请求的JSON信息:
{
    "name": "John Doe2"
}

# PUT更新
//PUT更新(不带_update) 都会直接更新数据
http://192.168.188.128:9200/customer/external/1
请求的JSON信息:
{
    "name": "John Doe2"
}

# 更新同时增加属性
## POST更新同时增加属性
http://192.168.188.128:9200/customer/external/1/_update
请求的JSON信息:
{
    "doc": { 
    "name": "Jane Doe",
    "age": 20
    }
}

## PUT更新同时增加属性
http://192.168.188.128:9200/customer/external/1
请求的JSON信息:
{
    "name": "Jane Doe",
    "age": 20
}

PUT 和 POST 不带_update 也可以
  • POST更新和PUT更新区别
不同:POST 操作会对比源文档数据,如果相同不会有什么操作,文档version不增加PUT操作总会将数据重新保存并增加 version 版本;

 带_update 对比元数据如果一样就不进行任何操作。看场景;

 对于大并发更新,不带 update;
 对于大并发查询偶尔更新,带 update;对比更新,重新计算分配规则。

删除

# 删除一条数据
http://192.168.188.128:9200/customer/external/1

# 删除索引
http://192.168.188.128:9200/customer

bulk 批量 API

使用kibana

# kibana dev tools执行

POST customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"John Doe"}
{"index":{"_id":"2"}}
{"name":"Jane Doe"}

# kibana delete|create|update操作
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"}}

# 样本测试数据
https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json

# POST请求
POST /bank/account/_bulk
请求的JSON信息:
{
    从上面网址获取JSON串
}
原文地址:https://www.cnblogs.com/HOsystem/p/14508964.html