zoukankan      html  css  js  c++  java
  • 《ElasticSearch6.x实战教程》之简单的API

    第三章-简单的API

    关注公众号:CoderBuff,回复“es”获取《ElasticSearch6.x实战教程》完整版PDF。

    万丈高楼平地起

    ES提供了多种操作数据的方式,其中较为常见的方式就是RESTful风格的API。

    简单的体验

    利用Postman发起HTTP请求(当然也可以在命令行中使用curl命令)。

    索引Index

    创建索引

    创建一个名叫demo的索引:

    PUT http://localhost:9200/demo

    ES响应:

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

    在创建索引时,可指定主分片和分片副本的数量:

    PUT http://localhost:9200/demo

    {
        "settings":{
            "number_of_shards":1,
            "number_of_replicas":1
        }
    }
    

    ES响应:

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

    查看指定索引

    GET http://localhost:9200/demo

    ES响应:

    {
        "demo": {
            "aliases": {},
            "mappings": {},
            "settings": {
                "index": {
                    "creation_date": "1561110747038",
                    "number_of_shards": "1",
                    "number_of_replicas": "1",
                    "uuid": "kjPqDUt6TMyywg1P7qgccw",
                    "version": {
                        "created": "5060499"
                    }, 
                    "provided_name": "demo"
                }
            }
        }
    }
    

    查询ES中的索引

    查询ES中索引情况:

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

    ES响应:

    health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
    yellow open demo wqkto5CCTpWNdP3HGpLfxA 5 1 0 0 810b 810b
    yellow open .kibana pwKW9hJyRkO7_pE0MNE05g 1 1 1 0 3.2kb 3.2kb

    可以看到当前ES中一共有2个索引,一个是我们刚创建的demo,另一个是kibana创建的索引.kibana。表格中有一些信息代表了索引的一些状态。

    health:健康状态,red表示不是所有的主分片都可用,即部分主分片可用。yellow表示主分片可用备分片不可用,常常是单机ES的健康状态,greens表示所有的主分片和备分片都可用。(官方对集群健康状态的说明,https://www.elastic.co/guide/en/elasticsearch/guide/master/cluster-health.html

    status:索引状态,open表示打开可对索引中的文档数据进行读写,close表示关闭此时索引占用的内存会被释放,但是此时索引不可进行读写操作。

    index:索引

    uuid:索引标识

    pri:索引的主分片数量

    rep:索引的分片副本数量,1表示有一个分片副本(有多少主分片就有多少备分片,此处表示5个备分片)。

    docs.count:文档数量

    docs.deleted:被删除的文档数量

    store.size:索引大小

    pri.store.size:主分片占用的大小

    删除索引

    删除demo索引,删除索引等同于删库跑路,请谨慎操作。

    DELETE http://localhost:9200/demo

    ES响应:

    {
        "acknowledged": true
    }
    

    类型Type(同时定义映射Mapping字段及类型)

    创建类型

    在前面基本术语中我们提到类型Type类似关系型数据库中的表,映射Mapping定义表结构。创建类型Type时需要配合映射Mapping。

    创建索引demo的类型为example_type,包含两个字段:created类型为date,message类型为keyword:

    方式一:

    PUT http://localhost:9200/demo/_mapping/example_type
    
    {
        "properties":{
            "created":{
                "type":"date"
            },
            "message":{
                "type":"keyword"
            }
        }
    }
    

    此时再次执行查询索引的操作,已经可以发现类型Type被创建了,遗憾的是,如果类型Type(或者映射Mapping)一旦定义,就不能删除,只能修改,为了保证本教程顺利进行方式二创建类型,所以此处执行DELETE http://localhost:9200/demo删除索引。删除索引后不要再创建索引,下面的这种方式是在创建索引的同时创建Type并定义Mapping

    方式二:

    PUT http://localhost:9200/demo
    {
        "mappings":{
            "example_type":{
                "properties":{
                    "created":{
                        "type":"date"
                    },
                    "message":{
                        "type":"keyword"
                    }
                }
            }
        }
    }
    

    此时执行GET http://localhost:9200/demo,可以看到我们在ES中创建了第一个索引以及创建的表结构,接下来插入就是数据(即文档)。

    文档Document

    插入文档

    系统定义_id

    POST http://localhost:9200/demo/example_type
    {
        "created":1561135459000,
        "message":"test1"
    }
    

    ES响应:

    {
        "_index": "demo",
        "_type": "example_type",
        "_id": "AWt67Ql_Tf0FgxupYlBX",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "created": true
    }
    

    查询文档

    ElasticSearch的核心功能——搜索。

    POST http://localhost:9200/demo/example_type/_search?pretty

    ES响应:

    {
        "took": 183,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 1,
            "hits": [
                {
                    "_index": "demo",
                    "_type": "example_type",
                    "_id": "AWt67Ql_Tf0FgxupYlBX",
                    "_score": 1,
                    "_source": {
                        "created": 1561135459000,
                        "message": "test1"
                    }
                }
            ]
        }
    }
    

    关于文档的查询是ElasticSearch的核心,后面的章节会详细介绍一些基本的简单查询和更为高级的复杂查询,此处仅作为对插入数据的验证,不做过多展开。

    修改文档

    根据文档_id修改

    POST http://localhost:9200/demo/example_type/AWt67Ql_Tf0FgxupYlBX/_update
    {
        "doc":{
            "message":"updated"
        }
    }
    

    ES响应:

    {
        "_index": "demo",
        "_type": "example_type",
        "_id": "AWt67Ql_Tf0FgxupYlBX",
        "_version": 2,
        "result": "updated",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        }
    }
    

    删除文档

    删除_id为AWt67Ql_Tf0FgxupYlBX的文档

    DELETE http://localhost:9200/demo/example_type/AWt67Ql_Tf0FgxupYlBX

    ES的响应:

    {
        "found": true,
        "_index": "demo",
        "_type": "example_type",
        "_id": "AWt67Ql_Tf0FgxupYlBX",
        "_version": 2,
        "result": "deleted",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        }
    }
    

    关注公众号:CoderBuff,回复“es”获取《ElasticSearch6.x实战教程》完整版PDF。

    这是一个能给程序员加buff的公众号 (CoderBuff)
  • 相关阅读:
    上百个让你事半功倍的jquery插件
    ashx+jquery+autocomplete.js实现自动填充
    如何引用 System.Runtime.Serialization.Json;
    关于通过标签取得相关文章的算法
    谷歌拼音与搜狗拼音自定义词库互转工具
    Community Sever 2007 简体中文语言包(适用CS2007 RTM/SP1/SP2)
    我的WCF之旅(1):创建一个简单的WCF程序
    网页中嵌入MediaPlayer各种属性与方法设置大全
    我的WCF之旅(3):在WCF中实现双工通信
    SQL Server 2005安装详解
  • 原文地址:https://www.cnblogs.com/yulinfeng/p/11210870.html
Copyright © 2011-2022 走看看