elasticsearch之crud

这篇具有很好参考价值的文章主要介绍了elasticsearch之crud。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

elasticsearch之crud

索引之crud

新增索引

PUT /book
{
  "mappings":{
    "properties":{
      "name":{"type": "keyword"},
      "info":{"type": "text"},
      "price":{"type": "integer"}
    }
  }
}

修改索引

索引不支持修改现有的,只支持新增


删除索引

DELETE /book

查询所有的索引

health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_task_manager_1   J_Qp6cL1QGSpNNGSFadkiQ   1   0          2            0      6.6kb          6.6kb
yellow open   book                     Itynp5i7QhmFyMQrKnf9hg   1   1          0            0       230b           230b
green  open   .apm-agent-configuration ce48C5agSqCWP7M3ZP2bEg   1   0          0            0       283b           283b
green  open   .kibana_1                XOg9ADncRKOByv5GAdN2xw   1   0          3            0     15.5kb         15.5kb

文档的crud

新增与覆盖文档

post:带id不存在则新增,存在则覆盖(全量更新),不带id永远新增,且自动生成id

put:必须带id,否则报错,如果存在则覆盖,如果不存在则新增

post带着id不存在则新增

POST /book/_doc/2
{
  "name":"mysql",
  "info":"mysql是最好用的数据库",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 4,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 14,
  "_primary_term" : 3
}
GET /book/_doc/2
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 4,
  "_seq_no" : 14,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "mysql",
    "info" : "mysql是最好用的数据库",
    "price" : 66
  }
}

post带着id存在,则覆盖

POST /book/_doc/2
{
  "name":"mysql",
  "info":"mysql是最好用的数据库,而且还免费",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 15,
  "_primary_term" : 3
}
GET /book/_doc/2
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 5,
  "_seq_no" : 15,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "mysql",
    "info" : "mysql是最好用的数据库,而且还免费"
  }
}

post不带id则新增,且自动生成id

POST /book/_doc
{
  "name":"php",
  "info":"php是最好用的语言",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "Zp2064sB7DFkWXEUKAII",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 3
}

put请求不带id报错,带着id没有就新增,有就覆盖

put不带id报错

PUT /book/_doc
{
  "name":"oracle",
  "info":"oracle是收费的数据库",
  "price":66
}
{
  "error" : "Incorrect HTTP method for uri [/book/_doc?pretty=true] and method [PUT], allowed: [POST]",
  "status" : 405
}

put带着id,不存在则新增

PUT /book/_doc/3
{
  "name":"oracle",
  "info":"oracle是收费的数据库",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 6,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 11,
  "_primary_term" : 3
}
GET /book/_doc/3
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 6,
  "_seq_no" : 11,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "oracle",
    "info" : "oracle是收费的数据库",
    "price" : 66
  }
}

put带着id存在则覆盖

PUT /book/_doc/3
{
  "name":"oracle",
  "info":"oracle是收费的数据库,所以小企业一般不用"
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 7,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 12,
  "_primary_term" : 3
}
GET /book/_doc/3
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 7,
  "_seq_no" : 12,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "oracle",
    "info" : "oracle是收费的数据库,所以小企业一般不用"
  }
}

强制新增

理解

POST /book/_doc/5可能是新增操作,也可能会是覆盖操作,如果加上_create就明确告诉es,我要新增,如果存在就会报错

POST /book/_doc/5/_create
{
  "name":"kafka",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "5",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 18,
  "_primary_term" : 3
}

再次新增时报错

{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[5]: version conflict, document already exists (current version [1])",
        "index_uuid" : "Itynp5i7QhmFyMQrKnf9hg",
        "shard" : "0",
        "index" : "book"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[5]: version conflict, document already exists (current version [1])",
    "index_uuid" : "Itynp5i7QhmFyMQrKnf9hg",
    "shard" : "0",
    "index" : "book"
  },
  "status" : 409
}

删除文档

DELETE /book/_doc/1

修改文档

语法:POST /{index}/type /{id}/_update

或者POST /{index}/_update/{id}

POST /book/_update/4
{
  "doc": {
     "info":"redis一个内存数据库,目前很流行"
  }
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "4",
  "_version" : 2,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  },
  "_seq_no" : 17,
  "_primary_term" : 3
}
GET /book/_doc/4
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "4",
  "_version" : 2,
  "_seq_no" : 17,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "redis",
    "info" : "redis一个内存数据库,目前很流行",
    "price" : 100
  }
}

更新只支持post请求

PUT /book/_update/4
{
  "doc": {
     "price": 200
  }
}
{
  "error" : "Incorrect HTTP method for uri [/book/_update/4?pretty=true] and method [PUT], allowed: [POST]",
  "status" : 405
}

并发修改文档

PUT /person/_doc/2?if_seq_no=4&if_primary_term=1
{
  "name":"ls2"
}

