常规操作elasticSearch查看和索引(存储)数据

这篇具有很好参考价值的文章主要介绍了常规操作elasticSearch查看和索引(存储)数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

常规操作elasticSearch:

对于elasticSearch的操作 通常用rest API完成

查看所有节点:
GET:
192.168.31.125:9200/_cat/nodes

示例返回:

127.0.0.1 16 97 10 0.23 0.56 0.62 dilm * 6a850788e223
查看健康状态:
GET:
192.168.31.125:9200/_cat/health

示例返回:

1635298278 01:31:18 elasticsearch green 1 1 4 4 0 0 0 0 - 100.0%
查看主节点:
GET:
192.168.31.125:9200/_cat/master

示例返回:

LxbmZJweRySmiwKYs4eAHg 127.0.0.1 127.0.0.1 6a850788e223
查看所有索引(类比mysql查看所数据库):
GET:
192.168.31.125:9200/_cat/indices

示例返回:

green  open .kibana_task_manager_1       CS2sQjxhTmqK_iwwyI0X4Q 1 0    2 0  31.2kb  31.2kb
green  open kibana_sample_data_ecommerce mPzvtBuMQ3KmIWZs9mGlyQ 1 0 4675 0   4.7mb   4.7mb
green  open .apm-agent-configuration     HNEJe3GNSBipQlb-3_Ltow 1 0    0 0    283b    283b
green  open .kibana_1                    hIKbqbavQhKjvhM9znRc9A 1 0   54 1 943.2kb 943.2kb
yellow open customer                     -EYnoz-SQSyFAuXWL_4J5w 1 1    1 0   3.3kb   3.3kb
保存一条数据 put 保存:

注意:

put保存必须有id(唯一识别)

PUT:
192.168.31.125:9200/索引/类型/唯一识别
192.168.31.125:9200/customer/external/1
json参数:
{"name":"zxl"}

示例返回:

成功示例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 5,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 9,
    "_primary_term": 1
}
不带唯一识别失败:
{
    "error": "Incorrect HTTP method for uri [/customer/external/] and method [PUT], allowed: [POST]",
    "status": 405
}
保存一条数据 POST保存:

注意:

不带id(唯一识别) 新增(唯一识别自动生成)

带id(唯一识别) id已存在 更新

带id(唯一识别) id已存在 新增

POST:
192.168.31.125:9200/索引/类型/唯一识别
192.168.31.125:9200/customer/external/2
json参数:
{"name":"sss"}

示例返回:

成功示例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "2",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 12,
    "_primary_term": 1
}
不带唯一识别成功实例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "y7atv3wBC3_10cmr4V9N",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 13,
    "_primary_term": 1
}
查看数据:
GET:
192.168.31.125:9200/索引/类型/唯一识别
192.168.31.125:9200/customer/external/1

示例返回:

成功示例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 7,
    "_seq_no": 11,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "ooo"
    }
}

_seq_no 和 _primary_term 常用语存储数据并发时乐观锁操作 防止同时请求被多次修改

***乐观锁保存示例:
PUT:
192.168.31.125:9200/customer/external/1?if_seq_no=11&if_primary_term=1
json参数:
{"name":"yyds"}

示例返回:

第一次操作条件符合成功:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 8,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 14,
    "_primary_term": 1
}
继续点击提交 _seq_no发生变化 保存失败:
{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[1]: version conflict, required seqNo [11], primary term [1]. current document has seqNo [14] and primary term [1]",
                "index_uuid": "-EYnoz-SQSyFAuXWL_4J5w",
                "shard": "0",
                "index": "customer"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, required seqNo [11], primary term [1]. current document has seqNo [14] and primary term [1]",
        "index_uuid": "-EYnoz-SQSyFAuXWL_4J5w",
        "shard": "0",
        "index": "customer"
    },
    "status": 409
}

***POST的_update更新数据更新文档示例:
POST:
192.168.31.125:9200/customer/external/1/_update
json参数:
{"name":"yyds"}

POST更新文档指定 索引/类型/唯一识别/_update

json参数应该为:{“doc”:{“name”:“333”}}

数据参数项必须在json对象的doc内。

数据会对比原数据,如果数据相同, 数据不会有操作,并且返回result:noop。此时操作:版本号_version 操作序列号_seq_no 不变。

示例返回:

第一次操作条件符合成功:

继续点击提交 保存:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 11,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 17,
    "_primary_term": 1
}
继续提交:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 11,
    "result": "noop",
    "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
    },
    "_seq_no": 17,
    "_primary_term": 1
}

只有POST带有_update参数,更新数据 数据才会对比原数据。

put和post不带有_udpate参数,都是更新数据。

如果增加唯一识别下数据的其他属性,直接带上添加保存即可。增加新属性 ,数据肯定变化,post带有udpate也是更新哦!

删除文档
DELETE请求:
192.168.31.125:9200/customer/external/1/

示例返回:

成功实例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 14,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 20,
    "_primary_term": 1
}
继续提交 删除不存在的主键文档:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 15,
    "result": "not_found",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 21,
    "_primary_term": 1
}

删除文档后查找数据

get:
192.168.31.125:9200/customer/external/1/
实例:
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "found": false
}

删除是可以针对索引 文档。没有直接删除类型的操作

删除索引
DELETE请求:
192.168.31.125:9200/customer/

示例返回:

成功实例:
{
    "acknowledged": true
}

删除索引后查找数据

