安装方式
- 传统方式
根据平台系统Windows、linux、mac 下载安装包
以linux为例,进入到想安装的目录位置,下载安装包并解压
# 进入安装目录
cd /home
# 下载安装包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.3-linux-x86_64.tar.gz
# 解压
tar -zxvf elasticsearch-7.17.3-linux-x86_64.tar.gz elasticsearch-7.17.3
- docker容器化安装
现在主流的容器化技术,可以不用考虑配置环境,非常方便,但是需要一定的docker基础
启动前配置
环境配置
elasticsearch 7.x 之前的版本都是需要安装 jdk 的,但是在7.x版本之后就内置了 jdk 环境,所以可以不安装环境,也可以安装 jdk 环境 ,可以参考 bin/elasticsearch-env 文件
if [ ! -z "$ES_JAVA_HOME" ]; then
JAVA="$ES_JAVA_HOME/bin/java"
JAVA_TYPE="ES_JAVA_HOME"
elif [ ! -z "$JAVA_HOME" ]; then
# fallback to JAVA_HOME
echo "warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME" >&2
JAVA="$JAVA_HOME/bin/java"
JAVA_TYPE="JAVA_HOME"
else
# use the bundled JDK (default)
if [ "$(uname -s)" = "Darwin" ]; then
# macOS has a different structure
JAVA="$ES_HOME/jdk.app/Contents/Home/bin/java"
else
JAVA="$ES_HOME/jdk/bin/java"
fi
JAVA_TYPE="bundled JDK"
fi
意思就是说先会去找 ES_JAVA_HOME 这个环境变量,如果存在就用这个环境变量下的jdk,不存在找 JAVA_HOME 这个环境变量,一般都会有这个环境,如果在没有就去找 ES_HOME 这个环境变量下的jdk,ES_HOME 这个指向的就是我们安装包里的jdk目录。
优先级:ES_JAVA_HOME>JAVA_HOME>ES_HOME
配置文件
重要配置的修改 | Elasticsearch: 权威指南 | Elastic
这里官网给我们提供了重要的一些参数,供我们选择性修改。
# Elasticsearch 默认启动的集群名字叫 elasticsearch 。你最好给你的生产环境的集群改个名字,改名字的目的很简单, 就是防止某人的笔记本电脑加入了集群这种意外
cluster.name: elasticsearch_production
# Elasticsearch 会在你的节点启动的时候随机给它指定一个名字,这些名字是在启动的时候产生的,每次启动节点, 它都会得到一个新的名字。这会使日志变得很混乱,因为所有节点的名称都是不断变化的。
node.name: elasticsearch_005_data
# Elasticsearch 会把你最重要的数据放在以下目录下
path.data: /path/to/data1,/path/to/data2
# Elasticsearch 会把日志放在以下目录下
path.logs: /path/to/logs
# Elasticsearch 会把插件放在以下目录下
path.plugins: /path/to/plugins
# 设定对你的集群的稳定 极其 重要。 当你的集群中有两个 masters(注:主节点)的时候,这个配置有助于防止 脑裂 ,一种两个主节点同时存在于一个集群的现象。此设置应该始终被配置为 master 候选节点的法定个数(大多数个)。法定个数就是 ( master 候选节点个数 / 2) + 1
discovery.zen.minimum_master_nodes: 1
# 检查内存,当内存过小时,会启动失败,建议开发环境暂时关闭,生产环境内存足够再开启
bootstrap.memory_lock: false
# 配置能够访问当前节点的主机,0.0.0.0所有主机都可访问
network.host: 0.0.0.0
最重要的就是开启远程访问:
# elasticsearch.yml 中配置
network.host: 0.0.0.0
设置内存大小:
# 在config目录下修改jvm配置
vim jvm.options
# jvm堆内存大小
-Xms1g
-Xmx1g
建议:xms和xmx设置一样,xmx不要超过系统内存的50%,不要超过30G
启动Elasticsearch
如果是windows启动,则直接运行elasticsearch.bat即可
linux系统中运行es不能是root用户,所有我们需要先创建一个es用户
# 创建es用户
adduser es
# 修改密码
passwd es
# 分配权限
chown -R es:es ./elasticsearch-7.17.3
# 切换用户
su es
# 运行 -d 后台
bin/elasticsearch -d
可能遇到的错误:
- max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
最大虚拟内存太小,调大系统的虚拟内存
# 切换到root
su root
vim /etc/sysctl.conf
# 追加内容
vm.max_map_count=262144
# 保存之后执行
sysctl -p
- max number of threads [1024] for user (es] is too low, increase to at least [4096]
无法创建本地线程问题,用户最大可创建线程数大小
vim /etc/security/limits.d/20-nproc.conf
# 改为以下配置
* soft nproc 4096
- the default discovery settings are unsuitable for production use; at least one of (discovery.seed hostsdiscovery.seed providers, custer.initial master nodes] must be configured
缺少默认配置
# elasticsearch.yml 文件中放开以下注释
discovery.seed_hosts: ["本机ip"]
cluster.initial_master_nodes: ["本机节点名称"]
浏览器访问:http://ip:9200
开启自启es
- 创建es的服务启动文件
# 进入系统启动目录
cd /etc/init.d
# 新建es文件
vim elasticsearch
- 填写配置内容
#!/bin/bash
#chkconfig: 345 63 37
#description: elasticsearch
#processname: elasticsearch-7.17.3
# 这个目录是你Es所在文件夹的目录
export ES_HOME=/usr/local/elasticsearch-7.17.3
case $1 in
start)
su es<<!
cd $ES_HOME
./bin/elasticsearch -d -p pid
exit
!
echo "elasticsearch is started"
;;
stop)
pid=`cat $ES_HOME/pid`
kill -9 $pid
echo "elasticsearch is stopped"
;;
restart)
pid=`cat $ES_HOME/pid`
kill -9 $pid
echo "elasticsearch is stopped"
sleep 1
su es<<!
cd $ES_HOME
./bin/elasticsearch -d -p pid
exit
!
echo "elasticsearch is started"
;;
*)
echo "start|stop|restart"
;;
esac
exit 0
注意点:以下配置不可删除,前面的#注释保持
#!/bin/bash
#chkconfig: 345 63 37
#description: elasticsearch
#processname: elasticsearch-7.17.3
- 赋予权限
chmod 777 elasticsearch
- 添加/删除服务
# 添加服务
chkconfig --add elasticsearch
# 删除服务,无需执行
chkconfig --del elasticsearch
- 启动/停止/重启服务
# 启动
service elasticsearch start
# 停止
service elasticsearch stop
# 重启
service elasticsearch restart
- 开启/关闭自启服务
# 开启开机自启服务
chkconfig elasticsearch on
# 关闭开机自启服务
chkconfig elasticsearch off
安装可视化界面kibana
- 下载
官网下载地址:https://artifacts.elastic.co/downloads/kibana/kibana-7.17.3-linux-x86_64.tar.gz
# 下载
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.3-linux-x86_64.tar.gz
# 解压
tar -zxvf kibana-7.17.3-linux-x86_64.tar.gz
- 修改配置文件
官网配置文件参考:配置 Kibana | Kibana 用户手册 | Elastic
# 编辑配置文件
vim config/kibana.yml
# 以下配置放开
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://es主机ip:9200"]
i18n.locale: "zh-CN"
- 启动kibana
nohup bin/kibana &
安装分词器
- 在线安装
es的默认分词器是standard ,会最小粒度分词
# 查看已安装的插件
bin/elasticsearch-plugin list
# 安装 analysis-icu 分词器
bin/elasticsearch-plugin install analysis-icu
# 删除 analysis-icu 分词器
bin/elasticsearch-plugin remove analysis-icu
注意:安装或者删除插件之后需要重启es服务才能生效
- 离线安装
下载ik分词器就需要离线下载,下载地址:Tags · medcl/elasticsearch-analysis-ik · GitHub
从gitHub上下载对应es版本的ik分词器,上传到linux服务器上的plugins目录
# 切换es用户
su es
# 进入到插件目录中
cd elasticsearch-7.17.3/plugins
# 下载压缩包
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.3/elasticsearch-analysis-ik-7.17.3.zip
# 解压
unzip -d elasticsearch-analysis-ik elasticsearch-analysis-ik-7.17.3.zip
# 删除安装包
rm -f elasticsearch-analysis-ik-7.17.3.zip
解压之后,删除安装包,重启es服务就生效了
# 切换root用户
su root
# 找到es服务
netstat -tunlp |grep 9200
tcp6 0 0 :::9200 :::* LISTEN 1734/java
# 杀掉进程
kill 1734
# 切换es用户
su es
# 重启es
./bin/elasticsearch -d
测试ik分词器
# ik最粗力度
POST _analyze
{
"analyzer":"ik_smart",
"text":"十月初七"
}
# 结果
{
"tokens": [
{
"token": "十月",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
},
{
"token": "初七",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 1
}
]
}
# ik最细力度
POST _analyze
{
"analyzer":"ik_max_word",
"text":"十月初七"
}
# 结果
{
"tokens": [
{
"token": "十月",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
},
{
"token": "十",
"start_offset": 0,
"end_offset": 1,
"type": "TYPE_CNUM",
"position": 1
},
{
"token": "月初",
"start_offset": 1,
"end_offset": 3,
"type": "CN_WORD",
"position": 2
},
{
"token": "月",
"start_offset": 1,
"end_offset": 2,
"type": "COUNT",
"position": 3
},
{
"token": "初七",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 4
},
{
"token": "七",
"start_offset": 3,
"end_offset": 4,
"type": "TYPE_CNUM",
"position": 5
}
]
}
索引操作
- 了解索引
注意:索引名称必须小写,不能以下划线开头
- 创建
put /索引名称
# 简单创建索引
PUT /index_test
# 创建索引并配置分片数和副本数
PUT /index_test
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
# 返回数据
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "index_test"
}
- 修改、删除
put /索引名称/_settings
delete /索引名称
# 修改索引配置
PUT /index_test/_settings
{
"index": {
"number_of_replicas": 1
}
}
# 删除索引
DELETE /index_test
# 返回
{
"acknowledged": true
}
- 查询
get /索引名称
# 查询索引信息
GET /index_test
# 返回
{
"index_test": {
"aliases": {}, # 别名
"mappings": {}, # 映射,有数据后映射的字段类型
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "3", # 分片
"provided_name": "index_test", # 索引名称
"creation_date": "1679481012424",
"number_of_replicas": "1", # 副本
"uuid": "2TTTjETOQnK4V53XmGEC8Q", # uuid
"version": { # 版本
"created": "7170399"
}
}
}
}
}
文档操作
1、elasticsearch面向文档,文档是所有可搜索数据的最小单位
2、文档会被序列化成json格式保存在elasticsearch中,每个json字段对应着数据类型(字符串、数值、布尔、日期、二进制、范围类型)
3、文档存在一个唯一id标识,一篇文档包含多个字段,类似mysql中的一条记录
4、文档不需要事先定义好字段数据类型,es会根据数据进行自动推演,支持数组、支持嵌套
- 新增文档
修改es的分词器为ik分词器,删除后重新新增索引
PUT /index_test
{
"settings": {
"index":{
"analysis.analyzer.default.type":"ik_max_word"
}
}
}
[put | post] /索引名称/[_doc | _create]/id
# 指定id,id存在则修改否则新增,会删除内容后重新新增
PUT /index_test/_doc/1
{
"name": "十月初七",
"age": 18,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 1,
"hobbyName": "唱歌"
},{
"hobbyId": 2,
"hobbyName": "羽毛球"
},{
"hobbyId": 3,
"hobbyName": "狼人杀"
}
]
}
# id又有可无
POST /index_test/_doc
{
"name": "青玖",
"age": 18,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 1,
"hobbyName": "密室逃脱"
},{
"hobbyId": 2,
"hobbyName": "看电影"
}
]
}
# 自动生成id
POST /index_test/_doc
{
"name": "青玖",
"age": 18,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 1,
"hobbyName": "密室逃脱"
},{
"hobbyId": 2,
"hobbyName": "看电影"
}
]
}
# id存在则报错
POST | PUT /index_test/_create/4
{
"name": "test",
"age": 3,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 1,
"hobbyName": "打游戏"
}
]
}
# 返回
{
"_index": "index_test", # 索引名称
"_type": "_doc", # 文档类型,默认为_doc,8.x之后被删除
"_id": "1", # 唯一id
"_version": 1, # 版本。每次修改+1
"result": "created", # 新增 修改为:updated
"_shards": { # 分片
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0, # 类似修改版本,每次修改递增,用于并发场景的乐观锁
"_primary_term": 1 # 恢复数据时_seq_no一样的冲突,避免写入被覆盖,每次恢复递增
}
PUT和POST新增文档的区别:PUT只能根据id去创建和修改文档,POST会根据是否存在id去自动创建id创建文档和更新文档,两种更新文档都会全量更新。create只能创建文档不能更新。
- 局部更新文档
POST /索引名称/_update/id
# 根据id局部更新文档
POST /index_test/_update/1
{
"doc": {
"age": 19
}
}
# 返回
{
"_index": "index_test",
"_type": "_doc",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
根据查询的内容进行修改(原子性)
# 查询出的文档进行修改(对id为1的文档修改年龄为20)
POST /index_test/_update_by_query
{
"query": {
"match": {
"_id":1
}
},
"script":{
"source":"ctx._source.age=20"
}
}
# 返回
{
"took": 185,
"timed_out": false,
"total": 1,
"updated": 1,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": []
}
- 批量写入
请求参数至少有四行,必须是偶数行
第一行参数为操作类型、索引、文档类型和id、第二行参数为新增数据,如下:
POST _builk
{"actionName":{"_index":"indexName","_type":"typeName","_id":"id"}}
{"field1":"value1","field2":"value2","field3":"value3"}
actionName:表示操作类型,主要有create、update、delete、index
# 批量修改
POST /_bulk
{"update":{"_index":"index_test","_id":3}}
{"doc":{"name":"青玖","age":18,"phone":"13100000000","hobbyList":[{"hobbyId":4,"hobbyName":"密室逃脱"},{"hobbyId":5,"hobbyName":"看电影"}]}}
{"update":{"_index":"index_test","_id":4}}
{"doc":{"name":"test","age":23,"phone":"13111111111","hobbyList":[{"hobbyId":6,"hobbyName":"打游戏"}]}}
{"create":{"_index":"index_test","_id":5}}
{"doc":{"name":"张三","age":3,"phone":"13333333333","hobbyList":[{"hobbyId":6,"hobbyName":"打游戏"}]}}
# 返回
{
"took": 71,
"errors": false,
"items": [
{
"update": {
"_index": "index_test",
"_type": "_doc",
"_id": "3",
"_version": 3,
"result": "noop",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1,
"status": 200
}
},
{
"update": {
"_index": "index_test",
"_type": "_doc",
"_id": "4",
"_version": 2,
"result": "noop",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1,
"status": 200
}
},
{
"create": {
"_index": "index_test",
"_type": "_doc",
"_id": "5",
"_version": 1,
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
}
]
}
- 查询文档
主要分两种查询,不建议用第一种,大多用第二种,更加易读json
1、rest风格请求url,携带参数,k-v格式
2、条件封装到requestBoby (官方推荐)
根据文档id查询
# 根据id查询文档
GET /index_test/_doc/1
# 返回
{
"_index": "index_test",
"_type": "_doc",
"_id": "1",
"_version": 4,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"phone": "13355490600",
"name": "十月初七",
"hobbyList": [
{
"hobbyName": "唱歌",
"hobbyId": 1
},
{
"hobbyName": "羽毛球",
"hobbyId": 2
},
{
"hobbyName": "狼人杀",
"hobbyId": 3
}
],
"age": 20
}
}
- 批量查询
_mget
# 跨索引批量查询
GET _mget
{
"docs": [
{
"_index": "index_dev",
"_id": 1
},
{
"_index": "index_test",
"_id": 1
}
]
}
# 返回
{
"docs": [
{
"_index": "index_dev",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"doc": {
"name": "张三",
"age": 3,
"hobbyList": [
1,
2
]
}
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "1",
"_version": 4,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"phone": "13355490600",
"name": "十月初七",
"hobbyList": [
{
"hobbyName": "唱歌",
"hobbyId": 1
},
{
"hobbyName": "羽毛球",
"hobbyId": 2
},
{
"hobbyName": "狼人杀",
"hobbyId": 3
}
],
"age": 20
}
}
]
}
# 单个索引内
GET /index_test/_mget
{
"ids": [
1,
2
]
}
# 返回
{
"docs": [
{
"_index": "index_test",
"_type": "_doc",
"_id": "1",
"_version": 4,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"phone": "13355490600",
"name": "十月初七",
"hobbyList": [
{
"hobbyName": "唱歌",
"hobbyId": 1
},
{
"hobbyName": "羽毛球",
"hobbyId": 2
},
{
"hobbyName": "狼人杀",
"hobbyId": 3
}
],
"age": 20
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "2",
"found": false
}
]
}
_msearch
# 夸索引查询
GET _msearch
{"index":"index_dev"}
{"query": {"match_all": {}},"from":0,"size":2}
{"index": "index_test"}
{"query": {"match": {"name":"十月初七"}}}
# 返回
{
"took": 28,
"responses": [
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "index_dev",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"doc": {
"name": "李四",
"age": 4,
"hobbyList": [
1,
3
]
}
}
},
{
"_index": "index_dev",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"doc": {
"name": "王五",
"age": 5,
"hobbyList": [
1,
4
]
}
}
}
]
},
"status": 200
},
{
"took": 25,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 3.4526575,
"hits": [
{
"_index": "index_test",
"_type": "_doc",
"_id": "1",
"_score": 3.4526575,
"_source": {
"phone": "13355490600",
"name": "十月初七",
"hobbyList": [
{
"hobbyName": "唱歌",
"hobbyId": 1
},
{
"hobbyName": "羽毛球",
"hobbyId": 2
},
{
"hobbyName": "狼人杀",
"hobbyId": 3
}
],
"age": 20
}
}
]
},
"status": 200
}
]
}
- Query DSL查询
1、match_all
查询所有,默认返回10条数据,因为_search采用分页查询,默认取10条,默认最大分页返回数据10000条,一万条记录加载到内存,内存消耗太大,不建议修改。
# 查询所有
GET /index_test/_search
{
"query": {
"match_all": {}
},
"from":2, # 位置
"size": 2 # 返回数据数量,最大为10000,可在setting中修改index.max_result_window
"sort":[ # 排序
{
"age":"desc"
}
],
"_source":["name","age"] # 返回指定字段
}
# 返回
{
"_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoAxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAANAWa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAM8Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAANEWa1U0djZXOWxRNE9wbWZoXzVwQ0F4Zw==",
"took": 3,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "index_test",
"_type": "_doc",
"_id": "4",
"_score": null,
"_source": {
"name": "test",
"age": 23
},
"sort": [
23
]
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "十月初七",
"age": 20
},
"sort": [
20
]
}
]
}
}
# 修改最大返回数量, _all修改所有索引,可指定索引
PUT /_all/_settings
{
"index.max_result_window": 20000
}
2、 scroll分页
# 拿到分页id
GET /index_test/_search?scroll=1m
{
"query": {
"match_all": {}
},
"size": 2,
"sort":[
{
"age":"asc"
}
]
}
# 返回
{
"_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoAxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL0Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL8Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL4Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4Zw==",
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "index_test",
"_type": "_doc",
"_id": "5",
"_score": 1.0,
"_source": {
"doc": {
"name": "张三",
"age": 3,
"phone": "13333333333",
"hobbyList": [
{
"hobbyId": 6,
"hobbyName": "打游戏"
}
]
}
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "青玖",
"age": 18,
"phone": "13100000000",
"hobbyList": [
{
"hobbyName": "密室逃脱",
"hobbyId": 4
},
{
"hobbyName": "看电影",
"hobbyId": 5
}
]
}
}
]
}
}
# 根据 scroll_id 分页
GET /_search/scroll
{
"scroll": "1m",
"scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoAxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL0Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL8Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL4Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4Zw=="
}
# 返回
{
"_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoAxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL0Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL8Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL4Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4Zw==",
"took": 8,
"timed_out": false,
"terminated_early": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "index_test",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"name": "test",
"age": 23,
"phone": "13111111111",
"hobbyList": [
{
"hobbyName": "打游戏",
"hobbyId": 6
}
]
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "bYRQE4cBQtOYu_2bG9Pp",
"_score": 1.0,
"_source": {
"name": "青玖",
"age": 18,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 1,
"hobbyName": "密室逃脱"
},
{
"hobbyId": 2,
"hobbyName": "看电影"
}
]
}
}
]
}
}
3、match
# 名称中带有十月的,查询的数据取决于索引采用的分词器,这里是ik分词器
GET /index_test/_search
{
"query":{
"match":{
"name":"十月"
}
}
}
# 返回
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.407595,
"hits": [
{
"_index": "index_test",
"_type": "_doc",
"_id": "1",
"_score": 1.407595,
"_source": {
"phone": "13355490600",
"name": "十月初七",
"hobbyList": [
{
"hobbyName": "唱歌",
"hobbyId": 1
},
{
"hobbyName": "羽毛球",
"hobbyId": 2
},
{
"hobbyName": "狼人杀",
"hobbyId": 3
}
],
"age": 20
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "dYRtF4cBQtOYu_2bjNNQ",
"_score": 1.407595,
"_source": {
"name": "十月初一",
"age": 18,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 1,
"hobbyName": "密室逃脱"
}
]
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "doRtF4cBQtOYu_2bsdOx",
"_score": 1.407595,
"_source": {
"name": "十月初二",
"age": 3,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 2,
"hobbyName": "看电影"
}
]
}
}
]
}
}
# 查询名称带有十月初七中两个字符
GET /index_test/_search
{
"query": {
"match": {
"name": {
"query": "十月初七",
"operator": "or",
"minimum_should_match": 2 # 最低匹配度,在倒排索引中最低匹配度
}
}
}
}
# 返回
{
"took": 29,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 3.4009905,
"hits": [
{
"_index": "index_test",
"_type": "_doc",
"_id": "1",
"_score": 3.4009905,
"_source": {
"phone": "13355490600",
"name": "十月初七",
"hobbyList": [
{
"hobbyName": "唱歌",
"hobbyId": 1
},
{
"hobbyName": "羽毛球",
"hobbyId": 2
},
{
"hobbyName": "狼人杀",
"hobbyId": 3
}
],
"age": 20
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "d4SUIYcBQtOYu_2batP6",
"_score": 2.2538662,
"_source": {
"name": "初七",
"age": 18,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 1,
"hobbyName": "密室逃脱"
},
{
"hobbyId": 2,
"hobbyName": "看电影"
}
]
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "dYRtF4cBQtOYu_2bjNNQ",
"_score": 1.8767934,
"_source": {
"name": "十月初一",
"age": 18,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 1,
"hobbyName": "密室逃脱"
}
]
}
},
{
"_index": "index_test",
"_type": "_doc",
"_id": "doRtF4cBQtOYu_2bsdOx",
"_score": 1.8767934,
"_source": {
"name": "十月初二",
"age": 3,
"phone": "13100000000",
"hobbyList": [
{
"hobbyId": 2,
"hobbyName": "看电影"
}
]
}
}
]
}
}