中文文档:

https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html

查看健康状态

curl -X GET 127.0.0.1:9200/_cat/health?v

查询当前es集群中所有的indices

curl -X GET 127.0.0.1:9200/_cat/indices?v

推荐使用Kibana进行DSL编写,有代码提示与纠正(提示给跟我一样的小白)

创建索引并配置:

number_of_shards为数据分片,默认值为5

number_of_replicas为数据备份数,如果只有一台机器,建议设置为0,避免索引一直处于yellow状态

注:该设置创建索引后无法修改(我记得是这样,有错望纠正)

PUT /index_name/_settings
{
"number_of_shards": 3,
"number_of_replicas": 0
}

定义字段的类型

text:文本类型(会被es分词器进行分词)可以保存字符串也可以保存数组
keyword:文本类型(不被分词) 可以保存字符串也可以保存数组
scaled_float:浮点型数据类型,scaling_factor用于精度限制
date:时间类型,可用format约束时间格式(epoch_millis为时间戳)

例子:

查看mapping

PUT /index_name/_doc/_mapping?pretty

{
"_doc":{
"properties":{
"my_id":{
"type":"long"
},
"content":{
"type":"text"
},
"money":{
"type":"scaled_float",
"scaling_factor":100
},
"customer":{
"type":"keyword"
},
"sync":{
"type":"boolean"
},
"create_time":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}

保存数组

textkeywor 类型可以保存字符串也可以保存数组

"properties": {
"fofa_rules": {
"type": "keyword"
}

创建mapping

curl -H "Content-Type:application/json" -X PUT 127.0.0.1:9200/user1 -d '
{
"mappings":{
"person":{
"properties":{
"address":{
"type":"text",
"fields":{
"keyword":{
"ignore_above":2048,
"type":"keyword"
}
}
},
"name":{
"type":"text",
"fields":{
"keyword":{
"ignore_above":256,
"type":"keyword"
}
}
}
}
}
}
}' | jq .

存字符串

修改值为字符串

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person/1/_update -d'
{
"doc":{
"name":"lisi",
"address":"北京"
}
}'

curl -X GET 127.0.0.1:9200/user/person/_search?q=_id:1 | jq .

查询结果:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "person",
"_id": "1",
"_score": 1,
"_source": {
"name": "lisi",
"age": 9,
"married": false,
"address": "北京"
}
}
]
}
}

存数组

修改值为数组

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person/1/_update -d'
{
"doc":{
"name":["lisi","wangwu"],
"address":["安徽","北京"]
}
}'

curl -X GET 127.0.0.1:9200/user/person/_search?q=_id:1 | jq .

查询结果

{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "person",
"_id": "1",
"_score": 1,
"_source": {
"name": [
"lisi",
"wangwu"
],
"age": 9,
"married": false,
"address": [
"安徽",
"北京"
]
}
}
]
}
}

创建索引

curl -X PUT 127.0.0.1:9200/www

删除索引

curl -X DELETE 127.0.0.1:9200/www

获取mapping

curl -X GET 127.0.0.1:9200/user/_mapping?pretty

返回参数

{
"user" : {
"mappings" : {
"person" : {
"properties" : {
"age" : {
"type" : "long"
},
"married" : {
"type" : "boolean"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"query" : {
"properties" : {
"match" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
}
}
}
}

创建mapping

注意:

  1. 这里只要上面获取mapping返回值里的mapping值
  2. 要创建的索引必需不存在
curl -H "Content-Type:application/json" -X PUT 127.0.0.1:9200/user -d '
{
"mappings":{
"person":{
"properties":{
"age":{
"type":"long"
},
"married":{
"type":"boolean"
},
"name":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
}
}
}
}
}' | jq .

添加字段

user引索的person类型添加字段

curl -H "Content-Type:application/json" -X PUT 127.0.0.1:9200/user/_mapping/person -d '
{
"properties":{
"created_at":{
"format":"YYYY-MM-dd HH:mm:ss",
"type":"date"
},
"it":{
"type":"boolean"
},
"is_scan":{
"type":"long"
},
"address":{
"type":"text",
"fields":{
"keyword":{
"ignore_above":256,
"type":"keyword"
}
}
}
}
}'

设置默认值

参考下面文档

https://blog.csdn.net/huangzhen__/article/details/104769018?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2aggregatepagefirst_rank_ecpm_v1~rank_v31_ecpm-1-104769018.pc_agg_new_rank&utm_term=Mapping%E8%AE%BE%E7%BD%AE%E9%BB%98%E8%AE%A4%E5%80%BC&spm=1000.2123.3001.4430

插入记录

post的方式,会自动生成id

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person -d '
{
"name": "dsb",
"age": 9000,
"married": true
}'

输出:

{
"_index":"user",
"_type":"person",
"_id":"55GiOH4BfTVguARX3JmZ",
"_version":1,
"result":"created",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":0,
"_primary_term":1
}

也可以使用PUT方法,但是需要传入id

curl -H "Content-Type:application/json" -X PUT 127.0.0.1:9200/user/person/4 -d '
{
"name": "sb",
"age": 9,
"married": false
}'

检索

Elasticsearch的检索语法比较特别,使用GET方法携带JSON格式的查询条件。

以下带 | jq .需要安装jq这是格式化json显示

全检索:

curl -X GET 127.0.0.1:9200/user/person/_search | jq .

输出结果:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "user",
"_type": "person",
"_id": "55GiOH4BfTVguARX3JmZ",
"_score": 1,
"_source": {
"name": "dsb",
"age": 9000,
"married": true
}
},
{
"_index": "user",
"_type": "person",
"_id": "4",
"_score": 1,
"_source": {
"name": "sb",
"age": 9,
"married": false
}
}
]
}
}