如果_seq_no不等于4或者_primary_term不等于1就会报错

{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[2]: version conflict, required seqNo [4], primary term [1]. current document has seqNo [5] and primary term [1]",
        "index_uuid" : "7CucaVfVTi-V5OvzmuXHgA",
        "shard" : "0",
        "index" : "person"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[2]: version conflict, required seqNo [4], primary term [1]. current document has seqNo [5] and primary term [1]",
    "index_uuid" : "7CucaVfVTi-V5OvzmuXHgA",
    "shard" : "0",
    "index" : "person"
  },
  "status" : 409
}

查询文档

  1. 根据id查找

    GET /book/_doc/2
    
    {
      "_index" : "book",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 2,
      "_seq_no" : 5,
      "_primary_term" : 3,
      "found" : true,
      "_source" : {
        "name" : "mysql",
        "info" : "mysql是最好用的数据库,而且还免费",
        "price" : 66
      }
    }
    
  2. 定制返回字段

    GET /book/_doc/4?_source_includes=name,info
    {
      "_index" : "book",
      "_type" : "_doc",
      "_id" : "4",
      "_version" : 2,
      "_seq_no" : 17,
      "_primary_term" : 3,
      "found" : true,
      "_source" : {
        "name" : "redis",
        "info" : "redis一个内存数据库,目前很流行"
      }
    }
    
    GET /book/_doc/4?_source_excludes=price
    {
      "_index" : "book",
      "_type" : "_doc",
      "_id" : "4",
      "_version" : 2,
      "_seq_no" : 17,
      "_primary_term" : 3,
      "found" : true,
      "_source" : {
        "name" : "redis",
        "info" : "redis一个内存数据库,目前很流行"
      }
    }
    
  3. 批量查询

    GET /_mget
    {
      "docs":[
        {"_id":1,"_index":"person"},
        {"_id":2,"_index":"book"}
      ]
    }
    
    {
      "docs" : [
        {
          "_index" : "person",
          "_type" : "_doc",
          "_id" : "1",
          "_version" : 2,
          "_seq_no" : 1,
          "_primary_term" : 1,
          "found" : true,
          "_source" : {
            "name" : "张三",
            "age" : 18
          }
        },
        {
          "_index" : "book",
          "_type" : "_doc",
          "_id" : "2",
          "_version" : 5,
          "_seq_no" : 15,
          "_primary_term" : 3,
          "found" : true,
          "_source" : {
            "name" : "mysql",
            "info" : "mysql是最好用的数据库,而且还免费"
          }
        }
      ]
    }
    
    
  4. 同一索引下批量查询

    类似与数据库的in效果

    GET /book/_mget
    {
      "docs":[
        {"_id":2},
        {"_id":4}
      ]
    }
    
    {
      "docs" : [
        {
          "_index" : "book",
          "_type" : "_doc",
          "_id" : "2",
          "_version" : 5,
          "_seq_no" : 15,
          "_primary_term" : 3,
          "found" : true,
          "_source" : {
            "name" : "mysql",
            "info" : "mysql是最好用的数据库,而且还免费"
          }
        },
        {
          "_index" : "book",
          "_type" : "_doc",
          "_id" : "4",
          "_version" : 2,
          "_seq_no" : 17,
          "_primary_term" : 3,
          "found" : true,
          "_source" : {
            "name" : "redis",
            "info" : "redis一个内存数据库,目前很流行",
            "price" : 100
          }
        }
      ]
    }
    
  5. 搜索写法

    GET /book/_search
    {
      "query":{
        "ids":{
          "values":[2,4]
        }
      }
    }
    
    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "book",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "name" : "mysql",
              "info" : "mysql是最好用的数据库,而且还免费"
            }
          },
          {
            "_index" : "book",
            "_type" : "_doc",
            "_id" : "4",
            "_score" : 1.0,
            "_source" : {
              "name" : "redis",
              "info" : "redis一个内存数据库,目前很流行",
              "price" : 100
            }
          }
        ]
      }
    }
    
  6. 批量操作 bulk

{ "action": { ...metadata... }}
{ "operation": { ...document... }}
{ "action": { ...metadata... }}
{ "operation": { ...document... }}
...

  • action 描述操作的元数据,包括index(索引文档)、delete(删除文档)、update(更新文档)等。
  • operation 包含具体的文档数据。

举例

POST /_bulk
{ "delete": { "_index": "test_index",  "_id": "5" }} 
{ "create": { "_index": "test_index",  "_id": "14" }}
{ "test_field": "test14" }
{ "update": { "_index": "test_index",  "_id": "2"} }
{ "doc" : {"test_field" : "bulk test"} }
{ "delete": { "_index": "test_index",  "_id": "5" }} 
 删除了test_index中id=5的文档
{ "create": { "_index": "test_index",  "_id": "14" }}
{ "test_field": "test14" }
往test_index里新增了一条数据
{ "update": { "_index": "test_index",  "_id": "2"} }
{ "doc" : {"test_field" : "bulk test"} }
更新test_index中id=2的数据

