ElasticSearch是一个基于Lucene的搜索服务器,它提供一个分布式多用户的全文搜索引擎,基于REST风格的web接口

常用术语

  • 文档Document:es存储的数据是文档型的,一条数据对应一篇文档,即可以理解为mysql中的一行数据,一个文档可以有多个字段,即mysql数据库一行可以有多个列
  • 索引Index:有具有相同字段的文档列表组成,一个文档的集合,对应mysql中的table。在es6.0之前一个index下可以创建多种type,有人将index比作database,type比作table,es6.0之后,index下只能创建一种类型的type,所以将index比作table会更好理解一点
  • 节点Node:一个es的运行实例,是集群的构成单元
  • 集群Cluster:由一个或多个节点组成

Document

Document其实就是一个Json Object,由字段(Field)组成,常见的数据类型如下:

  • 字符串:text、keyword
  • 数值型:log、integer、short、byte、double、float、half_float、scaled_float
  • 布尔:boolean
  • 日期:date
  • 二进制:binary
  • 范围类型:integer_range、float_range、long_range、double_range、date_range

每一个document都有一个唯一的id标识,即对应mysql中的主键,目前可以自行指定,如未指定es将自动生成

在每一个document中都有一些元数据(document metadata),主要用来标注文档的相关信息:

  • _index:文档所在的索引名
  • _type:文档所在的类型名
  • _id:文档唯一id
  • _uid:组合id,由 _type 和 _id组成 (6.x _type不再起作用,同 _id 一样)
  • _source:文档的原始Json数据
  • _all:整合所有字段内容到该字段,默认禁用

Index

Index在数据库类似Table,Index中存储的是具有相同结构的Document,每个Index都有自己的mapping定义,用于定义字段名和类型

一个集群中可以有多个Index,比如:nginx的access.log可以按照日期每天生成一个Index来存储

REST API

ElasticSearch对外提供REST API:

  • URI指定资源,如Index、Document等
  • HTTP Method指明资源操作类型,如GET、POST、PUT、DELETE等

常用交互方式:

  • Curl命令行
  • Kibana DevTools

Index API

创建索引

[root@master ~]# curl -X PUT 'http://10.244.2.18:9200/test_index?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test_index"
}

查看索引

[root@master ~]# curl -X GET 'http://10.244.2.18:9200/_cat/indices?pretty'
green open filebeat-7.2.0-2021.01.20-000001 zjUeoHT4TVSSuKaLwE71Gg 1 1 4037 0 5.2mb 2.5mb
green open .kibana_1 9ndxc6KESMW2OAzwMSwcIw 1 1 5 0 147kb 70.6kb
green open .kibana_task_manager P_NssAqSSwGdUUXgVH5x7A 1 1 2 0 108.3kb 53.7kb
green open test_index 7lPjFgWqRWOtZX3YMZ58ew 1 1 0 0 460b 230b

删除索引

[root@master ~]# curl -X DELETE 'http://10.244.2.18:9200/test_index?pretty'
{
"acknowledged" : true
}

Document API

创建文档

如果索引不存在,es会自动创建对应的index和type


# test_index/doc/1 表示 索引名称/type/id
curl -H 'Content-Type: application/json' -XPUT http://10.244.2.18:9200/test_index/doc/1 -d '
{
"name":"zhangsan",
"age":20
}'

不指明id创建document

若不指明id,es会自动创建id

curl -H 'Content-Type: application/json' -XPOST http://10.244.2.18:9200/test_index/doc -d '
{
"name":"lisi",
"age":21
}'

查询文档

curl -XGET http://10.244.2.18:9200/test_index/doc/1?pretty

查询所有文档

curl -XGET http://10.244.2.18:9200/test_index/doc/_search?pretty

根据条件查询文档

curl -H 'Content-Type: application/json' -XGET http://10.244.2.18:9200/test_index/doc/_search?pretty -d '
{
"query":{
"term":{
"_id":1
}
}
}'

修改文档

# 将test_index(index)下doc(type)的id为1的数据 name字段的值 修改为 xiaohong
curl -H 'Content-Type: application/json' -XPOST http://10.244.2.18:9200/test_index/doc/1/_update?pretty -d '
{
"doc":{
"name":"xiaohong"
}
}'

在文档中添加新的字段

curl -H 'Content-Type: application/json' -XPOST http://10.244.2.18:9200/test_index/doc/1/_update?pretty -d '
{
"doc":{
"sex":"girl"
}
}'

删除文档

curl -XDELETE http://10.244.2.18:9200/test_index/doc/1?pretty

根据条件删除对应文档

# 删除满足age:20的所有文档数据
curl -H 'Content-Type: application/json' -XPOST http://10.244.2.18:9200/test_index/doc/_delete_by_query?pretty -d '
{
"query":{
"match":{
"age":20
}
}
}'

批量写入API

es允许批量对文档数据进行操作,使用 _bulk 完成下面操作

action_type有4种index(创建文档)、update(更新文档)、create(创建文档)、delete(删除文档)

index和create区别在于,当文档已经存在时,index操作会覆盖文档,create则报错(create只负责创建文档)

curl -H 'Content-Type: application/json' -XPOST http://10.244.2.18:9200/_bulk?pretty -d '
{ "index" : { "_index" : "test_index", "_type" : "doc", "_id" : "1" } }
{ "name" : "mayun" , "age" : 51 }
{ "update" : { "_index" : "test_index", "_type" : "doc", "_id" : "1" } }
{ "doc" : { "age" : 52 }}
'
curl -H 'Content-Type: application/json' -XPOST http://10.244.2.18:9200/_bulk?pretty -d '
{ "create" : { "_index" : "test_index", "_type" : "doc", "_id" : "2" } }
{ "name" : "mahuateng" , "age" : 50 }
{ "index" : { "_index" : "test_index", "_type" : "doc", "_id" : "3" } }
{ "name" : "leijun" , "age" : 55 }
{ "delete" : { "_index" : "test_index", "_type" : "doc", "_id" : "3" } }
'

批量查询API

es允许一次查询多个文档,使用 _mget 完成下面操作

curl -H 'Content-Type: application/json' -XGET http://10.244.2.18:9200/_mget?pretty -d '
{
"docs": [
{ "_index" : "test_index", "_type" : "doc", "_id" : "1" },
{ "_index" : "test_index", "_type" : "doc", "_id" : "2" }
]
}
'