elasticsearch使用
大约 8 分钟
安装elasticsearch
参考 部署-linux-centOs-大数据基础配置
为什么要用es查询?因为又快又好
为什么快?抱歉,这不是本篇的重点,请自行百度
首先,需要对es有一个初步的概念,方便后续进行学习:mysql和es的比较
MySQL | Elasticsearch | 说明 |
---|---|---|
Table | Index | 索引(index),就是文档的集合,类似数据库的表(table) |
Row | Document | 文档(Document),就是一条条的数据,类似数据库中的行(Row),文档都是JSON格式 |
Column | Field | 字段(Field),就是JSON文档中的字段,类似数据库中的列(Column) |
Schema | Mapping | Mapping(映射)是索引中文档的约束,例如字段类型约束。类似数据库的表结构(Schema) |
SQL | DSL | DSL是elasticsearch提供的JSON风格的请求语句,用来操作elasticsearch,实现CRUD |
然后,需要知道es的数据类型有哪些
类型 | 关键字 | 说明 |
---|---|---|
字符串 | text | 存储长文本数据,支持分词,搜索时支持模糊匹配 |
字符串 | keyword | 存储短文本数据,不支持分词,搜索时支持精确匹配 |
整型 | long | 64位整数 |
整型 | integer | 32位整数 |
整型 | short | 16位整数 |
整型 | byte | 8位整数 |
浮点型 | double | 64位浮点数 |
浮点型 | float | 32位浮点数 |
浮点型 | half_float | 16位浮点数 |
浮点型 | scaled_float | 缩放类型的的浮点数, 比如price字段只需精确到分, 57.34缩放因子为100, 存储结果为5734 |
布尔型 | boolean | 布尔值 |
日期型 | date | 日期类型,精确到毫秒 |
二进制 | binary | 二进制数据 |
范围型 | range | 范围类型,比如年龄范围,价格范围 |
数组类型 | array | 直接使用[]定义即可,数组中所有的值必须是同一种数据类型, 不支持混合数据类型的数组 |
对象类型 | object | 对象可以有自己独立的字段 |
嵌套型 | nested | 嵌套类型是对象数据类型的一个特例, 可以让array类型的对象被独立索引和搜索. |
地理位置型 | geo_point | 地理位置类型, 可以精确到经纬度 |
地理形状型 | geo_shape | 地理形状类型, 可以精确到多边形, 线, 点等,使用较少 |
IP型 | ip | IP地址类型 |
计数数据型 | counter | 计数数据类型, 只能存储整数, 存储的数据会自动累加, 默认值是0 |
这里我们使用常用的数据类型来进行操作 字符串,数值型,复杂类型和GEO地理位置类型
初步使用
让我们从创建一个基本的Index,插入一条基本的数据,进行一次基本的查询开始
#创建一个index 你可以理解为mysql创建了一张表,内含一个字段,类型为long
PUT /test_index
{
"mappings": {
"properties": {
"phone_number": {
"type": "long"
}
}
}
}
#返回结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_index"
}
#插入一条数据
POST /test_index/_doc/
{
"phone_number": "13055554678"
}
#返回结果
{
"_index": "test_index",
"_id": "UzD4pJABg7TU77-R0-IB",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
#进行一次查询
#精准查询
POST /test_index/_search
{
"query": {
"term": {
"phone_number": 13055554678
}
}
}
#返回结果
{
"took": 67,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_id": "UzD4pJABg7TU77-R0-IB",
"_score": 1,
"_source": {
"phone_number": "13055554678"
}
}
]
}
}
#无法命中的查询
POST /test_index/_search
{
"query": {
"term": {
"phone_number": 13055550000
}
}
}
#返回结果
{
"took": 30,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
我们已经完成es的基础操作了!接下来我们进一步了解es的具体操作
索引
索引操作主要包含创建索引,删除索引,修改索引,查看索引等操作
查看索引
#查看所有索引
GET *
#数据过长在此不作展示
#查看指定索引信息
GET /test_index
#返回结果
{
"test_index": {
"aliases": {},
"mappings": {
"properties": {
"phone_number": {
"type": "long"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "test_index",
"creation_date": "1720754542881",
"number_of_replicas": "1",
"uuid": "apkf0ijCQcq6Na9kpulxog",
"version": {
"created": "8500010"
}
}
}
}
}
#查看指定索引字段
GET /test_index/_mapping
#返回结果
{
"test_index": {
"mappings": {
"properties": {
"phone_number": {
"type": "long"
}
}
}
}
}
创建索引
#创建索引
PUT /test_index2
{
# 索引设置 不熟悉可以先默认
"settings": {
"index": {
"number_of_shards": 1, # 分片数量设置为1,默认为5
"number_of_replicas": 1 # 副本数量设置为1,默认为1
}
},
# 映射配置
"mappings": {
"dynamic": false, # 动态映射配置 不熟悉可以默认
# 字段属性配置
"properties": {
"id": {
"type": "integer" # 表示字段id,类型为integer
},
"name": {
"type": "text",
"analyzer": "ik_max_word", # 存储时的分词器 需要安装,没有就不要这个字段
"search_analyzer": "ik_smart" # 查询时的分词器 需要安装,没有就不要这个字段
},
"createAt": {
"type": "date" #时间
}
}
}
}
#返回结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_index2"
}
新增字段
#新增字段
PUT /test_index2/_mapping
{
"properties": {
"bbj": {
"type": "keyword"
}
}
}
#返回结果
{
"acknowledged": true
}
#校验
GET /test_index2/_mapping
{
"test_index2": {
"mappings": {
"dynamic": "false",
"properties": {
"bbj": {
"type": "keyword"
},
"createAt": {
"type": "date"
},
"id": {
"type": "integer"
},
"name": {
"type": "text"
}
}
}
}
}
删除字段
ElasticSearch不支持直接删除字段,我们可以新建一个索引>导入数据>删除索引>重命名索引方式进行
创建新索引
#先建立新索引
PUT /test_index2_v2
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
},
"mappings": {
"dynamic": false,
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text"
},
"createAt": {
"type": "date"
}
}
}
}
#返回结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_index2_v2"
}
索引数据导入
#导入数据
POST /_reindex
{
"source": {
"index": "test_index2"
},
"dest": {
"index": "test_index2_v2"
},
"script": {
"source": "ctx._source.remove('createAt')"
}
}
#返回结果
{
"took": 38,
"timed_out": false,
"total": 0,
"updated": 0,
"created": 0,
"deleted": 0,
"batches": 0,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
#由于测试没有数据,所以数据为0
#这里可以进行数据验证
GET /test_index2_v2/_search
删除旧索引
DELETE /test_index2
#返回结果
{
"acknowledged": true
}
新索引重命名为旧索引名
PUT /test_index2_v2/_aliases
{
"actions": [
{
"add": {
"index": "test_index2_v2",
"alias": "test_index2"
}
}
]
}
#返回结果
{
"acknowledged": true
}
删除索引
DELETE /test_index2
#返回结果
{
"acknowledged": true
}
记录
新增记录
#插入数据
POST /test_index2/_doc
{
"id": "2016113135",
"name": "bjtang",
"createAt": "1999-01-01"
}
#返回结果
{
"_index": "test_index2_v2",
"_id": "VDBQtJABg7TU77-R5OJq",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
#插入数据带ID
POST /test_index2/_doc/2
{
"id": "2016113136",
"name": "zc",
"createAt": "1998-01-01"
}
#返回结果
{
"_index": "test_index2_v2",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
#可以看到,如果在插入时未指定ID,则es会自动生成ID
进阶操作 使用logstash进行新增记录
删除记录
#直接根据ID删除
DELETE /test_index2/_doc/2
#返回结果
{
"_index": "test_index2_v2",
"_id": "2",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
#根据条件删除
POST /test_index2/_delete_by_query
{
"query":{
"match":{
"name":"bjtang"
}
}
}
#返回结果
{
"took": 44,
"timed_out": false,
"total": 1,
"deleted": 1,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
更新记录
#数据都被删除了,先添加示例数据
POST /test_index2/_doc
{
"id": "2016113135",
"name": "bjtang",
"createAt": "1999-01-01"
}
#随机的ID
VTBltJABg7TU77-RDOI3
更新或新建文档
#更新指定的文档,不存在即是新增文档,也是新增文档的一种
PUT /test_index2/_doc/VTBltJABg7TU77-RDOI3
{
"name":"bjtang3"
}
#返回结果
{
"_index": "test_index2_v2",
"_id": "VTBltJABg7TU77-RDOI3",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}
更新指定文档
#更新指定的文档,不存在即报错
POST /test_index2/_update/VTBltJABg7TU77-RDOI3
{
"doc": {
"name":"bjtang2"
}
}
#返回结果
{
"_index": "test_index2_v2",
"_id": "VTBltJABg7TU77-RDOI3",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1
}
#### 批量更新
```sh
#根据查询范围更新文档
POST test_index2/_update_by_query
{
"script": {
"source": "ctx._source.name ='bjtang' ",
"lang": "painless"
},
"query": {
"term": {
"doc.name": "bjtang"
}
}
}
查询记录
查询记录前先补充一些测试数据
POST /test_index2/_doc
{
"id": "2016113136",
"name": "TTT",
"createAt": "1999-01-01"
}
POST /test_index2/_doc
{
"id": "2016113138",
"name": "FFF",
"createAt": "1999-01-01"
}
POST /test_index2/_doc
{
"id": "2016113139",
"name": "GGG",
"createAt": "1999-01-01"
}
根据ID查询记录
GET /test_index2/_doc/VTBltJABg7TU77-RDOI3
#返回结果
{
"_index": "test_index2_v2",
"_id": "VTBltJABg7TU77-RDOI3",
"_version": 6,
"_seq_no": 12,
"_primary_term": 1,
"found": true,
"_source": {
"name": "bjtang334",
"doc": {
"name": "bjtang"
}
}
}
条件精准查询
#根据名称精准查询
POST /test_index2/_search
{
"query": {
"match": {
"name": "bjtang334"
}
}
}
#返回结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2039728,
"hits": [
{
"_index": "test_index2_v2",
"_id": "VTBltJABg7TU77-RDOI3",
"_score": 1.2039728,
"_source": {
"name": "bjtang334",
"doc": {
"name": "bjtang"
}
}
}
]
}
}
条件匹配查询
#根据名称精准查询
POST /test_index2/_search
{
"query": {
"query_string": {
"query": "*F*",
"fields": ["name"]
}
}
}
#返回结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "test_index2_v2",
"_id": "VzCkyZABg7TU77-RuuJr",
"_score": 1,
"_source": {
"id": "2016113138",
"name": "FFF",
"createAt": "1999-01-01"
}
}
]
}
}
#由于在最初创建时keyword未配置,所以这里使用query_string进行模糊查询示例
条件范围查询
#根据ID范围查询,大于等于和小于等于2016113138 相当于只查询2016113138
POST /test_index2/_search
{
"query": {
"range": {
"id": {
"gte": 2016113138,
"lte": 2016113138
}
}
}
}
#根据ID范围查询,大于2016113137和小于2016113139 也相当于只查询2016113138
POST /test_index2/_search
{
"query": {
"range": {
"id": {
"gt": 2016113137,
"lt": 2016113139
}
}
}
}
#两个POST请求返回结果是相同的
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "test_index2_v2",
"_id": "VzCkyZABg7TU77-RuuJr",
"_score": 1,
"_source": {
"id": "2016113138",
"name": "FFF",
"createAt": "1999-01-01"
}
}
]
}
}
多条件查询
#must 条件与, must_not条件非 , should条件或
POST /test_index2/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "FFF"
}
},
{
"term": {
"id": "2016113138"
}
}
],
"must_not": [ ],
"should": [ ]
}
}
}
#返回结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.2039728,
"hits": [
{
"_index": "test_index2_v2",
"_id": "VzCkyZABg7TU77-RuuJr",
"_score": 2.2039728,
"_source": {
"id": "2016113138",
"name": "FFF",
"createAt": "1999-01-01"
}
}
]
}
}
分页查询
#查询第一页的两条数据
POST /test_index2/_search
{
"from": 0,
"size": 2
}
#返回结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "test_index2_v2",
"_id": "VjCkyZABg7TU77-RlOKM",
"_score": 1,
"_source": {
"id": "2016113136",
"name": "TTT",
"createAt": "1999-01-01"
}
},
{
"_index": "test_index2_v2",
"_id": "VzCkyZABg7TU77-RuuJr",
"_score": 1,
"_source": {
"id": "2016113138",
"name": "FFF",
"createAt": "1999-01-01"
}
}
]
}
}
参考
你已经完成了es入门学习,接下来要学习更深入了es,可以参考以下文章
es入门使用https://www.cnblogs.com/buchizicai/p/17093719.html
es系列教程https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&__biz=MzIxMjE3NjYwOQ==&scene=1&album_id=1337850434433744897#wechat_redirect
es数据类型https://www.cnblogs.com/shoufeng/p/10692113.html