Elasticsearch7.6学习笔记1 Getting start with Elasticsearch

Elasticsearch7.6学习笔记1 Getting start with Elasticsearch 前言

权威指南中文只有2.x, 但现在es已经到7.6. 就安装最新的来学下.

安装

这里是学习安装, 生产安装是另一套逻辑.

win

es下载地址:

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-windows-x86_64.zip

kibana下载地址:

https://artifacts.elastic.co/downloads/kibana/kibana-7.6.0-windows-x86_64.zip

官方目前最新是7.6.0, 但下载速度惨不忍睹. 使用迅雷下载速度可以到xM.

bin\elasticsearch.bat bin\kibana.bat

双击bat启动.

docker安装

对于测试学习,直接使用官方提供的docker镜像更快更方便。

安装方法见: https://www.cnblogs.com/woshimrf/p/docker-es7.html

以下内容来自:

https://www.elastic.co/guide/en/elasticsearch/reference/7.6/getting-started.html

Index some documents 索引一些文档

本次测试直接使用kibana, 当然也可以通过curl或者postman访问localhost:9200.

访问localhost:5601, 然后点击Dev Tools.

Elasticsearch7.6学习笔记1 Getting start with Elasticsearch

新建一个客户索引(index)

PUT /{index-name}/_doc/{id}

PUT /customer/_doc/1 { "name": "John Doe" }

put 是http method, 如果es中不存在索引(index) customer, 则创建一个, 并插入一个数据, id为, name=John`.
如果存在则更新. 注意, 更新是覆盖更新, 即body json是什么, 最终结果就是什么.

返回如下:

{ "_index" : "customer", "_type" : "_doc", "_id" : "1", "_version" : 7, "result" : "updated", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 6, "_primary_term" : 1 }

_index 是索引名称

_type 唯一为_doc

_id 是文档(document)的主键, 也就是一条记录的pk

_version 是该_id的更新次数, 我这里已经更新了7次

_shards 表示分片的结果. 我们这里一共部署了两个节点, 都写入成功了.

在kibana上设置-index manangement里可以查看index的状态. 比如我们这条记录有主副两个分片.

Elasticsearch7.6学习笔记1 Getting start with Elasticsearch

保存记录成功后可以立马读取出来:

GET /customer/_doc/1

返回

{ "_index" : "customer", "_type" : "_doc", "_id" : "1", "_version" : 15, "_seq_no" : 14, "_primary_term" : 1, "found" : true, "_source" : { "name" : "John Doe" } }

_source 就是我们记录的内容

批量插入

当有多条数据需要插入的时候, 我们可以批量插入. 下载准备好的文档, 然后通过http请求导入es.

创建一个索引bank: 由于shards(分片)和replicas(副本)创建后就不能修改了,所以要先创建的时候配置shards. 这里配置了3个shards和2个replicas.

PUT /bank { "settings": { "index": { "number_of_shards": "3", "number_of_replicas": "2" } } }

文档地址: https://gitee.com/mirrors/elasticsearch/raw/master/docs/src/test/resources/accounts.json

下载下来之后, curl命令或者postman 发送文件请求过去

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"

每条记录格式如下:

{ "_index": "bank", "_type": "_doc", "_id": "1", "_version": 1, "_score": 0, "_source": { "account_number": 1, "balance": 39225, "firstname": "Amber", "lastname": "Duke", "age": 32, "gender": "M", "address": "880 Holmes Lane", "employer": "Pyrami", "email": "amberduke@pyrami.com", "city": "Brogan", "state": "IL" } }

在kibana monitor中选择self monitor. 然后再indices中找到索引bank。可以看到我们导入的数据分布情况。

Elasticsearch7.6学习笔记1 Getting start with Elasticsearch

可以看到, 有3个shards分在不同的node上, 并且都有2个replicas.

开始查询

批量插入了一些数据后, 我们就可以开始学习查询了. 上文知道, 数据是银行职员表, 我们查询所有用户,并根据账号排序.

类似 sql

select * from bank order by account_number asc limit 3

Query DSL

GET /bank/_search { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ], "size": 3, "from": 2 }

_search 表示查询

query 是查询条件, 这里是所有

size 表示每次查询的条数, 分页的条数. 如果不传, 默认是10条. 在返回结果的hits中显示.

from表示从第几个开始

返回:

{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1000, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "2", "_score" : null, "_source" : { "account_number" : 2, "balance" : 28838, "firstname" : "Roberta", "lastname" : "Bender", "age" : 22, "gender" : "F", "address" : "560 Kingsway Place", "employer" : "Chillium", "email" : "robertabender@chillium.com", "city" : "Bennett", "state" : "LA" }, "sort" : [ 2 ] }, { "_index" : "bank", "_type" : "_doc", "_id" : "3", "_score" : null, "_source" : { "account_number" : 3, "balance" : 44947, "firstname" : "Levine", "lastname" : "Burks", "age" : 26, "gender" : "F", "address" : "328 Wilson Avenue", "employer" : "Amtap", "email" : "levineburks@amtap.com", "city" : "Cochranville", "state" : "HI" }, "sort" : [ 3 ] }, { "_index" : "bank", "_type" : "_doc", "_id" : "4", "_score" : null, "_source" : { "account_number" : 4, "balance" : 27658, "firstname" : "Rodriquez", "lastname" : "Flores", "age" : 31, "gender" : "F", "address" : "986 Wyckoff Avenue", "employer" : "Tourmania", "email" : "rodriquezflores@tourmania.com", "city" : "Eastvale", "state" : "HI" }, "sort" : [ 4 ] } ] } }

返回结果提供了如下信息

took es查询时间, 单位是毫秒(milliseconds)

timed_out search是否超时了

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpspyf.html