Elastic Search:(一)快速入门

目录

1.快速入门

1.1 核心概念介绍

1.2 RESTful风格介绍

1.2.1 概念

1.2.2 方法

1.3 索引

1.3.1 新增索引(PUT)

1.3.2 获取索引(GET) 

1.3.3 删除索引(DELETE)

1.3.4 判断索引是否存在(HEAD)

1.3.5 关闭索引(POST)

1.3.6 开启索引(POST)

1.4 映射

1.4.1 新增映射

1.4.2 获取映射

1.4.3 更新映射

1.5 文档

1.5.1 新增文档(PUT、POST)

1.5.2 查找文档(GET)

1.5.3 更新文档

1.5.4 删除文档


1.快速入门

1.1 核心概念介绍

名称介绍
索引(index)一个索引相当于一个关系型数据库;
类型(type)一个type相当于一类表,在7.x版本后废除。
映射(mapping)mapping定义了每个字段的类型等信息,相当于关系型数据库中的表结构。
文档(document)一个document相当于关系型数据库中的一行记录。
字段相当于关系型数据库的字段。

集群

集群由一个或多个节点组成,一个集群默认名称为elastic search
节点

集群的节点,一台机器或一个进程。

分片和副本
  1. 副本是分片的副本。分片由主分片(primary shard)和负分片(replia shard)之分;
  2. 一个index数据在物理上被分布在多个主分片中,每个主分片只存放部分数据;
  3. 每个主分片可以有多个副本,叫副本分片,是主分片的复制。

1.2 RESTful风格介绍

1.2.1 概念

  1. RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。
  2. elastic search 基于RESTful风格进行设计。

1.2.2 方法

方法名称介绍
HEAD只获取某个资源的头部信息

PUT

获取资源

POST

创建或更新资源,用于发布资源
GET创建或更新资源,用于创建节点
DELETE删除资源

1.3 索引

可使用curl和postman两种方式进行操作,postman为可视化形式,较为简便。如下给出curl指令。postman的请求指令为图中标出的发送请求。

1.3.1 新增索引(PUT)

新增索引均采用PUT指令

  1. 请求

    # curl指令
    curl -X PUT "localhost:9200/lyf"
    # postman指令
    PUT <http://localhost/9200/lyf>
    
  2. 响应

    {
        "acknowledged": true,
        "shards_acknowledged": true,
        "index": "lyf"
    }
    

1.3.2 获取索引(GET) 

索引获取方式均采用GET指令

  1. 获取单个索引

    指定索引名即可获取单个索引信息

    1. 请求

      # curl指令
      curl -X GET "localhost:9200/lyf"
      # postman指令
      GET <http://localhost/9200/lyf>
      
    2. 响应

      {
          "lyf": {
              "aliases": {},
              "mappings": {},
              "settings": {
                  "index": {
                      "routing": {
                          "allocation": {
                              "include": {
                                  "_tier_preference": "data_content"
                              }
                          }
                      },
                      "number_of_shards": "1",
                      "provided_name": "lyf",
                      "creation_date": "1630293470150",
                      "number_of_replicas": "1",
                      "uuid": "As3J8NWLSLO0xO2Ck-Po2w",
                      "version": {
                          "created": "7140099"
                      }
                  }
              }
          }
      }
      

  2. 批量获取索引

    将指定多个索引名通过“,”分隔开,即可获取多个索引信息

    1. 请求

      # curl指令
      curl -X GET "localhost:9200/lyf,wjj"
      # postman指令
      GET <http://localhost/9200/lyf,wjj>
      
    2. 响应

      {
          "lyf": {
              "aliases": {},
              "mappings": {},
              "settings": {
                  "index": {
                      "routing": {
                          "allocation": {
                              "include": {
                                  "_tier_preference": "data_content"
                              }
                          }
                      },
                      "number_of_shards": "1",
                      "provided_name": "lyf",
                      "creation_date": "1630293470150",
                      "number_of_replicas": "1",
                      "uuid": "As3J8NWLSLO0xO2Ck-Po2w",
                      "version": {
                          "created": "7140099"
                      }
                  }
              }
          },
          "wjj": {
              "aliases": {},
              "mappings": {},
              "settings": {
                  "index": {
                      "routing": {
                          "allocation": {
                              "include": {
                                  "_tier_preference": "data_content"
                              }
                          }
                      },
                      "number_of_shards": "1",
                      "provided_name": "wjj",
                      "creation_date": "1630293693626",
                      "number_of_replicas": "1",
                      "uuid": "DOze7sXOTGOm6Y5cO_0WQQ",
                      "version": {
                          "created": "7140099"
                      }
                  }
              }
          }
      }
      

  3. 获取所有索引

    第一种获取所有索引的方式为:在url后面加入“_all”指令,该方式获得索引的详细信息

    1. 请求

      # curl指令
      curl -X GET "localhost:9200/_all"
      # postman指令
      GET <http://localhost/9200/_all>
      
    2. 响应

      {
          "lyf": {
              "aliases": {},
              "mappings": {},
              "settings": {
                  "index": {
                      "routing": {
                          "allocation": {
                              "include": {
                                  "_tier_preference": "data_content"
                              }
                          }
                      },
                      "number_of_shards": "1",
                      "provided_name": "lyf",
                      "creation_date": "1630293470150",
                      "number_of_replicas": "1",
                      "uuid": "As3J8NWLSLO0xO2Ck-Po2w",
                      "version": {
                          "created": "7140099"
                      }
                  }
              }
          },
          "wjj": {
              "aliases": {},
              "mappings": {},
              "settings": {
                  "index": {
                      "routing": {
                          "allocation": {
                              "include": {
                                  "_tier_preference": "data_content"
                              }
                          }
                      },
                      "number_of_shards": "1",
                      "provided_name": "wjj",
                      "creation_date": "1630293693626",
                      "number_of_replicas": "1",
                      "uuid": "DOze7sXOTGOm6Y5cO_0WQQ",
                      "version": {
                          "created": "7140099"
                      }
                  }
              }
          }
      }
      

  4. 获取所有索引

    第二中获取所有索引的方式为在url后加入“_cat/indices”指令,该方式获取索引的简略信息

    1. 请求

      # curl指令
      curl -X GET "http:localhost/9200/_cat/indices"
      # postman指令
      GET <http://localhost/9200/_cat/indices>
      
    2. 响应

      green  open .geoip_databases LtZCua40SDWsH2uySlq1FQ 1 0 42 0 40.8mb 40.8mb
      yellow open wjj              DOze7sXOTGOm6Y5cO_0WQQ 1 1  0 0   208b   208b
      yellow open lyf              As3J8NWLSLO0xO2Ck-Po2w 1 1  0 0   208b   208b
      

