ElasticSearch 增删改查

 

HTTP 协议本身语义:
GET 获取资源。
POST 新建资源(也可以用于更新资源)。
PUT 更新资源。
DELETE 删除资源。

 

ES通过HTTP Restful方式管理数据:
1.格式:#操作 /index/type/id
2.操作:可以进行
    添加(POST)
    修改(PUT)
    删除(DELETE)
    查询(GET)
    批量插入(_bulk)
    批量获取(_mget)
3.分词器需要在service的plugins导入响应插件

 

 

curl http://192.168.8.200:9200/index/type/id
# index 索引
# type 类型
# id 当前索引内唯一标

 

curl -X GET "http://10.100.172.116:9200/index_name?pretty"
# 查看索引

 

curl -X POST  -H "Content-Type: application/json" "http://192.168.8.200:9200/su/i" --data '{"name":"s","age":19}'
# 添加
# 没有id则自动创建

 

# curl -XDELETE "http://10.100.172.116:9200/indexname"
# 删除索引

 

curl -X DELETE "http://192.168.8.200:9200/su/i/AWwtxGO-KRenIdAIKtoj"
# 删除 指定id数据

 

curl -X PUT  -H "Content-Type: application/json" "http://192.168.8.200:9200/su/i/AWwtxGO-KRenIdAIKtoj" --data '{"name":"f","age":22}'
# 修改,传入需要修改数据的id AWwtxGO-KRenIdAIKtoj
# 如果id不是想修改,在这里会根据传入的id新增一条或修改到其它id的数据。

 

curl -X POST -H "Content-Type: application/json" "http://192.168.8.200:9200/su/i/AWwtxGO-KRenIdAIKtoj/_update" --data '{"doc":{"name":"hf"}}'
# 局部修改使用post和_update

 

$ curl -X GET 'http://localhost:9200/_cat/indices?v'
// 查看所有Index

 

$ curl 'localhost:9200/_mapping?pretty=true'
// 列出索引类型

 

$ curl -X PUT 'localhost:9200/weather'
// 新建索引weather

 

$ curl -X DELETE 'localhost:9200/weather'
// 删除索引weather

 

$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.8/elasticsearch-analysis-ik-5.6.8.zip
// 安装中文分词插件ik
// 根据es版本对应ik版本v5.6.8 

 

$ curl -X PUT -H "Content-Type: application/json" 'localhost:9200/accounts' --data '
{
  "mappings": {
    "person": {
      "properties": {
        "user": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "title": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "desc": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }
      }
    }
  }
}'
// 凡是需要搜索的中文字段,都要单独设置一下。
// 新建一个名称为accounts的 Index,里面有一个名称为person的 Type。person有三个字段。
// analyzer是字段文本的分词器,search_analyzer是搜索词的分词器。ik_max_word分词器是插件ik提供的,可以对文本进行最大数量的分词。

 

$ curl -X PUT -H "Content-Type: application/json" 'localhost:9200/accounts/person/1' --data '
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}' 
// Index 里面新增一条记录


$ curl -X POST 'localhost:9200/accounts/person' -d '
{
  "user": "李四",
  "title": "工程师",
  "desc": "系统管理"
}'

 

$ curl 'localhost:9200/accounts/person/1?pretty=true'
// 查看记录 

 

$ curl -X DELETE 'localhost:9200/accounts/person/1'
// 删除记录

 

$ curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
    "user" : "张三",
    "title" : "工程师",
    "desc" : "数据库管理,软件开发"
}' 
//更新记录,重新发一条数据
/*
    "_version" : 2,
    "result" : "updated",
    "created" : false
*/
// 记录的 Id 没变,但是版本(version)从1变成2,操作类型(result)从created变成updated,created字段变成false,因为这次不是新建记录。

 

$ curl 'localhost:9200/accounts/person/_search'
// 查询数据,返回所有记录 

// took字段表示该操作的耗时(单位为毫秒),
// timed_out字段表示是否超时,
// hits字段表示命中的记录,里面子字段的含义如下。
    // total:返回记录数,本例是2条。
    // max_score:最高的匹配程度,本例是1.0。
    // hits:返回的记录组成的数组。
// 每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。

 

$ curl 'localhost:9200/accounts/person/_search'  -d '
{
  "query" : { "match" : { "desc" : "软件" }}
}'
// Match查询,desc字段 : 匹配条件。

 

$ curl 'localhost:9200/accounts/person/_search'  -d '
{
  "query" : { "match" : { "desc" : "管理" }},
  "size": 1
}'
// 默认一次返回10条结果,可以通过size字段改变这个设置。

 

$ curl 'localhost:9200/accounts/person/_search'  -d '
{
  "query" : { "match" : { "desc" : "管理" }},
  "from": 1,
  "size": 1
}'
// 可以通过from字段,指定位移。
// 从位置1开始(默认是从位置0开始),只返回一条结果。

 

$ curl 'localhost:9200/accounts/person/_search'  -d '
{
  "query" : { "match" : { "desc" : "软件 系统" }}
}'
// 如果有多个搜索关键字, Elastic 认为它们是or关系。
// 软件 or 系统

 

$ curl 'localhost:9200/accounts/person/_search'  -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "desc": "软件" } },
        { "match": { "desc": "系统" } }
      ]
    }
  }
}'
// 如果要执行多个关键词的and搜索,必须使用布尔查询。

 

 

 

 

https://www.ruanyifeng.com/blog/2018/10/restful-api-best-practices.html

http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html
https://www.elastic.co/cn/blog/a-practical-introduction-to-elasticsearch
https://creativecommons.org/licenses/by-nc-nd/3.0/deed.zh

 

posted @ 2019-07-26 10:02  chentianyuan  阅读(299)  评论(0编辑  收藏  举报