关于后端:ElasticSearch系列基础用法

基本操作

索引<index>

创立

# 1.创立索引
- PUT /索引名 ====> PUT /products
- 留神: 
        1.ES中索引衰弱转态  red(索引不可用) 、yellwo(索引可用,存在危险)、green(衰弱)
        2.默认ES在创立索引时回为索引创立1个备份索引和一个primary索引
        
# 2.创立索引 进行索引分片配置
- PUT /products
{
  "settings": {
    "number_of_shards": 1, #指定主分片的数量
    "number_of_replicas": 0 #指定正本分片的数量
  }
}

)

查问

# 查问索引
- GET /_cat/indices?v

删除

# 3.删除索引
- DELETE /索引名 =====> DELETE /products
- DELETE /*     `*代表通配符,代表所有索引`

映射<mapping>

创立

字符串类型: keyword 关键字 关键词 、text 一段文本

数字类型:integer long

小数类型:float double

布尔类型:boolean

日期类型:date

# 1.创立索引&映射
PUT /products
{ 
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  }, 
  "mappings": {
    "properties": {
      "title":{
        "type": "keyword"
      },
      "price":{
        "type": "double"
      },
      "created_at":{
        "type": "date"
      },
      "description":{
        "type": "text"
      }
    }
  }
}

阐明: ES中反对字段类型十分丰盛,如:text、keyword、integer、long、ip 等。更多参见https://www.elastic.co/guide/en/elasticsearch/reference/7.15/…

查问

# 1.查看某个索引的映射
- GET /索引名/_mapping =====> GET /products/_mapping

文档<document>

增加文档

POST /products/_doc/1 #指定文档id 
{
  "title":"iphone13",
  "price":8999.99,
  "created_at":"2021-09-15",
  "description":"iPhone 13屏幕采纳6.1英寸OLED屏幕。"
}
POST /products/_doc/ #主动生成文档id
{
  "title":"iphone14",
  "price":8999.99,
  "created_at":"2021-09-15",
  "description":"iPhone 13屏幕采纳6.8英寸OLED屏幕"
}
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "sjfYnXwBVVbJgt24PlVU",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

查问文档

GET /products/_doc/1
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "iphone13",
    "price" : 8999.99,
    "created_at" : "2021-09-15",
    "description" : "iPhone 13屏幕采纳6.1英寸OLED屏幕"
  }
}

删除文档

DELETE /products/_doc/1
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

更新文档

PUT /products/_doc/sjfYnXwBVVbJgt24PlVU
{
  "title":"iphon15"
}

阐明: 这种更新形式是先删除原始文档,在将更新文档以新的内容插入。

POST /products/_doc/sjfYnXwBVVbJgt24PlVU/_update
{
    "doc" : {
        "title" : "iphon15"
    }
}

阐明: 这种形式能够将数据原始内容保留,并在此基础上更新。

批量操作

POST /products/_doc/_bulk #批量索引两条文档
     {"index":{"_id":"1"}}
          {"title":"iphone14","price":8999.99,"created_at":"2021-09-15","description":"iPhone 13屏幕采纳6.8英寸OLED屏幕"}
    {"index":{"_id":"2"}}
          {"title":"iphone15","price":8999.99,"created_at":"2021-09-15","description":"iPhone 15屏幕采纳10.8英寸OLED屏幕"}
POST /products/_doc/_bulk #更新文档同时删除文档
    {"update":{"_id":"1"}}
        {"doc":{"title":"iphone17"}}
    {"delete":{"_id":2}}
    {"index":{}}
        {"title":"iphone19","price":8999.99,"created_at":"2021-09-15","description":"iPhone 19屏幕采纳61.8英寸OLED屏幕"}

阐明:批量时不会因为一个失败而全副失败,而是继续执行后续操作,在返回时依照执行的状态返回!

本文由mdnice多平台公布

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理