1.3.3 删除索引(DELETE)

使用DELETE指令删除索引。

  1. 请求

    # curl指令
    curl -X DELETE "localhost:9200/wjj"
    # postman指令
    DELETE <http://localhost/9200/wjj>
    
  2. 响应

    {
        "acknowledged": true
    }
    

1.3.4 判断索引是否存在(HEAD)

  1. 请求

    # curl指令
    # 存在的索引
    curl -I DELETE "localhost:9200/lyf"
    # 不存在的索引
    curl -I "localhost:9200/wjj"
    
    # postman指令
    # 存在的索引
    DELETE <http://localhost/9200/lyf>
    # 不存在的索引
    DELETE <http://localhost/9200/wjj>
    
  2. 响应

    # 存在索引响应
    HTTP/1.1 200 OK
    # 不存在索引响应
    HTTP/1.1 404 Not Found
    

1.3.5 关闭索引(POST)

  1. 请求

    # curl指令
    curl -X POST "localhost:9200/lyf"
    # postman指令
    POST <http://localhost/9200/lyf>
    
  2. 响应

    {
        "acknowledged": true,
        "shards_acknowledged": true,
        "indices": {
            "lyf": {
                "closed": true
            }
        }
    }
    

1.3.6 开启索引(POST)

  1. 请求

    # curl指令
    curl -X POST "localhost:9200/lyf"
    # postman指令
    POST <http://localhost/9200/lyf>
    
  2. 响应

    {
        "acknowledged": true,
        "shards_acknowledged": true
    }
    

1.4 映射

1.4.1 新增映射

映射相当于关系型数据库中的表,每一个映射中包含的字段即表的属性。具体新增操作为:

  1. 发送请求

    PUT <http://localhost/9200/lyf/_mapping>
    
  2. 点击Body,选择row,之后选择JSON

  3. 填写JSON格式的映射属性

    {
        "properties":{
            "name":{
                "type":"text"
            },
            "No":{
                "type":"text"
            },
            "age":{
                "type":"keyword"
            },
            "sex":{
                "type":"keyword"
            },
            "address":{
                "type":"text"
            }
        }
    }
    
  4. 得到请求

    {
        "acknowledged": true
    }
    

