美文网首页大数据大数据平台搭建Java 杂谈
Elasticsearch学习教程系列(1)-命令学习(一) 集

Elasticsearch学习教程系列(1)-命令学习(一) 集

作者: 抹布先生M | 来源:发表于2019-03-14 19:25 被阅读7次

    本教程是基于Elasticsearch6.5版本编写
    在本系列教程上一篇文章中,我们介绍了Elasticsearch的一些基本概念、安装并运行起了一个Elasticsearch节点。现在我们已经启动并运行了节点(集群),下一步是了解如何与它进行通信。幸运的是,Elasticsearch提供了一个非常全面和强大的REST API,您可以使用它与集群进行交互。使用API 可以完成的一些事项如下:

    • 检查集群运行情况,状态和统计信息
    • 管理您的群集,节点和索引数据和元数据
    • 对索引执行CRUD(创建,读取,更新和删除)和搜索操作
    • 执行高级搜索操作,例如分页,排序,过滤,脚本编写,聚合等等

    集群运行情况(Cluster Health)

    让我们从基本运行状况检查开始,我们可以使用它来查看集群的运行情况。我们将使用curl来执行此操作,但您可以使用任何允许您进行HTTP / REST调用的工具。假设我们仍然在我们启动Elasticsearch的同一节点上打开另一个命令shell窗口。
    ''[builder@master ~]$ curl -X GET "http://localhost:9200/_cat/health?v"
    '' epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    '' 1547394013 15:40:13 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%
    ''
    我们可以看到名为“elasticsearch”的群集处于green(绿色)状态。
    每当我们要求群集健康时,我们要么获得绿色,黄色或红色。

    绿色 - 一切都很好(集群功能齐全)
    黄色 - 所有数据均可用,但尚未分配一些副本(群集功能齐全)
    红色 - 某些数据由于某种原因不可用(群集部分功能)
    注意:当群集为红色时,它将继续提供来自可用分片的搜索请求,但您可能需要尽快修复它,因为存在未分配的分片。
    同样从上面的响应中,我们可以看到总共1个节点,并且我们有0个分片,因为我们还没有数据。请注意,由于我们使用的是默认群集名称(elasticsearch),并且由于Elasticsearch默认使用单播网络发现来查找同一台计算机上的其他节点,因此您可能会意外启动计算机上的多个节点并将它们所有都加入一个集群。在这种情况下,您可能会在上面的响应中看到多个节点。
    查看集群中的节点列表:
    ''[builder@master ]$ curl -X GET "http://localhost:9200/_cat/nodes?v"
    '' ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
    '' 127.0.0.1 20 98 8 1.90 mdi * siwsSwZ
    从上面的结果中,我们可以看到一个名为“siwsSwZ”的节点,它是我们集群中当前的单个节点。

    索引(indices)的CRUD、查询操作

    查看索引列表:

    [builder@master ~]$ curl -X GET "http://localhost:9200/_cat/indices?v"
    health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
    

    因为我们的节点是刚刚建立的,还没数据,所以上面查询的结果只有一行字段名,没有索引数据

    创建索引

    现在让我们创建一个名为“customer”的索引,然后再次列出所有索引(下面第一个命令使用PUT动词创建名为“customer”的索引。加?pretty表示返回的结果打印格式化过的JSON(如果有的话)。):

    [builder@master ~]$ curl -X PUT "localhost:9200/customer?pretty"
     {
       "acknowledged" : true,
       "shards_acknowledged" : true,
       "index" : "customer"
     }
    [builder@master ~]$ curl -X GET "localhost:9200/_cat/indices?v"
    health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    yellow open   customer i7R-XRH8R0Kn7uLQRNC_yQ   5   1          0            0      1.1kb          1.1kb
    

    第二个命令的结果告诉我们,我们现在有一个名为customer的索引,它有5个主分片和1个副本(默认值),并且它包含0个文档。
    您可能还注意到客户索引标记了黄色运行状况。回想一下我们之前的讨论,黄色表示某些副本尚未(尚未)分配。此索引发生这种情况的原因是因为默认情况下Elasticsearch为此索引创建了一个副本。由于我们目前只有一个节点在运行,因此在另一个节点加入集群的较晚时间点之前,尚无法分配一个副本(用于高可用性)。将该副本分配到第二个节点后,此索引的运行状况将变为绿色。

    Tips: 上面命令中,-X PUT 后的路径不要带http哟,否则会出现如下错误的:

    [builder@master ~]$ curl -X PUT "http://localhost:9200/_cat/customer1?pretty"
     {
       "error" : "Incorrect HTTP method for uri [/_cat/customer1?pretty] and method [PUT], allowed: [POST]",
       "status" : 405
     }
    

    查询文档

    现在让我们在customer索引中加入一些内容。我们将一个简单的客户文档索引到客户索引中,ID为1,如下所示:

    [builder@master ~]$  curl -X PUT  "localhost:9200/customer/_doc/1?pretty" -H "Content-Type:application/json" -d '{ "name": "Builder Luo"}' 
     {
       "_index" : "customer",
       "_type" : "_doc",
       "_id" : "1",
       "_version" : 1,
       "result" : "created",
       "_shards" : {
         "total" : 2,
         "successful" : 1,
         "failed" : 0
       },
       "_seq_no" : 0,
       "_primary_term" : 2
     }
    

    curl命令说明:-X为请求方式,-H 参数为设置请求头,-d参数为请求参数 。
    '' Tips: 关于:路径localhost:9200/customer/_doc/1?pretty
    '' localhost:9200/customer是之前创建的索引,后接/_doc/1表示为在customer索引上创建1个ID为1的文档(?pretty表示将返回的响应json格式化美观)
    提示:值得注意的是,Elasticsearch在你将文档编入索引之前不需要先显式创建索引。在前面的示例中,如果customer索引事先尚未存在,则Elasticsearch将自动创建customer索引。
    查看刚刚创建好的文档:

    [builder@master ~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"
    {
       "_index" : "customer",
       "_type" : "_doc",
       "_id" : "1",
       "_version" : 1,
       "found" : true,
       "_source" : {
         "name" : "Builder Luo"
       }
    }
    

    删除索引

    [builder@master ~]$ curl -X DELETE "localhost:9200/customer?pretty"
    {
       "acknowledged" : true
    }
    [builder@master ~]$  curl -X GET "localhost:9200/_cat/indices?v"
    health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
    

    上面意味着索引已成功删除,我们现在回到我们在集群中没有任何内容的地方。
    在我们继续之前,让我们再仔细看看到目前为止我们学到的一些API命令:

     curl -X PUT ''localhost:9200/customer''
     curl -X GET ''localhost:9200/customer''
     curl -X DELETE ''localhost:9200/customer''
    
     curl -X PUT ''localhost:9200/customer/_doc/1''  -H "Content-Type:application/json" -d '{ "name": "Builder Luo"}'
     curl -X GET ''localhost:9200/customer/_doc/1''
     curl -X DELETE ''localhost:9200/customer/_doc/1''
    

    如果我们仔细研究上述命令,我们实际上可以看到我们如何在Elasticsearch中访问数据的模式。该模式可归纳如下:

    <HTTP Verb>        Node_address/<Index>/<Type>/<ID>
    

    这种REST访问模式在所有API命令中都非常普遍,如果你能记住它,你将在掌握Elasticsearch方面有一个良好的开端。

    替换文档

    Elasticsearch几乎实时提供数据操作和搜索功能。默认情况下,从索引/更新/删除数据到搜索结果中显示的时间,您可能会有一秒钟的延迟(刷新间隔)。
    数据在事务完成后立即可用,这是Elasticsearch与SQL等其他平台的重要区别
    之前,我们执行过如下命令:

    ''[builder@master ~]$  curl -X PUT  "localhost:9200/customer/_doc/1?pretty" -H "Content-Type:application/json" -d '{ "name": "Builder Luo"}' 
    下面,我们执行一样的命令,文档ID还是指定为1,只是-d参数内容有变化:
    ''[builder@master ~]$  curl -X PUT  "localhost:9200/customer/_doc/1?pretty" -H "Content-Type:application/json" -d '{ "name": "Baby Mon"}' 
    ''{
    ''   "_index" : "customer",
    ''   "_type" : "_doc",
    ''   "_id" : "1",
    ''   "_version" : 2,
    ''   "result" : "updated",
    ''   "_shards" : {
    ''     "total" : 2,
    ''     "successful" : 1,
    ''     "failed" : 0
    ''   },
    ''   "_seq_no" : 1,
    ''   "_primary_term" : 1
    '' }
    
    
    

    以上内容将ID为1的文档名称从“Builder Luo”更改为“Baby Mon”, result字段值为: updated。另一方面,如果我们使用不同的ID,则会对新文档编制索引,并且索引中已有的现有文档保持不变。
    索引时,ID部分是可选的。如果未指定,Elasticsearch将生成随机ID,然后使用它来索引文档。Elasticsearch生成的实际ID(或前面示例中显式指定的内容)将作为索引API调用的一部分返回。
    此示例显示如何在没有显式ID的情况下索引文档:

    [builder@master ~]$  curl -X POST  "localhost:9200/customer/_doc?pretty" -H "Content-Type:application/json" -d '{ "name": "Moonlight Chen"}'
     {
       "_index" : "customer",
       "_type" : "_doc",
       "_id" : "UmGaVGgBol3Y-BIhbtd0",
       "_version" : 1,
       "result" : "created",
       "_shards" : {
         "total" : 2,
         "successful" : 1,
         "failed" : 0
       },
       "_seq_no" : 0,
       "_primary_term" : 1
     }
    
    

    可以看到上面自动生成的ID是:UmGaVGgBol3Y-BIhbtd0
    请注意,在上面的情况中,我们使用POST动词而不是PUT,因为我们没有指定ID。

    修改文档数据

    除了能够重新索引和替换文档,我们还可以更新文档数据。(请注意,Elasticsearch实际上并没有在内部进行就地更新。每当我们进行更新时,Elasticsearch都会删除旧文档,然后一次性对应用了更新的新文档编制索引。)

    # -d参数,需要封装成: { "doc": {dataJSON}}
    [builder@master ~]$  curl -X POST  "localhost:9200/customer/_doc/1/_update?pretty" -H "Content-Type:application/json" -d '{ "doc": { "name": "My Baby Mon",  "age": 20} }' 
    {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 4,
        "result" : "updated",
        "_shards" : {
            "total" : 2,
            "successful" : 1,
            "failed" : 0
        },
        "_seq_no" : 3,
        "_primary_term" : 1
    }
    
    
    # 也可以使用简单脚本执行更新。此示例使用脚本将年龄增加5:
    [builder@master ~]$  curl -X POST  "localhost:9200/customer/_doc/1/_update?pretty" -H "Content-Type:application/json" -d '{ "script":  "ctx._source.age += 5" }'
    {
       "_index" : "customer",
       "_type" : "_doc",
       "_id" : "1",
       "_version" : 5,
       "result" : "updated",
       "_shards" : {
         "total" : 2,
         "successful" : 1,
         "failed" : 0
       },
       "_seq_no" : 4,
       "_primary_term" : 1
     }
    # 在上面的示例中,ctx._source指的是即将更新的当前源文档。
    
    

    Elasticsearch提供了在给定查询条件(如SQL UPDATE-WHERE语句)的情况下更新多个文档的功能。请参阅[docs-update-by-queryAPI[https://www.elastic.co/guide/en/elasticsearch/reference/6.5/docs-update-by-query.html]]
    本文到这里,我们主要介绍了Elasticsearch的索引、文档的一些主要命令,我们将在下一篇文章中介绍Elasticsearch的批处理命令、以及探索操作数据的命令

    -End- 扫描关注一下,可以互相交流学习哟

    相关文章

      网友评论

        本文标题:Elasticsearch学习教程系列(1)-命令学习(一) 集

        本文链接:https://www.haomeiwen.com/subject/tnacmqtx.html