zoukankan      html  css  js  c++  java
  • elasticsearch restful api操作使用指南

    .创建索引

        PUT twitter

        {

            "settings" : {

                "index" : {

                    "number_of_shards" : 3,

                    "number_of_replicas" : 2

                }

            }

        }

        语法规则:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

        备注:

            1.number_of_shards:索引分片数

            2.number_of_replicas:索引副本数

            3.index可以简写

        具体索引字段见:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html

    .获取索引

        1.GET /twitter

        {

            "twitter": {

                "aliases": {},

                "mappings": {},

                "settings": {

                    "index": {

                        "creation_date": "1505635897531",

                        "number_of_shards": "3",

                        "number_of_replicas": "2",

                        "uuid": "VpozplmDTIObzv-lUXryuw",

                        "version": {

                            "created": "5060099"

                        },

                        "provided_name": "twitter"

                    }

                }

            }

        }

        2.GET twitter/[_settings|_mappings|_aliases] - 分别获取与之对应的内容节点信息

    .删除索引

        DELETE /twitter

    .关闭和开启索引

        1.关闭索引

            POST /twitter/_close

        2.开启索引

            POST /twitter/_open

    .创建索引类型

        PUT twitter

        {

            "mappings": {

                "tweet": {

                    "properties": {

                        "message": {

                            "type": "text"

                        }

                    }

                }

            }

        }

        :如果索引不存在则创建,已存在则会报错.如下:

        {

            "error": {

                "root_cause": [

                    {

                        "type": "index_already_exists_exception",

                        "reason": "index [twitter/Exa9xht9SUe_djnaOhX4fA] already exists",

                        "index_uuid": "Exa9xht9SUe_djnaOhX4fA",

                        "index": "twitter"

                    }

                ],

                "type": "index_already_exists_exception",

                "reason": "index [twitter/Exa9xht9SUe_djnaOhX4fA] already exists",

                "index_uuid": "Exa9xht9SUe_djnaOhX4fA",

                "index": "twitter"

            },

            "status": 400

        }

        那么如果针对已存在的索引增加一个类型?

        PUT twitter/_mapping/user

        {

            "properties": {

                "name": {

                    "type": "text"

                }

            }

        }

        批量给多个索引增加一个操作类型

        PUT aa,bb,cc/_mapping/test

        {

            "properties": {

                "name": {

                    "type": "text"

                }

            }

        }

        文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

    .获取索引类型

        1.获取某个索引下单个类型

            GET /twitter/_mapping/game

        2.全局匹配某个类型

            GET /_mapping/game

        3.获取全部类型

            GET /_all/_mapping

            GET /_mapping

    .添加数据

        PUT twitter/tweet/1

        {

            "user" : "kimchy",

            "post_date" : "2009-11-15T14:12:12",

            "message" : "trying out Elasticsearch"

        }

        :

            1.不存则添加否则更新.

            2.索引/类型/主键ID

            3.twitter/tweet/1?op_type=create只负责创建,存在则报错

    8.乐观锁

        PUT twitter/tweet/1?version=2

        {

            "message" : "elasticsearch now has versioning support, double cool!"

        }

    9.主键ID自增

        POST twitter/tweet/

        {

            "user" : "kimchy",

            "post_date" : "2009-11-15T14:12:12",

            "message" : "trying out Elasticsearch"

        }

        主键ID生成格式:AV6PKkAT4fkb07FW6KlE

    10.获取数据

        1.正常返回数据格式

        GET twitter/tweet/0

        返回:

        {

            "_index": "twitter",

            "_type": "tweet",

            "_id": "1",

            "_version": 4,

            "found": true,

            "_source": {

                "user": "kimchy",

                "post_date": "2009-11-15T14:12:12",

                "message": "trying out Elasticsearch"

            }

        }

        2.获取source原数据

            GET /twitter/tweet/1/_source

            {

                "_index": "twitter",

                "_type": "tweet",

                "_id": "1_source=false",

                "found": false

            }

        文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html

    11.修改数据

        POST twitter/tweet/1/_update

        {

            "script" : {

                "source": "ctx._source.user += params.user",

                "lang": "painless",

                "params" : {

                    "user" : "blue"

                }

            }

        }

        :更新tweet类型中user字段,拼接更新.最终结果:??????blue

        合并更新

        POST test/type1/1/_update

        {

            "doc" : {

                "user" : "new_name"

            }

        }

        文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

    12.删除数据

        DELETE /twitter/tweet/1

    结语

        推荐使用一款软件:postman

  • 相关阅读:
    HP惠普战66电源黄灯闪烁无法充电
    C#.NET rabbit mq 持久化时报错 durable
    手动解压安装mysql8.0 on windows my.ini
    C#.NET MySql8.0 EF db first
    EF MYSQL 出现:输入字符串的格式不正确
    EF MYSQL DB FIRST 出现2次数据库名
    mysql windows 下配置可远程连接
    团队项目的Git分支管理规范
    一个简单的软件测试流程
    微服务架构下的质量迷思——混沌工程
  • 原文地址:https://www.cnblogs.com/zhanghuizong/p/7536606.html
Copyright © 2011-2022 走看看