elasticsearch 基本操作:CURD

一 基础操作:

  • 新建索引
  • 查看索引
  • 更新索引
  • 删除索引
  • 添加索引数据
  • 查看索引数据
  • 更新索引数据
  • 删除索引数据
  • 批量添加索引数据
  • 清空索引内的数据
  • 查看所有的索引
  • 查看索引的mapping

1 新建索引

PUT /second
{
  "mappings": {
    "properties": {
      "class": {
        "type": "text"
      },
      "room": {
        "type": "integer"
      }
    }
  }
}

2 查看索引

GET /second/_mapping

{
  "second" : {
    "mappings" : {
      "properties" : {
        "class" : {
          "type" : "text"
        },
        "room" : {
          "type" : "integer"
        }
      }
    }
  }
}

3 修改索引

es一旦确认了mapping便不能再修改filed了

4 删除索引

DELETE second

5 新增field

#新增一个field为student
PUT /second/_doc/_mapping?include_type_name=true
{
  "properties": {
    "student": {
      "type": "text"
    }
  }
}

6 新建索引数据

#新增1条数据id为1的数据(文档)es叫文档 mysql 叫行
PUT /second/_doc/1
{
  "class":"三年二班",
  "room":"305",
  "student":"周杰伦"
}
# 非指定_id创建
POST /second/_doc/
{
  "class":"一年二班",
  "room":"306",
  "student":"昆凌"
}

7 查看索引数据

GET /second/_doc/1

8 修改索引数据

# 2种格式
PUT /second/_doc/1
{
  "class":"三年二班",
  "room":"305",
  "student":"周杰伦"
}

POST /second/_update/1
{
  "doc": {
    "class": "三年二班",
    "room": "404",
    "student": "周杰伦"
  }
}

9 删除索引数据

DELETE /second/_doc/1

10 批量添加索引数据

阿里云官方代码

#映射
PUT /product_info
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  },
  "mappings": {
      "properties": {
        "productName": {
          "type": "text",
          "analyzer": "standard"
        },
        "annual_rate":{
          "type":"keyword"
        },
        "describe": {
          "type": "text",
          "analyzer": "standard"
        }
    }
  }
}
#批量插入

POST /product_info/products/_bulk
{"index":{}}
{"productName":"大健康天天理财","annual_rate":"3.2200%","describe":"180天定期理财,最低20000起投,收益稳定,可以自助选择消息推送"}
{"index":{}}
{"productName":"西部通宝","annual_rate":"3.1100%","describe":"90天定投产品,最低10000起投,每天收益到账消息推送"}
{"index":{}}
{"productName":"安详畜牧产业","annual_rate":"3.3500%","describe":"270天定投产品,最低40000起投,每天收益立即到账消息推送"}
{"index":{}}
{"productName":"5G设备采购月月盈","annual_rate":"3.1200%","describe":"90天定投产品,最低12000起投,每天收益到账消息推送"}
{"index":{}}
{"productName":"新能源动力理财","annual rate":"3.0100%","describe":"30天定投产品推荐,最低8000起投,每天收益会消息推送"}
{"index":{}}
{"productName":"微贷赚","annual_rate":"2.7500%","describe":"热门短期产品,3天短期,无须任何手续费用,最低500起投,通过短信提示获取收益消息"}

注意事项

注意,es7.x版本取消了type的概念,employee改为_doc即可,

Elasticsearch 7.x
Specifying types in requests is deprecated. For instance, indexing a document no longer requires a document type. The new index APIs are PUT {index}/_doc/{id} in case of explicit ids and POST {index}/_doc for auto-generated ids.
The include_type_name parameter in the index creation, index template, and mapping APIs will default to false. Setting the parameter at all will result in a deprecation warning.
The default mapping type is removed.

11 清空索引的数据

POST /product_info/_delete_by_query
{
  "query":{
    "match_all": {}
  }
}

11 查看所有的索引

GET /_cat/indices

12 查看索引的mapping

GET /second/_mapping

参考链接: https://help.aliyun.com/document_detail/134807.htm?spm=a2c4g.11186623.2.4.3e70585fJPKOEK
https://www.qikegu.com/docs/3074

发表评论

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