1.4.2 获取映射

  1. 获取单一映射

    1. 发送GET请求

      GET <http://localhost:9200/students/_mapping>
      
    2. 得到响应

      {
          "students": {
              "mappings": {
                  "properties": {
                      "No": {
                          "type": "text"
                      },
                      "address": {
                          "type": "text"
                      },
                      "age": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "sex": {
                          "type": "keyword"
                      }
                  }
              }
          }
      }
      
  2. 批量获取映射

    1. 发送GET请求,用","将多个索引隔开

      <http://localhost:9200/students,lyf/_mapping>
      
    2. 得到响应

      {
          "students": {
              "mappings": {
                  "properties": {
                      "No": {
                          "type": "text"
                      },
                      "address": {
                          "type": "text"
                      },
                      "age": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "sex": {
                          "type": "keyword"
                      }
                  }
              }
          },
          "lyf": {
              "mappings": {}
          }
      }
      

  3. 获取所有映射

    1. 发送GET请求,在根路径下输入_mapping或者输入_all/mapping

      <http://localhost:9200/_mapping>
      
      <http://localhost:9200/_all/_mapping>
      
    2. 得到相应

      {
          "students": {
              "mappings": {
                  "properties": {
                      "No": {
                          "type": "text"
                      },
                      "address": {
                          "type": "text"
                      },
                      "age": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "sex": {
                          "type": "keyword"
                      }
                  }
              }
          },
          "lyf": {
              "mappings": {}
          }
      }
      

1.4.3 更新映射

映射的更新只能新增映射字段,而无法修改映射字段。即,缺少某一字段可以添加,但若字段设置错误,则无法修改。

更新映射类似新增映射,在已有映射基础上进行字段的添加。

  1. 发送POST请求

    POST <http://localhost:9200/lyf/_mapping>
    
  2. 点击Body,选择row,之后选择JSON

  3. 填写JSON格式的映射属性

    {
        "properties":{
            "name":{
                "type":"text"
            },
            "No":{
                "type":"text"
            },
            "age":{
                "type":"keyword"
            },
            "sex":{
                "type":"keyword"
            },
            "address":{
                "type":"text"
            },
    				// 新增字段
    				"phone":{
    						"type":"text"
    				}
        }
    }
    
  4. 得到响应

1.5 文档

1.5.1 新增文档(PUT、POST)

  1. 指定Id的方式

    1. 发送PUT请求

      在指定索引后加入_doc/id方式,新增一个文档,例如为索引students新增一个id为1的文档

      PUT <http://localhost/9200/students/_doc/1>
      
    2. 输入新增内容

      输入新增文档内容,其字段应与映射的字段相对应,若索引不存在映射,则新增文档后,默认生成索引映射,且字段与新增文档中的字段一致。

      {
          "name":"许嵩",
          "No":"2020131566",
          "age":"30",
          "sex":"男",
          "address":"安徽省"
      }
      
    3. 得到相应信息

      {
          "_index": "students",
          "_type": "_doc",
          "_id": "1",
          "_version": 1,
          "result": "created",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 0,
          "_primary_term": 1
      }
      

  2. 自动创建文档(不指定id)

    该方式与指定id新增文档方式步骤相同,但是文档id由系统自动生成,请求方式为POST请求。

    1. 发送PUT请求

      在指定索引后加入_doc/id方式,新增一个文档,例如为索引students新增一个id为1的文档

      PUT <http://localhost/9200/students/_doc>
      
    2. 输入新增内容

      输入新增文档内容,其字段应与映射的字段相对应,若索引不存在映射,则新增文档后,默认生成索引映射,且字段与新增文档中的字段一致。

      {
          "name":"周杰伦",
          "No":"2020131567",
          "age":"40",
          "sex":"男",
          "address":"台湾省"
      }
      
    3. 得到相应信息

      {
          "_index": "students",
          "_type": "_doc",
      		// 系统生成id
          "_id": "NtQ8lnsBlAiSLOzxFcXJ",
          "_version": 1,
          "result": "created",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 1,
          "_primary_term": 1
      }
      

  3. 防止重复id覆盖文档

    在新建文档的过程中,若id相同,内容不同,则之前保存的内容会被覆盖,为防止文档覆盖,在指定文档id后加入指令 ⇒ ?op_type=create,如下所示:

    	PUT <http://localhost:9200/students/_doc/NtQ8lnsBlAiSLOzxFcXJ?op_type=create>
    

