elasticsearch操作

本文详细介绍了Elasticsearch的操作,包括添加、更新和创建文档,检索、全文搜索、短语搜索、高亮搜索及聚合分析。还讨论了删除文档、查看集群健康状况、处理更新丢失问题以及文档部分更新的策略。同时提到了版本控制在避免更新冲突中的作用。
摘要由CSDN通过智能技术生成

添加

类型为employee,该类型位于索引megacorg,每个雇员索引一个文档,该文档包含该雇员的全部信息(面向文档),该雇员的id为1

需要index、type、id

curl -X PUT -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp/employee/1 --data '{
   "first_name": "John",
   "last_name": "Smith",
   "age": 25,
   "about": "I love to go rock climbing",
   "interests": [
      "sports",
      "music"
   ]
}'

添加更多的雇员
curl -X PUT -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp/employee/2 --data '{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}'

增加索引的时候,默认会有5个主分片,主分片是在创建索引的时候就要固定的,而副本分片个数随时可修改,比如,创建一个主分片为3,副本分片为1的索引。当往es中put数据时,会按照id进行hash,然后put到对应的分片上。

[root@focuson1 ~]# curl -X PUT -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp2 --data '{
>    "settings" : {
>       "number_of_shards" : 3,
>       "number_of_replicas" : 1
>    }
> }'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 68

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

往es添加数据时,也可以不指定id,会自动创建id,需要使用post请求,方式如下:

[root@focuson1 ~]# curl -X POST -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp/employee/ --data '{
>    "first_name": "John2",
>    "last_name": "Smith2",
>    "age": 256,
>    "about": "I love to go rock climbing",
>    "interests": [
>       "sports",
>       "music"
>    ]
> }'
HTTP/1.1 201 Created
Location: /megacorp/employee/TfQ8ymMBtknNDl0i3mwi
content-type: application/json; charset=UTF-8
content-length: 179

{"_index":"megacorp","_type":"employee","_id":"TfQ8ymMBtknNDl0i3mwi","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":3,"_primary_term":1}

更新文档时,

和添加时是一样的,返回一个version,是一个不同于之前的version。更新时,elasticsearch将旧的文档标记为已删除,并增加一个全新的文档,旧的文档会在后台自动清除,但是不会立即清除。

创建文档

返回409,代表已存在,不能创建,如果不加op_type=create,会更新。也可以在URL最后加上/_create
[root@focuson1 ~]# curl -X PUT -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp/employee/1?op_type=create --data '{
>    "first_name": "John",
>    "last_name": "Smith",
>    "age": 25,
>    "about": "I love to go rock climbing",
>    "interests": [
>       "sports",
>       "music"
>    ]
> }'
HTTP/1.1 409 Conflict
content-type: application/json; charset=UTF-8
content-length: 445

{"error":{"root_cause":[{"type":"version_conflict_engine_exception","reason":"[employee][1]: version conflict, document already exists (current version [4])","index_uuid":"hKhKh3YRT6yRiWQiBPSYuw","shard":"3","index":"megacorp"}],"type":"version_conflict_engine_exception","reason":"[employee][1]: version conflict, document already exists (current version [4])","index_uuid":"hKhKh3YRT6yRiWQiBPSYuw","shard":"3","index":"megacorp"},"status":409}

检索文档:

  • 根据需要index、type、id,返回某个文档
[root@focuson1 ~]# curl -X GET -i http://focuson1:9200/megacorp/employee/1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 249

{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"found":true,"_source":{
   "first_name": "John",
   "last_name": "Smith",
   "age": 25,
   "about": "I love to go rock climbing",
   "interests": [
      "sports",
      "music"
   ]
}}
  • pretty
在请求参数中加上pretty,会使返回更加可读,但是source不会,会按照我们添加时候的格式返回
[root@focuson1 ~]# curl -X GET -i http://focuson1:9200/megacorp/employee/1?pretty
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 294

{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 3,
  "found" : true,
  "_source" : {
    "first_name" : "John",
    "last_name" : "Smith",
    "age" : 25,
    "about" : "I love to go rock climbing",
    "interests" : [
      "sports",
      "music"
    ]
  }
}
  • 返回部分字段
只返回部分字段
[root@focuson1 ~]# curl -X GET -i http://focuson1:9200/megacorp/employee/1?_source=first_name,last_name
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 128

{"_index":"megacorp","_type":"employee","_id":"1","_version":3,"found":true,"_source":{"last_name":"Smith","first_name":"John"}}
  • 只返回source部分
只返回source里面的值
[root@focuson1 ~]# curl -X 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值