get:
192.168.31.125:9200/customer/external/1/
实例:
{
    "error": {
        "root_cause": [
            {
                "type": "index_not_found_exception",
                "reason": "no such index [customer]",
                "resource.type": "index_expression",
                "resource.id": "customer",
                "index_uuid": "_na_",
                "index": "customer"
            }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index [customer]",
        "resource.type": "index_expression",
        "resource.id": "customer",
        "index_uuid": "_na_",
        "index": "customer"
    },
    "status": 404
}
批量导入数据:
POST:
192.168.31.125:9200/customer/external/_bulk

json参数语法:

{"index":{"_id":"1"}} 文档1的主键
{"name":"zhao"}       文档1的内容
{"index":{"_id":"2"}} 文档2的主键
{"name":"qian"}       文档2的主键

常规操作elasticSearch查看和索引(存储)数据

复杂批量实例:
POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123"}}
{ "doc":{"title":"My updated blog post"} }

POST /_bulk
{ “delete”: { “_index”: “website”, “_type”: “blog”, “_id”: “123” }} 删除
{ “create”: { “_index”: “website”, “_type”: “blog”, “_id”: “123” }}创建
{ “title”: “My first blog post” } 创建的内容
{ “index”: { “_index”: “website”, “_type”: “blog” }} 插入
{ “title”: “My second blog post” } 插入的内容
{ “update”: { “_index”: “website”, “_type”: “blog”, “_id”: “123”}} 更新
{ “doc”:{“title”:“My updated blog post”} } 更新的内容

常规操作elasticSearch查看和索引(存储)数据文章来源地址https://www.toymoban.com/news/detail-401515.html

到了这里,关于常规操作elasticSearch查看和索引(存储)数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ES-Elasticsearch查看所有索引及查看某索引下的信息

    1.查看所有索引,地址栏直接访问下面的连接 http://localhost:9200/_cat/indices?vpretty 2.查看某索引下存的信息,查询的信息为索引结构信息(indexName为索引名称) GET方法:http://127.0.0.1:9200/indexName?pretty 3.查看某个索引下的所有文档数据 GET方法:http://localhost:9200/indexName/_search(需带下

    2024年02月11日
    浏览(31)
  • [golang gin框架] 38.Gin操作Elasticsearch创建索引、修改映射、数据CURD以及数据分页

    常见的 Golang 操作 ElasticSearch 的插件主要有下面两个: 第三方插件: github.com/olivere/elastic 官网插件 github.com/elastic/go-elasticsearch 其中 elastic 比 go-elasticsearch 文档更全面一些,start 量也更多一些,本节讲解 elastic 使用第三方库 https://github.com/olivere/elastic 来连接 ES 并进行操作 注意

    2024年02月09日
    浏览(44)
  • ElasticSearch篇——Restful风格详解以及常见的命令,涵盖_cat命令查看ES默认数据、索引和文档的增删改查以及复杂搜索,超详细、超全面、超细节!

    一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要是用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更加简洁,更有层次,更易于实现缓存等机制。 一、基本Rest命令说明 1、命令 对应的就是head可视化界面的下面的信息(换句话

    2024年01月16日
    浏览(29)
  • es 索引操作(创建、查看、删除)

    Elasticsearch采用Rest风格API,因此其API就是一次http请求,只要能发起http请求。 settings:表示索引库设置,其中可以定义索引库的各种属性 比如分片数 副本数等,暂时不设置,都走默认。 GET /索引名称 GET /索引名称1,索引名称2,索引名称3,… 信息就比较多了 #! Deprecation: Elasticse

    2023年04月08日
    浏览(25)
  • 原生语言操作和spring data中RestHighLevelClient操作Elasticsearch,索引,文档的基本操作,es的高级查询.查询结果处理. 数据聚合.相关性系数打分

    ​ Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasti

    2024年02月05日
    浏览(56)
  • elasticsearch索引操作,索引创建、索引更新、索引删除

    创建索引 更新索引,添加字段 注意更新索引时与创建索引大致一样,只是更新索引时候的url不同,需要在后面加一个 _mapping 路径,同时请求的json里面不需要 mappings 路径,只需要 properties 即可 更新索引,修改配置 同理在更新setting的时候和更新maping的时候一样 获取索引结构

    2024年02月11日
    浏览(33)
  • 微服务学习|初识elasticsearch、操作索引库、文档操作、RestClient操作索引库、RestClient操作文档

    elasticsearch是一款非常强大的开源搜索引擎,可以帮助我们从海量数据中快速找到需要的内容。 elasticsearch结合kibana、Logstash、Beats,也就是elastic stack (ELK)。被广泛应用在日志数据分析、实时监控等领域 elasticsearch是elastic stack的核心,负责存储、搜索、分析数据 Lucene是一个jav

    2024年01月18日
    浏览(36)
  • ElasticSearch索引操作入门

    目录 一、索引创建 二、查看索引 1、查看所有索引 2、查看单个索引 三、删除索引 四、映射关系 1、先创建一个索引 2、创建映射 2.1、创建映射 2.2、创建映射设置分片,不设置会默认一个主分片一个备份分片 2.3、ignore_above限定字符长度 2.4、doc_values 属性 2.5、fielddata属性 3、

    2024年03月14日
    浏览(26)
  • elasticsearch的索引库操作

    索引库就类似数据库表,mapping映射就类似表的结构。我们要向es中存储数据,必须先创建“库”和“表”。 mapping是对索引库中文档的约束,常见的mapping属性包括: type:字段数据类型,常见的简单类型有: 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家

    2024年02月10日
    浏览(22)
  • Elasticsearch(1)——倒排索引与HTTP操作Elasticsearch

    1 前言 Elastic Stack 核心产品包括 Elasticsearch【存储数据】、Kibana【展示数据】、Beats 和 Logstash【收集与传输数据】(也称为 ELK Stack)等等。能够安全可靠地从任何来源获取任何格式的数据,然后对数据进行搜索、分析和可视化。sa Elasticsearch 是一个分布式、RESTful 风格的搜索和

    2024年02月12日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包