Elasticsearch API实现增删改查

版权声明:嘟嘟嘟,转发记得提一下 https://blog.csdn.net/ticktak/article/details/82346018

使用kibana对elasticsearch进行操作

  1. 打开elasticsearch和kibana
  2. 访问http://192.168.25.129:5601 (使用安装kibana的ip)
  3. 使用Dev Tools(如下)
    这里写图片描述

使用elasticsearch API实现CRUD

  1. 添加索引

    
    #指定配置创建索引lib
    
    
    #lib为索引名称
    
    
    #"number_of_shards":3  分片数量为3
    
    
    #"number_of_replicas":0 备份数量为0
    
    PUT /lib/  
    {
        "settings":{
            "index":{
                    "number_of_shards":3,      
                    "number_of_replicas":0      
            }
        }
    }
    
    
    #使用默认配置创建索引lib2
    
    PUT lib2
  2. 查询索引配置

    
    #查询lib的设置
    
    GET /lib/_settings
    
    
    #结果如下
    
    {
      "lib": {
        "settings": {
          "index": {
            "creation_date":     "1534492852386",
                "number_of_shards": "3",
                "number_of_replicas": "0",
            "uuid":     "XvO3MPUdQ-SMu-o    VZCGAWw",
            "version": {
              "created":   "6030299"
            },
            "provided_name":     "lib"
          }
        }
      }
    }
    
    
    #查询lib2设置
    
    GET /lib2/_settings
    
    
    #结果如下,可以看出,默认分片为5个,备份1个
    
    {
      "lib2": {
        "settings": {
          "index": {
            "creation_date":     "1534492911101",
                "number_of_shards": "5",
                "number_of_replicas": "1",
            "uuid":     "8DzDTWeeSHujmvb    RGAHg_g",
            "version": {
              "created":   "6030299"
            },
            "provided_name":     "lib2"
          }
        }
      }
    }
    
    
    #查询所有索引的配置
    
    GET _all/_settings
    
  3. 添加文档

    
    #指定添加文档id为1
    
    
    #type为user,1是文档编号
    
    PUT /lib/user/1      
    {
        "first_name":"Jane",
        "last_name":"Smith",
        "age":32,
        "about":"I like to collect rock albums",
        "interests":["music"]
    }
    
    
    #结果
    
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
      },
      "_seq_no": 0,
      "_primary_term": 1
    }
    
    
    #不指定id添加文档
    
    POST /lib/user/
    {
        "first_name":"Douglas",
        "last_name":"Fir",
        "age":23,
        "about":"I like to build cabinets",
        "interests":["forestry"]
    }
    
    
    #结果如下,id由elasticsearch生成
    
    {
      "_index": "lib",
      "_type": "user",
      "_id":   "mJP7RmUBx8NiZecJ2McD"  ,
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
      },
      "_seq_no": 0,
      "_primary_term": 1
    }
  4. 文档的查询

    
    #查询id为1的文档
    
    GET /lib/user/1
    
    
    #结果
    
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "first_name":"Jane",
        "last_name":"Smith",
        "age": 32,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    }
    
    
    #查询不指定id添加的文档
    
    GET /lib/user/mJP7RmUBx8NiZecJ2McD
    
    
    #结果
    
    {
      "_index": "lib",
      "_type": "user",
      "_id":   "mJP7RmUBx8NiZecJ2McD"  ,
      "_version": 1,
      "found": true,
      "_source": {
        "first_name":"Douglas",
        "last_name": "Fir",
        "age": 23,
        "about": "I like to build cabinets",
        "interests": [
          "forestry"
        ]
      }
    }
    
    
    #指定查询的字段
    
    GET /lib/user/1?_source=age,about
    
    //结果
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "about": "I like to collect rock albums",
        "age": 32
      }
    }
  5. 更新文档

    
    #直接覆盖原文档
    
    PUT /lib/user/1 
    {
        "first_name":"Jane",
        "last_name":"Smith",
        "age":36,
        "about":"I like to collect rock albums",
        "interests":["music"]
    }
    
    
    #修改指定字段(注意使用的是POST,推荐使用该方式)
    
    
    POST /lib/user/1/_update
    {
        "doc":{
            "age":30
        }
    }
  6. 删除文档

    
    #删除id为1的文档
    
    DELETE /lib/user/1
    
    //结果
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 4,
      "result": "deleted",
      "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
      },
      "_seq_no": 3,
      "_primary_term": 1
    } 

猜你喜欢

转载自blog.csdn.net/ticktak/article/details/82346018