Elasticsearch 5.4 Documents API

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: 一单文档API1 Index API2 Get API3 Delete API4 Update API二多文档API1 Multi Get API2 Bulk API3 Delete By Q...

ELasticsearch文档的CRUD主要包括以下2个大的方面:单文档和多文档,翻译如下:

一、单文档API

1.1 Index API

写入文档,索引为twitter,type为tweet,id为1:

PUT twitter/tweet/1
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

返回结果:

{
  "_index": "twitter",
  "_type": "tweet",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

1.2 Get API

get api可以通过id查看文档:

GET twitter/tweet/0

查看文档是否存在:

HEAD twitter/tweet/0

1.3 Delete API

根据ID删除:

DELETE twitter/tweet/1

1.4 Update API

Update API允许通过脚本更新文档,更新操作会先读取文档,执行脚本,最后重新索引。更新操作意味着重新索引文档,当然执行更新操作不能关闭_source字段。
写入一条文档做测试:

PUT test/type1/1
{
    "counter" : 1,
    "tags" : ["red"]
}

把counter的值更新为5,执行脚本如下:

POST test/type1/1/_update
{
    "script" : {
        "inline": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    }
}

给tags增加一个值:

POST test/type1/1/_update
{
    "script" : {
        "inline": "ctx._source.tags.add(params.tag)",
        "lang": "painless",
        "params" : {
            "tag" : "blue"
        }
    }
}

通过ctx可以操作_index, _type, _id, _version, _routing, _parent, and _now(当前的timestamp)。

给文档增加一个新的字段:

POST test/type1/1/_update
{
    "script" : "ctx._source.new_field = \"value_of_new_field\""
}

删除一个字段:

POST test/type1/1/_update
{
    "script" : "ctx._source.remove(\"new_field\")"
}

脚本中还可以执行逻辑语句,以下脚本将会删除tag字段中含有green的文档。

POST test/type1/1/_update
{
    "script" : {
        "inline": "if (ctx._source.tags.contains(params.tag)) { ctx.op = \"delete\" } else { ctx.op = \"none\" }",
        "lang": "painless",
        "params" : {
            "tag" : "green"
        }
    }
}

也可以通过传递文档的一部分,新增的内容会合并到原始文档中,例如增加字段:

POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    }
}

如果同时又docscriptdoc会被忽略。
如果新传入的文档原始文档中已经存在,再次更新会被忽略,也就是覆盖一模一样的字段内容会被忽略。把detect_noop设为false,即使文档内容一样,也会执行。

POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    },
    "detect_noop": false
}

二、多文档API

2.1 Multi Get API

通过ID一次获取多个文档的方式:

GET _mget
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2"
        }
    ]
}

如果索引相同:

GET test/_mget
{
    "docs" : [
        {
            "_type" : "type",
            "_id" : "1"
        },
        {
            "_type" : "type",
            "_id" : "2"
        }
    ]
}

如果type相同:

GET test/type/_mget
{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}

简写:

GET test/type/_mget
{
    "ids" : ["1", "2"]
}

缺省type会返回索引下所有的type下的所有符合id的文档:

GET test/_mget
{
    "ids" : ["1", "1"]
}

如果文档不存在,使用使用upserts插入新文档:

POST test/type1/1/_update
{
    "script" : {
        "inline": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    },
    "upsert" : {
        "counter" : 1
    }
}

2.2 Bulk API

Bulk API可以批量插入:

POST _bulk
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

2.3 Delete By Query API

通过查询条件删除:


POST twitter/_delete_by_query
{
  "query": { 
    "match": {
      "message": "some message"
    }
  }
}

2.4 Update By Query API

通过查询更新文档:

POST twitter/_update_by_query
{
  "script": {
    "inline": "ctx._source.likes++",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}

2.5 Reindex API

reindex api用于从一个索引拷贝文档到另外一个索引,注意,mapping、setting中的分片数和副本数都不会被拷贝。

把索引twitter中的文档拷贝到new_twitter:

POST _reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}
相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
2月前
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。
|
存储 人工智能 自然语言处理
Elasticsearch Inference API增加对阿里云AI的支持
本文将介绍如何在 Elasticsearch 中设置和使用阿里云的文本生成、重排序、稀疏向量和稠密向量服务,提升搜索相关性。
462 14
Elasticsearch Inference API增加对阿里云AI的支持
|
12月前
|
存储 人工智能 API
(Elasticsearch)使用阿里云 infererence API 及 semantic text 进行向量搜索
本文展示了如何使用阿里云 infererence API 及 semantic text 进行向量搜索。
450 8
|
监控 API 索引
Elasticsearch集群使用 _cluster/health API
Elasticsearch集群使用 _cluster/health API
462 2
|
Unix API 索引
Elasticsearch集群使用 _cat/health API
Elasticsearch集群使用 _cat/health API
244 1
|
人工智能 自然语言处理 搜索推荐
Elasticsearch 开放 inference API 增加了对 Azure OpenAI 嵌入的支持
【6月更文挑战第8天】Elasticsearch 推出开放 inference API,支持 Azure OpenAI 嵌入,强化搜索和数据分析能力。此更新使用户能灵活集成 AI 技术,实现智能精准搜索。Azure OpenAI 的语言理解能力优化了用户查询处理,提升搜索相关性。示例代码显示了如何结合两者处理查询。该创新提升数据检索效率,适用于智能客服和推荐系统,但也带来数据安全和模型准确性等挑战。这标志着搜索和数据分析领域的智能化新阶段,期待更多创新应用。未来,我们需要持续探索和完善,以发挥技术的最大潜力。
156 3
|
存储 缓存 Java
掌握Elasticsearch集群参数查询API
掌握Elasticsearch集群参数查询API
|
8月前
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
1336 64
|
7月前
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
3294 0
|
存储 安全 数据管理
如何在 Rocky Linux 8 上安装和配置 Elasticsearch
本文详细介绍了在 Rocky Linux 8 上安装和配置 Elasticsearch 的步骤,包括添加仓库、安装 Elasticsearch、配置文件修改、设置内存和文件描述符、启动和验证 Elasticsearch,以及常见问题的解决方法。通过这些步骤,你可以快速搭建起这个强大的分布式搜索和分析引擎。
459 5