Elasticsearch 操作文档
安装部署(Centos7.x)
- 下载
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-linux-x86_64.tar.gz - 修改系统 ulimit 参数
- 启动
集群
ES的集群设置比较简单,只需要在配置文件中加上
集群优化配置
| 概念 | 说明 |
|---|---|
| todo | todo |
基本概念
Type概念在ES7.x版本已经去掉了,所以目前Index又具备表的概念,
| ES | 说明 | MySQL |
|---|---|---|
| Index(索引) | 索引就是文档的容器,是一类文档的集合,逻辑空间上的分类,物理空间上的分片 | DataBase(数据库) |
| Type(类型) | Table(表) | |
| Document(文档) | Row(行) | |
| Field(字段) | Column(列) | |
| Mapping(映射) | Schema(约束) | |
| 存储的都是索引 | Index(索引) | |
| Query DSL | SQL |
常用操作
Index(索引)操作
创建索引
# 创建animals索引
curl --location --request PUT 'http://localhost:9200/animals' \
--header 'Content-Type: application/json' \
--data-raw '{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"kind": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"number": {
"type": "integer"
}
}
}
}'
查询索引
# 查询集群中的所有索引
curl --location --request GET 'http://localhost:9200/_cat/indices?v'
# 查看索引相关信息
curl --location --request GET 'http://localhost:9200/animals'
# 查看索引的文档总数
curl --location --request GET 'http://localhost:9200/animals/_count'
# 查看以animals开头的索引
curl --location --request GET 'http://localhost:9200/_cat/indices/animals*?v&s=index'
# 查看状态为绿的索引
curl --location --request GET 'http://localhost:9200/_cat/indices?v&health=green'
删除索引
curl --location --request DELETE 'http://localhost:9200/animals'
关闭索引
如果索引被关闭,那么关于这个索引的所有读写操作都会被阻断
curl --location --request POST 'http://localhost:9200/_close'
打开索引
curl --location --request POST 'http://localhost:9200/_open'
冻结索引
冻结索引和关闭索引类似,关闭索引是既不能读,也不能写。而冻结索引是可以读,但是不能写
curl --location --request POST 'http://localhost:9200/_freeze'
解冻索引
curl --location --request POST 'http://localhost:9200/_unfreeze'
Document(文档)操作
创建文档
# 指定id创建文档,如果id不存在则直接创建,如果ID已存在则先删除老数据再创建新数据
curl --location --request PUT 'http://localhost:9200/animals/_doc/1' \
--header 'Content-Type: application/json' \
--data-raw '{
"kind":"金毛",
"name":"Jim",
"number":1
}'
# 不指定id,自动生成id
curl --location --request POST 'http://localhost:9200/animals/_doc' \
--header 'Content-Type: application/json' \
--data-raw '{
"kind":"柴犬",
"name":"chai",
"number":2
}'
# 指定id,如果id已经存在,则报错
curl --location --request PUT 'http://localhost:9200/animals/_doc/2?op_type=create' \
--header 'Content-Type: application/json' \
--data-raw '{
"kind":"博美",
"name":"mei",
"number":2
}'
# 同上,另一种写法
curl --location --request PUT 'http://localhost:9200/animals/_create/3' \
--header 'Content-Type: application/json' \
--data-raw '{
"kind":"拉布拉多",
"name":"duo",
"number":3
}'
查询文档(基础查询)
# 查询集群中的所有索引
curl --location --request GET 'http://localhost:9200/_cat/indices?v'
# 查询集群中的所有索引的数据
curl --location --request GET 'http://localhost:9200/_search'
# 查询索引index1的数据
curl --location --request GET 'http://localhost:9200/index1/_search'
# 查询索引index1、index2的数据
curl --location --request GET 'http://localhost:9200/index1,index2/_search'
# 查询已以index开头的索引的数据
curl --location --request GET 'http://localhost:9200/index*/_search'
# 查询指定索引指定id的文档
curl --location --request GET 'http://localhost:9200/animals/_doc/2'
更新文档
文档必须已经存在,更新会对相应字段进行修改
curl --location --request POST 'http://localhost:9200/animals/_update/2' \
--header 'Content-Type: application/json' \
--data-raw '{
"doc":{
"number":2
}
}'
删除文档
curl --location --request DELETE 'http://localhost:9200/animals/_doc/Jd7IuoIBSRe4rlchkqbA'
高级查询:URI Search(支持Get)
支持的参数Parameters:q、df、analyzer、analyze_wildcard、batched_reduce_size、default_operator、lenient、explain、_source、stored_fields、sort、track_scores、track_total_hits、timeout、terminate_after、from、size、search_type、allow_partial_search_results,具体含义请参照官方文档
# 查询animals索引全部内容
curl --location --request POST 'http://localhost:9200/animals/_search' \
--header 'Content-Type: application/json' \
--data-raw '{}'
# 查询animals索引中kind=金毛的文档
curl --location --request GET 'http://localhost:9200/animals/_search?q=kind:"金毛"'
# 查询索引animals、movies中id=1的文档
curl --location --request GET 'http://localhost:9200/animals,movies/_search?q=_id:1'
# 同上
curl --location --request GET 'http://localhost:9200/animals/_search?q=1&df=_id'
# 泛查询,查询所有value为1的数据
curl --location --request GET 'http://localhost:9200/animals/_search?q=1'
# term查询:title包括Beautiful OR Mind
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:(Beautiful Mind)'
# Phrase查询,title为Beautiful Mind
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:"Beautiful Mind"'
# 查询title含Beautiful,但Mind为泛查询(即全部字段都进行查询mind)
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:Beautiful Mind'
# 范围查询
curl --location --request GET 'http://localhost:9200/movies/_search?q=year:>2000'
curl --location -g --request GET 'http://localhost:9200/movies/_search?q=year:[2018 TO 2022]'
# 布尔查询,+ (这个词必须存在),- (该个词不得存在),不带+/-(可选的,它的存在增加了相关性)
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:(Hello -The +Last)'
# 查询term模糊匹配,~1:表示编辑距离允许为1,允许一个字符不匹配,可以匹配一些单词拼写错误场景
# 如:beautifl~1仍然允许查询beautiful
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:(beautifl~1)'
# 查询phrase近似度匹配
# 可以查询出与"Lord Rings"最大编辑距离为2的内容,如Lord of the Rings
curl --location --request GET 'http://localhost:9200/movies/_search?q=title:"Lord Rings"~2'
高级查询:Request Body Search(支持Get、Post)
Match query,执行全文搜索的标准查询
# 查询animals索引全部文档
curl --location --request GET 'http://localhost:9200/animals/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"profile": true,
"query": {
"match_all": {}
}
}'
# 分页查询
# ignore_unavailable=true,可以忽略尝试访问不存在的索引 404_idx 导致的报错
curl --location --request POST 'http://localhost:9200/movies,404_idx/_search?ignore_unavailable=true' \
--header 'Content-Type: application/json' \
--data-raw '{
"profile": true,
"from":10,
"size":10,
"query":{
"match_all": {}
}
}'
// term查询,title包括last OR christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match": {
"title": "last christmas"
}
}
}'
# term查询,title包括last AND christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match": {
"title": {
"query": "last christmas",
"operator": "and"
}
}
}
}'
# hrase查询
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_phrase": {
"title":{
"query": "one love"
}
}
}
}'
# phrase查询-近似度匹配
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_phrase": {
"title":{
"query": "one love",
"slop": 1
}
}
}
}'
# 排序
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"sort":[{"year":"desc"}],
"query":{
"match_all": {}
}
}'
# 过滤source
curl --location --request POST 'http://localhost:9200/animals/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"_source":["kind"],
"query":{
"match_all": {}
}
}'
Painless contexts,script query
curl --location --request POST 'http://localhost:9200/animals/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"script_fields": {
"new_field": {
"script": {
"lang": "painless",
"source": "'\''小 '\''+doc['\''kind'\''].value"
}
}
},
"query": {
"match_all": {}
}
}'
# 查询title包含last AND christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"profile": true,
"query":{
"query_string":{
"default_field": "title",
"query": "last AND christmas"
}
}
}'
# 查询title包含last或者christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"profile": true,
"query":{
"query_string":{
"default_field": "title",
"query": "last christmas"
}
}
}'
# 同上
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"profile": true,
"query":{
"query_string":{
"default_field": "title",
"query": "last OR christmas"
}
}
}'
# 查询title包含last或AND或christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"profile": true,
"query":{
"simple_query_string":{
"fields": ["title"],
"query": "last AND christmas"
}
}
}'
# 查询name同时包含last和christmas
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"profile": true,
"query":{
"simple_query_string":{
"fields": ["title"],
"query": "last christmas",
"default_operator": "AND"
}
}
}'
Term query
term query会查询一个准确匹配字段的分词,包括空格和区分大小写,查询文本避免使用term query,而应使用match query
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"profile": true,
"query":{
"term": {
"year": {
"value": 2018
}
}
}
}'
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"range" : {
"year" : {
"gte" : 2018,
"lte" : 2200,
"boost" : 2.0
}
}
}
}'
Boolean query
bool 查询采用匹配越多越好的方法,因此每个匹配的 must 或 should 子句的分数将被加在一起以提供每个文档的最终 _score
支持参数:must、filter、should、must_not
# 查询title必须包含Last,genre必须包含Crime,year为2018的文档
curl --location --request POST 'http://localhost:9200/movies/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"bool" : {
"must" : [{
"match" : { "title" : "Last" }
},{
"match" : { "genre" : "Crime" }
}],
"should": {
"term" : {"id":"5893"}
},
"filter": [
{ "range": { "year": { "gte": "2008" }}}
]
}
}
}'
插件使用
todo
插件开发
todo

浙公网安备 33010602011771号