elasticsearch restful api操作使用指南

 2023-09-19 阅读 22 评论 0

摘要:一.创建索引 PUT twitter { elasticsearch怎么用。"settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 web api restful,} } } 语法规则:https://www.elastic.co/guide/en/elasticsearch/reference/current/indi

.创建索引

    PUT twitter

    {

elasticsearch怎么用。        "settings" : {

            "index" : {

                "number_of_shards" : 3,

                "number_of_replicas" : 2

web api restful,            }

        }

    }

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

restful api设计。 

    备注:

        1.number_of_shards:索引分片数

        2.number_of_replicas:索引副本数

restful api文档,        3.index可以简写

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

 

.获取索引

restful api框架?    1.GET /twitter

    {

        "twitter": {

            "aliases": {},

restfulapi生成工具,            "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

转载于:https://www.cnblogs.com/huizong/p/7536606.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/79141.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息