Elasticsearch(ES)数据写入和查询

作者介绍:简历上没有一个精通的运维工程师。请点击上方的蓝色《运维小路》关注我,下面的思维导图也是预计更新的内容和当前进度(不定时更新)。

图片

中间件,我给它的定义就是为了实现某系业务功能依赖的软件,包括如下部分:

Web服务器

代理服务器

ZooKeeper

Kafka

RabbitMQ

Hadoop HDFS

Elasticsearch ES (本章节)

上个小节我们介绍了分片和副本,并且通过命令创建索引,在创建索引的时候定义了分片和副本,但是我们并没有向索引里面写入数据,今天我们就来介绍如何向Elasticsearch(ES)里面写入数据。

数据写入

自动生成ID

这里如果索引不存在,则会自动创建索引(按照默认的规则定义分片和副本,1分片1副本),--d后面数据就是要写入的数据。

curl -X POST "http://localhost:9200/your_index/_doc" \
-H "Content-Type: application/json" \
-d '{
  "field1": "value1",
  "field2": 100,
  "timestamp": "2023-10-01T12:00:00"
}'

手工指定ID

这个就是我们前面讲解过的指定id,这个id在内容之外的。​​​​​​​

curl -X PUT "http://localhost:9200/your_index/_doc/1" \
-H "Content-Type: application/json" \
-d '{
  "field1": "value_for_id_1",
  "field2": 200
}'

从文件写入数据

[root@localhost ~]# cat bulk_data.txt 
{ "index": { "_index": "your_index", "_id": "2" } }
{ "field1": "value1", "field2": 100 }
{ "create": { "_index": "your_index", "_id": "3" } }
{ "field1": "value2", "field2": 200 }



curl -X POST "http://localhost:9200/_bulk" \
-H "Content-Type: application/x-ndjson" \
--data-binary @bulk_data.txt

​​​​​​​

数据查询

模糊查询

注:这个方式只少量测试数据,大量的数据查询需要加入更多的条件,这个涉及到Mapping(映射)。下面的数据也就是我们前面通过3种方式插入的数据。​​​​​​​

[root@localhost ~]# curl -X GET "http://192.168.31.172:9200/your_index/_search?pretty"
{
  "took" : 121,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "your_index",
        "_type" : "_doc",
        "_id" : "EYIrdZgBLdTDE3X6KcYS",  #随机生成did
        "_score" : 1.0,
        "_source" : {                 #source里面才是我们写入的内容
          "field1" : "value1",
          "field2" : 100,
          "timestamp" : "2023-10-01T12:00:00"
        }
      },
      {
        "_index" : "your_index",
        "_type" : "_doc",
        "_id" : "2",                 #从文件里面读取写入es的数据
        "_score" : 1.0,
        "_source" : {
          "field1" : "value1",
          "field2" : 100
        }
      },
      {
        "_index" : "your_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "field1" : "value2",
          "field2" : 200
        }
      },
      {
        "_index" : "your_index",
        "_type" : "_doc",   
        "_id" : "1",             #指定的es的id
        "_score" : 1.0,
        "_source" : {
          "field1" : "value_for_id_1",
          "field2" : 200
        }
      }
    ]
  }
}

虽然通过curl命令也很容易实现对ES的写入和查询,但是实际情况下我们几乎不会不会使用这个方式写入数据,查询通过curl查询命令在运维层面可能使用会略多一点。

字段过滤查询

虽然这个查询和上面的查询结构是一样的,但是这个是带有查询条件的。​​​​​​​

curl -X GET "http://localhost:9200/your_index/_search?pretty" \
-H "Content-Type: application/json" \
-d '{
  "_source": ["field1", "field2"],  # 只返回指定字段
  "size": 5,                       # 返回5条文档
  "query": { "match_all": {} }      # 匹配所有文档
}'

运维小路

一个不会开发的运维!一个要学开发的运维!一个学不会开发的运维!欢迎大家骚扰的运维!

关注微信公众号《运维小路》获取更多内容。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值