按条件检索:

get查询

curl -X GET 127.0.0.1:9200/user/person/1 | jq .

查询结果:

{
"_index": "user",
"_type": "person",
"_id": "1",
"_version": 4,
"found": true,
"_source": {
"name": "lisi",
"age": 9,
"married": false,
"address": "北京"
}
}

name=sb的记录

curl -X GET 127.0.0.1:9200/user/person/_search?q=name:sb | jq .

返回参数:

{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "person",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "sb",
"age": 9,
"married": false
}
}
]
}
}

age=9的记录

curl -X GET 127.0.0.1:9200/user/person/_search?q=age:9 | jq .

post查询

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person/_search -d '
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "sb"
}
}
]
}
}
}' | jq .

输出结果:

{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "user",
"_type": "person",
"_id": "4",
"_score": 0.6931472,
"_source": {
"name": "sb",
"age": 9,
"married": false
}
}
]
}
}

更多条件

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person/_search -d '
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gt": "10"
}
}
}
]
}
},
"from": 0,
"size": 10,
"sort": {
"age": {
"order": "desc"
}
},
"aggs": {}
}' | jq .

返回结果:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "user",
"_type": "person",
"_id": "55GiOH4BfTVguARX3JmZ",
"_score": null,
"_source": {
"name": "dsb",
"age": 9000,
"married": true
},
"sort": [
9000
]
},
{
"_index": "user",
"_type": "person",
"_id": "0ZHGOH4BfTVguARXXZoF",
"_score": null,
"_source": {
"name": "sbs",
"age": 6000,
"married": true
},
"sort": [
6000
]
}
]
}
}

修改

指定id修改

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person/4/_update -d'
{
"doc":{
"name":"11"
}
}'

返回结果:

{
"_index":"user",
"_type":"person",
"_id":"4",
"_version":2,
"result":"updated",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":2,
"_primary_term":1
}

按条件修改

把所有age>1的所有记录的married修改为true

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person/_update_by_query -d'
{
"script":{
"source":"ctx._source.married=params.married",
"params":{
"married":true
}
},
"query":{
"bool":{
"must":[
{
"range":{
"age":{
"gt":"1"
}
}
}
]
}
}
}'

把所有age>1的所有记录的age修改为12

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person/_update_by_query -d'
{
"script":{
"source":"ctx._source.age=12"
},
"query":{
"bool":{
"must":[
{
"range":{
"age":{
"gt":"1"
}
}
}
]
}
}
}'

把所有name=sbs的所有记录的age修改为38

{
"script":{
"source":"ctx._source.age=38"
},
"query":{
"bool":{
"must":[
{
"match":{
"name":"sbs"
}
}
]
}
}
}

删除

删除指定id的记录

curl -H "Content-Type:application/json" -X DELETE 127.0.0.1:9200/user/person/4

按条件删除

curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user/person/_delete_by_query -d'
{
"query":{
"bool":{
"must":[
{
"match":{
"name":"sb"
}
}
]
}
}
}'

删除所有数据

# user和product索引里类型为_doc的删除所有数据
curl -H "Content-Type:application/json" -X POST 127.0.0.1:9200/user,product/_doc/_delete_by_query -d'
{
"query":{
"bool":{
"must":[
{
"match_all":{ }
}
]
}
}
}'

