Elasticsearch基本操作

By 刘志军 , 2019-12-14, 分类: elasticsearch

elasticsearch

熟悉了elasticsearch 的基本概念后,可以动手实践了,实践才能出真知。

显示索引列表

curl http://localhost:9200/_cat/indices?v

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   accounts MtqOgd0aTb6emn2rj-VfNQ   1   1          2            0        5kb            5kb

用来表示当前系统中有哪些索引,一个索引用一行来记录来表示,每条数据记录索引的基本情况

创建索引

使用 PUT 请求,创建一个名叫 custmer 的索引

curl -X PUT "localhost:9200/customer?pretty"

或者使用PostMan操作

微信截图_20191216000923.png

返回结果

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

创建索引时,可以指定一些配置参数,例如分析器,在后面章节有介绍。

删除索引

DELETE 请求删除 customer 索引。

curl -X DELETE "localhost:9200/customer?pretty"

acknowledged 表示删除成功

{
  "acknowledged" : true
}

创建文档

微信截图_20200223115532.png

创建文档就是将文档加入索引库中方便后面检索。用POST请求,不需要指定文档ID,系统会自动分配唯一ID。(如果要自己指定id,就用PUT方式)

POST /customer/_doc?pretty

{
  "name": "张翠山",
  "address": "湖北十堰"
}

返回id为0xMBGGoB15mDBRoAwkbg的文档。

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

或者用create的方式创建, 创建id为的1的文档。如果该文档已经存在与索引中,则会报错。

PUT users/_doc/1?op_type=create
{
  xxxxxx
}

或者

PUT users/_create/1
{
  xxxxxx
}

替换文档

在 customer 索引下创建或替换 id 为 1 的文档,文档是json格式,该文档有两个字段。注意这里用的是 PUT 方法,同时在url中指定了文档 id,当该id对应的文档不存在时创建新的文档。如果原文章存在,所有字段将全部被替换

PUT /customer/_doc/1?pretty

Content-Type: application/json

{
  "name": "张三丰", 
  "address": "广东深圳"
}

返回结果:

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

查找文档

在 custmer 索引下找id 等于 1 的文档

GET /customer/_doc/1?pretty

返回:

{
    "_index": "customer",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "_seq_no": 2,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "张三丰",
        "address": "广东深圳"
    }
}

如果没找到返回的结果是:

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "3",
  "found" : false
}

不过我们在实际应用场景中,很少通过文档id去查找,而是通过模糊匹配全文检索,而且支持多个字段的搜索,例如输入关键字"python",可以在文档的 title 字段搜索,也可以在文档的 content 字段搜索。关于搜索API,后面再详细介绍

更新文档

当原数据(比如MySQL)有更新时,我们要及时将更新同步索引中的文档才能正确检索到结果。前面介绍了用PUT请求对文档进行替换,但如果并不是每个字段都需要更新的时候,我们也可以单独针对某一个字段进行更新。

注意这里是 POST 请求, URL 的前缀是 _update, 并指定 ID 为 1,比如我们只修改名字,其他字段不变

POST /customer/_update/1?pretty
{
  "doc": { "name": "张真人" }
}

返回

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

更新的原理其实还是建立在替换的基础之上,他会把旧的文档删除,然后创建一个新的。

删除文档

DELETE 请求删除指定文档

DELETE /customer/_doc/1BMNGGoB15mDBRoAVUZF?pretty

返回:

{
    "_index": "customer",
    "_type": "_doc",
    "_id": "1BMNGGoB15mDBRoAVUZF",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 8,
    "_primary_term": 1
}

批量操作

在一个请求中可以批量操作,一次添加多个文档,或者更新文档的同时删除另外一个文档,非常灵活。例如:添加两个文档(注意数据最后一行要有回车换行)批量操作的格式是:

action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
...

批量添加两个文档

POST /customer/_bulk?pretty
{"index":{"_id":"3"}}
{"name": "张无忌", "address":"万安寺" }
{"index":{"_id":"4"}}
{"name": "赵敏", "address":"蒙古草原" }

返回结果:

{
    "took": 15,
    "errors": false,
    "items": [
        {
            "index": {
                "_index": "customer",
                "_type": "_doc",
                "_id": "3",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 9,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "customer",
                "_type": "_doc",
                "_id": "4",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 10,
                "_primary_term": 1,
                "status": 201
            }
        }
    ]
}

更新id为1的name,同时删除id为2的文档

POST /customer/_bulk?pretty

{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}

5种操作用一张图总结如下:

微信截图_20190415154915.png

index
create
update
delete
doc

批量读取

GET /_mget
{
  "docs" : [
    {
      "_index": "test",
      "_id": 1
    },
     {
      "_index": "test",
      "_id": 2
    }
  ]
}

文档查询

Elasticsearch 的查询语法非常强大单独放在下一节

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-search-API.html

添加新字段

PUT  http://localhost:9200/account/_mapping

{
  "properties": {
    "last_post_at": {
      "type": "date"
    }
  }
}


{
    "acknowledged": true
}

关注公众号「Python之禅」,回复「1024」免费获取Python资源

python之禅

猜你喜欢

2019-12-14
Elasticsearch 分析器
2019-12-14
Elasticsearch 安装
2019-12-14
Elasticsearch 查询
2019-12-14
Elasticsearch基本概念
2019-12-14
Elasticsearch 映射