1.5.2 查找文档(GET)

  1. 指定文档id查找

    1. 使用GET请求,根据指定文档id获取详细信息

      GET <http://localhost:9200/students/_doc/NtQ8lnsBlAiSLOzxFcXJ>
      
    2. 得到详细信息

      {
          "_index": "students",
          "_type": "_doc",
          "_id": "NtQ8lnsBlAiSLOzxFcXJ",
          "_version": 1,
          "_seq_no": 1,
          "_primary_term": 1,
          "found": true,
          "_source": {
              "name": "周杰伦",
              "No": "2020131567",
              "age": "40",
              "sex": "男",
              "address": "台湾省"
          }
      }
      

  2. 查找多个文档(法1)

    1. 发送GET请求,在根路径后输入 _mget

      GET <http://localhost:9200/_mget>
      
    2. 输入限定信息,以精确查找需要的信息

      {
          "docs":[
              {
      						// 索引
                  "_index":"students",
                  // 类型
      						"_type":"_doc",
                  // 文档id
      						"_id":"1"
              },
              {
                  "_index":"students",
                  "_type":"_doc",
                  "_id":"NtQ8lnsBlAiSLOzxFcXJ"
              }
          ]
      }
      
    3. 得到详细信息

      {
          "docs": [
              {
                  "_index": "students",
                  "_type": "_doc",
                  "_id": "1",
                  "_version": 1,
                  "_seq_no": 0,
                  "_primary_term": 1,
                  "found": true,
                  "_source": {
                      "name": "许嵩",
                      "No": "2020131566",
                      "age": "30",
                      "sex": "男",
                      "address": "安徽省"
                  }
              },
              {
                  "_index": "students",
                  "_type": "_doc",
                  "_id": "NtQ8lnsBlAiSLOzxFcXJ",
                  "_version": 1,
                  "_seq_no": 1,
                  "_primary_term": 1,
                  "found": true,
                  "_source": {
                      "name": "周杰伦",
                      "No": "2020131567",
                      "age": "40",
                      "sex": "男",
                      "address": "台湾省"
                  }
              }
          ]
      }
      

    还可以将索引和类型指定在GET请求中,如下:

    GET <http://localhost:9200/students/_mget>
    // 填写内容去掉_index字段
    {
        "docs":[
            {
                // "_index":"students",
                "_type":"_doc",
                "_id":"1"
            },
            {
                // "_index":"students",
                "_type":"_doc",
                "_id":"NtQ8lnsBlAiSLOzxFcXJ"
            }
        ]
    }
    
    GET <http://localhost:9200/students/_doc/_mget>
    // 填写内容去掉_index和_type字段
    {
        "docs":[
            {
                // "_index":"students",
                // "_type":"_doc",
                "_id":"1"
            },
            {
                // "_index":"students",
                // "_type":"_doc",
                "_id":"NtQ8lnsBlAiSLOzxFcXJ"
            }
        ]
    }
    
    // 当填写内容仅有id字段时,可以简写如下:
    GET <http://localhost:9200/students/_doc/_mget>
    {
        "ids":["1", "NtQ8lnsBlAiSLOzxFcXJ"]
    }
    // 效果一样
    

1.5.3 更新文档

  1. 修改字段信息(指定文档id)

    1. 发送POST请求

      POST <http://localhost:9200/students/_update/1>
      
    2. 填写修改信息

      {
          "doc":{
              "name":"许嵩",
              "No":"2020131566",
      				// 修改年龄
              "age":"32",
              "sex":"男",
              "address":"安徽省"
          }
      }
      
    3. 得到响应信息

      {
          "_index": "students",
          "_type": "_doc",
          "_id": "1",
          "_version": 2,
          "result": "updated",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 2,
          "_primary_term": 1
      }
      

  2. 新增文档字段(指定文档id)

    1. 发送POST请求

      POST <http://localhost:9200/students/_update/1>
      
    2. 填写增加字段信息

      {
          "script":"ctx._source.phone = [15524587588L]"
      }
      
    3. 得到响应信息

      {
          "_index": "students",
          "_type": "_doc",
          "_id": "1",
          "_version": 3,
          "result": "updated",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 3,
          "_primary_term": 1
      }
      
  3. 删除文档字段(指定文档id)

    1. 发送POST请求

      POST <http://localhost:9200/students/_update/1>
      
    2. 填写修改信息

      {
          "script":"ctx._source.remove(\\"phone\\")"
      }
      
    3. 得到响应信息

      {
          "_index": "students",
          "_type": "_doc",
          "_id": "1",
          "_version": 4,
          "result": "updated",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 4,
          "_primary_term": 1
      }
      
  4. 根据文档参数值,更新指定文档字段

    1. 发送POST请求

      POST <http://localhost:9200/students/_update/1>
      
    2. 填写修改信息

      {
          "script":"ctx._source.remove(\\"phone\\")"
      }
      
    3. 得到响应信息

      {
          "_index": "students",
          "_type": "_doc",
          "_id": "1",
          "_version": 4,
          "result": "updated",
          "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
          },
          "_seq_no": 4,
          "_primary_term": 1
      }
      

1.5.4 删除文档

删除文档,指定文档id使用DELETE删除

// 删除文档id为1的文档
DELETE <http://localhost/9200/students/_doc/1>
  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shaco、LYF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值