06 curl 操作elasticsearch的CRUD的更多相关文章

  1. Curl操作Elasticsearch的常用方法

    Elasticsearch对于文档操作,提供了以下几种API,本文就说明如何使用curl方式来调用这些API. API种类 单文档操作API 1.* Index API 索引文档 * 为文档创建索引 ...

  2. elasticsearch(5) curl 操作elasticsearch

    创建索引之前可以对索引做初始化操作, 比如指定shards数量以及replicas的数量.     library为索引的名称 CURL -XPUT 'http://192.168.1.10:9200 ...

  3. window下使用curl操作elasticsearch

    1.下载curlzip,https://curl.haxx.se/download.html; 2.解压,在bin文件夹中找到curl.exe,右键“以管理员身份运行”,cmd e: 换盘符:出现E: ...

  4. 使用Sense操作ElasticSearch CRUD

    安装完成之后,我们该开始学习关于ElasticSearch最基本的CURD操作了. ElasticSearch作为一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,其接口也 ...

  5. ElasticSearch第二步-CRUD之Sense

    ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...

  6. 使用curl命令操作elasticsearch

    使用curl命令操作elasticsearch 大岩不灿 发表于 2015年4月25日 浏览 7,426 次 第一:_cat系列_cat系列提供了一系列查询elasticsearch集群状态的接口.你 ...

  7. 学习用Node.js和Elasticsearch构建搜索引擎(3):使用curl命令操作elasticsearch

    使用Elasticsearch不免要提到curl工具,curl是利用URL语法在命令行方式下工作的开源文件传输工具.官网地址:https://curl.haxx.se/ 因为elasticsearch ...

  8. ElasticSearch之CURL操作(有空再去整理)

    https://www.cnblogs.com/jing1617/p/8060421.html ElasticSearch之CURL操作 CURL的操作    curl是利用URL语法在命令行方式下工 ...

  9. Go操作Elasticsearch

    文章转自   Elasticsearch Elasticsearch 下载 https://www.elastic.co/cn/start 运行 解压后cd到解压目录 ./bin/elasticsea ...

  10. GO学习-(31) Go语言操作Elasticsearch

    Elasticsearch 本文简单介绍了ES.Kibana和Go语言操作ES. Elasticsearch 介绍 Elasticsearch(ES)是一个基于Lucene构建的开源.分布式.REST ...

随机推荐

  1. CI和自动化测试的结合(jenkins的搭建和使用)

    CI持续集成是一种思想,具体实现是通过jenkins持续集成工具去实现的. Jenkins的安装和配置(war包启动) 安装jenkins的方式有两种: 方式一:通过下载安装包安装,jenkins.m ...

  2. 企业数据清洗项目实践day1

    今天先把国标excel表的数据在Python里转化成了字典类型, 暂时定共分为四层,层层分类. 代码 1 def std_excel(): 2 dict={"A":{"0 ...

  3. 测试开发之系统篇-Docker容器安装

    前面文章我们讲到,容器是运行在宿主机上的一个进程,多个容器之间使用同一个宿主机上的操作系统内核.此处以Ubuntu20.04系统为例,介绍Docker容器引擎的安装过程. 安装 安装依赖. sudo ...

  4. https安全性 带给im 消息加密的启发

    大家好,我是蓝胖子,在之前# MYSQL 是如何保证binlog 和redo log同时提交的?这篇文章里,我们可以从mysql的设计中学会如何让两个服务的调用逻辑达到最终一致性,这也是分布式事务实现 ...

  5. 区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新

    目录 简介 什么是区块链 区块链不是什么 区块链的基础:密码学 区块链的基础:分布式系统和共识机制 超级账本Hyperledger 以太坊 Libra 比特币 总结 简介 区块链是一种防篡改的共享数字 ...

  6. OpenHarmony 社区运营报告(2023 年 10 月)

      ● 截至2023年10月,OpenHarmony社区共有51家共建单位,累计超过6200名贡献者产生24.2万多个PR,2.3万多个Star,6.1万多个Fork,59个SIG. ● OpenHa ...

  7. Git 安全远程访问:SSH 密钥对生成、添加和连接步骤解析

    使用 SSH 密钥对的 Git 安全远程访问:生成.添加和连接 SSH(Secure Shell)是一种用于安全远程访问的协议,它提供了加密通信和身份验证机制.在使用 SSH 连接到远程 Git 存储 ...

  8. 全面的开发者文档和用户目标解析:API 文档指南和开发者旅程

    开发者文档 开发者文档,也称为 API 文档,是一种专门针对软件开发人员的技术写作形式.这种类型的文档通常包括 API 的技术规范.代码注释.软件设计和架构以及软件开发中涉及的其他详细技术描述.开发者 ...

  9. Godot Label样式 Textrue纹理,实现样式修改,背景填充

    目录 前言 运行环境 新建项目 Style 样式讲解 StyleBoxEmpty:普通样式 StyleBoxTexture:字体样式 StyleBoxFlat:填充样式 StyleBoxLine:行样 ...

  10. mysql 重新整理——逻辑架构[二]

    前言 在此简洁逻辑架构,mysql的基础逻辑架构其实和sql server很像的. 正文 首先是客户端,发起了连接. 然后呢连接池后那一块,就是有分析器的那一块,那一块是干啥的呢. 其实我们写的语句呢 ...