create:相当于强制创建 PUT /index/type/id/_create

update:执行的是局部更新partial update操作

每个操作互不影响。操作失败的行会返回其失败信息文章来源地址https://www.toymoban.com/news/detail-854462.html

到了这里,关于elasticsearch之crud的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • 全文搜索引擎 Elasticsearch详解

    Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据。Elasticsearch 在 Apache Lucene 的基础上开发而成,由 Elasticsearch N.V.(即现在的 Elastic)于 2010 年首次发布。Elasticsearch 以其简单的

    2023年04月22日
    浏览(35)
  • Elasticsearch:什么是搜索引擎?

    搜索引擎是一种软件程序或系统,旨在帮助用户查找存储在互联网或特定数据库中的信息。 搜索引擎的工作原理是对各种来源的内容进行索引和编目,然后根据用户的搜索查询向用户提供相关结果列表。 搜索引擎对于希望快速有效地查找特定信息的用户来说是有用的工具。

    2024年02月21日
    浏览(37)
  • 分布式搜索引擎ElasticSearch——深入elasticSearch

    聚合的分类 DSL实现Bucket聚合 DSL实现Metric聚合 RestAPI实现聚合 https://github.com/medcl/elasticsearch-analysis-pinyin DSL实现自动补全查询 Completion Suggester 修改酒店索引库数据结构 RestAPI实现自动补全查询 实现酒店搜索页面输入框的自动补全 数据同步思路分析 利用MQ实现mysql与elasticsearch数

    2024年01月17日
    浏览(40)
  • 搜索引擎ElasticSearch分布式搜索和分析引擎学习,SpringBoot整合ES个人心得

    Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,

    2024年02月04日
    浏览(63)
  • SpringBoot集成Elasticsearch搜索引擎

    Elasticsearch是一个基于Lucene的搜索引擎,它提供了实时、可扩展和可伸缩的搜索功能。Spring Boot是一个用于构建新Spring应用的起点,它旨在简化开发人员的工作,使其能够快速地构建可扩展的、可维护的应用程序。 在现代应用程序中,搜索功能是非常重要的。它可以帮助用户快

    2024年02月19日
    浏览(42)
  • SpringBoot 使用 Elasticsearch 搜索引擎

    作者:禅与计算机程序设计艺术 Spring Boot 是由 Pivotal 团队提供的一套用于开发基于 Spring 框架的应用的工具包。其主要目标是通过提供简单易用的starter包来简化开发流程。Spring Boot 极大的地方在于其依赖自动配置,可以很好的满足开发人员的开发需求。Spring Boot 提供了数据访

    2024年02月09日
    浏览(43)
  • 全文搜索引擎 Elasticsearch 入门使用

    目录 1、安装 2、基本概念 2.1 Node 与 Cluster 2.2 Index 2.3 Document  2.4 Type 3、新建和删除 Index 4、中文分词设置  5、数据操作  5.1 新增记录  5.2 查看记录   5.3 删除记录 5.4 更新记录  6、数据查询 6.1 返回所有记录 6.2 全文搜索  6.3 逻辑运算 7、参考链接 本文从零开始,讲解如何

    2024年02月09日
    浏览(39)
  • 搜索引擎 Elasticsearch 的三大坑

    ES 搜索引擎系列文章汇总: 一、别只会搜日志了,求你懂点原理吧 二、ES 终于可以搜到”悟空哥“了! 三、1W字|40 图|硬核 ES 实战 本文主要内容如下: 搜索引擎 现在是用得越来越多了,比如我们日志系统中用到的 ELK 就用到了 搜索引擎 Elasticsearch (简称 ES)。 那对于搜

    2024年02月01日
    浏览(34)
  • 开源的全文搜索引擎Elasticsearch

    Elasticsearch是一个开源的全文搜索引擎,可以实现快速、实时的数据搜索和分析。它是基于Apache Lucene的搜索引擎库开发而来,提供了一个分布式、多租户的全文搜索引擎平台,能够支持海量数据的实时检索、聚合分析和可视化展示。 Elasticsearch 的主要特点包括: 分布式架构:

    2024年02月08日
    浏览(43)
  • 分布式搜索引擎----elasticsearch

    目录 1、初识elasticsearch 1.1、什么是elasticsearch 1.2.ELK技术栈 2、正向索引和倒排索引 2.1、正向索引 2.2、倒排索引 2.3、正向索引和倒排索引的区别 3、elasticsearch中的概念理解 3.1、文档和字段 3.2、索引和映射 3.3、mysql与elasticsearch         elasticsearch是一款非常强大的开源搜索

    2024年02月11日
    浏览(47)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包