ES 基本使用
部署服务
使用 安装包 或者 容器 下载 启动 elasticserch
https://www.elastic.co/cn/start
docker 命令如下
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.10.1
默认会在 9200
端口运行
GET localhost:9200
如果有相应代表运行成功
{
"name": "1a22b65b032e",
"cluster_name": "docker-cluster",
"cluster_uuid": "T08GpTq8QWWkglHr04oiZA",
"version": {
"number": "7.10.1",
"build_flavor": "default",
"build_type": "docker",
"build_hash": "1c34507e66d7db1211f66f3513706fdf548736aa",
"build_date": "2020-12-05T01:00:33.671820Z",
"build_snapshot": false,
"lucene_version": "8.7.0",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}
索引
我们通过HTTP请求与ES服务进行交互
创建索引
PUT {{LOCAL_ES}}/user
创建了一个名为user
的索引
查看索引列表
GET {{LOCAL_ES}}/_all
简单版本:
GET {{LOCAL_ES}}/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open user AG5bAWgpRyWFTwegKhaE_w 1 1 0 0 208b 208b
查看索引
GET {{LOCAL_ES}}/user
删除索引
DELETE {{LOCAL_ES}}/user
索引状态变更
开启: POST{{LOCAL_ES}}/user/_open
关闭: POST{{LOCAL_ES}}/user/_close
Mapping
字段类型
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
字符串:
- text: 需要被全文搜索的内容。设置text类型后,字段内容会被分析,升到倒排索引以前,字符串会被分析器拆分成一个个词项。text字段不用于排序,很少用于聚合
- keyword : 全字匹配,用于过滤和排序
日期类型:
- date
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
数值类型:
- long
- integer
- short
- byte
- double
- float
- half_float
- scaled_float
- unsigned_long
范围类型:
- integer_range
"properties": {
"expected_attendees": {
"type": "integer_range"
},
"time_frame": {
"type": "date_range",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
- float_range
- long_range
- double_range
- date_range
- ip_range
映射创建
ES 只能新增映射,不能修改或删除,只能数据重建。
新增映射
POST {{LOCAL_ES}}/ts300/_mapping
{
"properties": {
"contents": {
"type": "text"
},
"type": {
"type": "keyword"
},
"author": {
"type": "keyword"
},
"title": {
"type": "keyword"
}
}
}
原有映射修改
- 创建一个新的权限索引,包含调整后的字段或类型 #job job2
- 将原有索引(reindex)到新索引 #job reindex->#job2
- 删除原有索引 #DELETE job
- 将新索引别名设置为原有索引名 # job2 alias->job
文档
添加文档
POST {{LOCAL_ES}}/ts300/_create/1
请求体:
{
"id": 1,
"contents": "孤鸿海上来,池潢不敢顾。\n侧见双翠鸟,巢在三珠树。\n矫矫珍木巅,得无金丸惧。\n美服患人指,高明逼神恶。\n今我游冥冥,弋者何所慕。",
"type": "五言古诗",
"author": "张九龄",
"title": "感遇四首之一"
}
返回值:
{
"_index": "ts300",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
更新文档(全部替换)
POST {{LOCAL_ES}}/ts300/_doc/1
如果id原来没有则创建
获取文档
GET{{LOCAL_ES}}/ts300/_doc/1
获取多个文档
POST {{LOCAL_ES}}/ts300/_mget
{
"ids": [
1
]
}
更新文档字段
不推荐使用
POST {{LOCAL_ES}}/ts300/_update/1
{
"script": "ctx._source.title=\"感遇四首之1一\""
}
删除字段
{
"script": "ctx._source.remove(\"title\")"
}
删除文档
DELETE {{LOCAL_ES}}/ts300/_doc/1
ingest
文档预处理
创建
PUT {{LOCAL_ES}}/_ingest/pipeline/indexed_at
{
"description": "Adds timestamp to documents",
"processors": [
{
"set": {
"field": "_source.timestamp",
"value": "{{_ingest.timestamp}}"
}
},
{
"script": {
"source": "ctx.cont_length = ctx.contents.length();"
}
}
]
}
模板
PUT _template/my_template
{
"index_patterns": [
"some_index*"
],
"aliases": {
"some_index": {}
},
"settings": {
"index.default_pipeline": "indexed_at",
"number_of_replicas": 1,
"refresh_interval": "30s"
},
"mappings": {
"properties": {
"cont_length":{
"type":"long"
},
"author": {
"type": "text",
"fields": {
"field": {
"type": "keyword"
}
},
"analyzer": "ik_max_word"
},
"contents": {
"type": "text",
"fields": {
"field": {
"type": "keyword"
}
},
"analyzer": "ik_max_word",
"fielddata": true
},
"timestamp": {
"type": "date"
},
"title": {
"type": "text",
"fields": {
"field": {
"type": "keyword"
}
},
"analyzer": "ik_max_word"
},
"type": {
"type": "text",
"fields": {
"field": {
"type": "keyword"
}
},
"analyzer": "ik_max_word"
}
}
}
}
IK 分词器安装
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.1/elasticsearch-analysis-ik-7.10.1.zip
配置 kibnana 数据可视化 展示
点击visulizaiton
选择图标
点击 Split Slices
然后点击Run
生成图表