Elasticsearch 学习笔记
简介
ElasticSearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口。Elasticsearch 是用 Java 语言开发的,并作为 Apache 许可条款下的开放源码发布,是一种流行的企业级搜索引擎。
ElasticSearch 用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
分布式的文档存储引擎 分布式的搜索引擎和分析引擎 分布式,支持PB级数据
搜索领域: 如百度、谷歌,全文检索等。 门户网站: 访问统计、文章点赞、留言评论等。 广告推广: 记录员工行为数据、消费趋势、员工群体进行定制推广等。 信息采集: 记录应用的埋点数据、访问日志数据等,方便大数据进行分析。
下载镜像源
https://mirrors.huaweicloud.com/elasticsearch/7.6.2/
https://mirrors.huaweicloud.com/kibana/7.6.2/
安装中文分词插件,在elasticsearch-6.2.2\bin目录下执行以下命令
elasticsearch-plugin install https://github.91chifun.workers.dev/https://github.com//medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
一、 ElasticSerach 认识
1.1 索引 index
1.2 类型 type
- 一种type就像一类表,比如user表,order表
1.3 映射 mapping
- mapping定义了每个字段的类型等信息。相当于关系型数据库中的表结
1.4 文档 document
- 一个document相当于关系型数据库中的一?行行记录
1.5 字段 field
1.6 集群 cluster
-集群由一个或多个节点组成,一个集群有一个默认名称"elasticsearch"
1.7 节点 node
1.8 分片和副本 node
- 副本是分片的副本。分?有主分片(primary?Shard)和副本分片(replica?Shard)之分。Index数据在物理理上被分布在多个主分片中,每个主分片只存放部分数据。每个主分?可以有多个副本,叫副本分片,是主分片的复制。
1.9 核心数据类型
1.9.1 字符串:
- 用于全文索引,该类型的字段将通过分词器 进?分词
- 不分词,只能搜索该字段的完整的值
1.9.2 数值型
- long, integer, short, byte, double, float, half_float, scaled_float
1.9.3 数值型 boolean
1.9.3 二进制
- 该类型的字段把值当做经过 base64 编码的字符串,默认不存储,且不可搜索
1.9.4 范围类型
- 范围类型表示值是一个范围,而不是?个具体的值
- integer_range, float_range, long_range, double_range, date_range
- 譬如 age 的类型是 integer_range,那么值可以是 {“gte” : 20, “lte” : 40};搜索 “term” : {“age”: 21} 可以搜索该值
1.9.5 日期
- 由于Json没有date类型,所以es通过识别字符串是否符合format定义的格式来判断是否为date类型
- format默认为:strict_date_optional_time||epoch_millis
- “2022-01-01” “2022/01/01 12:10:30” 这种字符串格式
二、索引基本操作
2.1 创建索引 PUT请求
localhost:9200/nba
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "nba"
}
2.2 查看索引 GET请求
localhost:9200/nba
{
"nba": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1573278626713",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "eeQmIsZ8Tl-GJ-xpFuOirg",
"version": {
"created": "7020199"
},
"provided_name": "nba"
}
}
}
}
2.4 删除索引 DELETE请求
localhost:9200/nba
{
"acknowledged": true
}
2.5 批量获取索引 GET请求
localhost:9200/cba,nba
-响应
{
"cba": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1573281458107",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "ikxZrzk2TVqQn7zRi2_glw",
"version": {
"created": "7020199"
},
"provided_name": "cba"
}
}
},
"nba": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1573281355145",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "hkhv1WKSQqWil3P9UXt3Aw",
"version": {
"created": "7020199"
},
"provided_name": "nba"
}
}
}
}
2.6 获取全部索引 GET请求
localhost:9200/_all
-响应
}
}
"cba": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1573281458107",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "ikxZrzk2TVqQn7zRi2_glw",
"version": {
"created": "7020199"
},
"provided_name": "cba"
}
}
},
"nba": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1573281355145",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "hkhv1WKSQqWil3P9UXt3Aw",
"version": {
"created": "7020199"
},
"provided_name": "nba"
}
}
}
2.7 使用_cat获取全部索引 GET请求
localhost:9200/_cat/indices?v
-响应
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_task_manager YA0slSRkRJqrE_jF0aOlFA 1 0 2 0 45.5kb 45.5kb
yellow open cba ikxZrzk2TVqQn7zRi2_glw 1 1 0 0 230b 230b
yellow open nba hkhv1WKSQqWil3P9UXt3Aw 1 1 0 0 283b 283b
green open .kibana_1 579wwXgCQJKJU1Ge7cJVXw 1 0 4 1 24kb 24kb
2.8 判断索引是否存在 HEAD请求
localhost:9200/nba
-响应
状态码"200"则为存在,不存在则为"404"
2.9 关闭索引 不删除 POST请求
localhost:9200/nba/_close
-响应
{
"acknowledged": true,
"shards_acknowledged": true
}
2.10 打开索引 POST请求
localhost:9200/nba/_open
-响应
{
"acknowledged": true,
"shards_acknowledged": true
}
三、映射的介绍与使用
- type: text 可分词
- type: keyword 不可分词
3.1 创建Mapping PUT请求
localhost:9200/nba/_mapping
{
"properties":{
"name":{
"type":"text"
},
"team_name":{
"type":"text"
},
"position":{
"type":"keyword"
},
"play_year":{
"type":"keyword"
},
"jerse_no":{
"type":"keyword"
}
}
}
-响应
{
"acknowledged": true
}
3.2 查看Mapping信息 GET请求
localhost:9200/nba/_mapping
{
"nba": {
"mappings": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
}
}
}
}
}
3.3 批量获取Mapping信息 GET请求
localhost:9200/nba,cba/_mapping
{
"nba": {
"mappings": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
}
}
}
},
"cba": {
"mappings": {}
}
}
3.4 获取所有Mapping信息第一种方式 GET请求
localhost:9200/_mapping
{
"nba": {
"mappings": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
}
}
}
},
"cba": {
"mappings": {}
}
}
3.5 获取所有Mapping信息第二种方式 GET请求
localhost:9200/_all/_mapping
{
"nba": {
"mappings": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
}
}
}
},
"cba": {
"mappings": {}
}
}
3.6 增加Mapping字段 POST请求
localhost:9200/nba/_mapping
{
"properties":{
"name":{
"type":"text"
},
"team_name":{
"type":"text"
},
"position":{
"type":"keyword"
},
"play_year":{
"type":"keyword"
},
"jerse_no":{
"type":"keyword"
},
"country":{
"type":"keyword"
}
}
}
{
"acknowledged": true
}
四、文档的增删改查
4.1 新增文档 指定ID PUT/POST请求
localhost:9200/nba/_doc/1
{
"name":"哈登",
"team_name":"火箭",
"position":"得分后卫",
"play_year":"10",
"jerse_no":"13"
}
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
4.2 新增文档 自动生成ID POST请求
localhost:9200/nba/_doc
{
"name":"库里",
"team_name":"勇士",
"position":"组织后卫",
"play_year":"10",
"jerse_no":"30"
}
{
"_index": "nba",
"_type": "_doc",
"_id": "7PkGT24BeuZ7t7g8CXe-",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
4.3 自动创建索引 POST请求
localhost:9200/wnba/_doc/1
{
"name":"周琦",
"team_name":"波兰国家队",
"position":"中锋",
"play_year":"3",
"jerse_no":"9"
}
{
"_index": "wnba",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
4.4 查看自动创建的索引 GET请求
localhost:9200/wnba
{
"wnba": {
"aliases": {},
"mappings": {
"properties": {
"jerse_no": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"play_year": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"position": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"team_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"settings": {
"index": {
"creation_date": "1573284418718",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "fKs9KZ11R3-_zgKi8WFQTQ",
"version": {
"created": "7020199"
},
"provided_name": "wnba"
}
}
}
}
4.5 指定操作类型
localhost:9200/nba/_doc/1?op_type=create
{
"name":"周琦",
"team_name":"波兰国家队",
"position":"中锋",
"play_year":"3",
"jerse_no":"9"
}
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [5])",
"index_uuid": "hkhv1WKSQqWil3P9UXt3Aw",
"shard": "0",
"index": "nba"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [5])",
"index_uuid": "hkhv1WKSQqWil3P9UXt3Aw",
"shard": "0",
"index": "nba"
},
"status": 409
}
4.6 查看指定ID文档 GET请求
localhost:9200/nba/_doc/1
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 5,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
}
4.7 查看多条文档 第一种方式 GET/POST请求
localhost:9200/_mget
{
"docs" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "1"
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "2"
}
]
}
{
"docs": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 5,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"found": false
}
]
}
4.8 查看多条文档 第二种方式 GET/POST请求
localhost:9200/nba/_mget
{
"docs" : [
{
"_type" : "_doc",
"_id" : "1"
},
{
"_type" : "_doc",
"_id" : "2"
}
]
}
{
"docs": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 5,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"found": false
}
]
}
4.9 查看多条文档 第三种方式 GET/POST请求
localhost:9200/nba/_doc/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
{
"docs": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 5,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"found": false
}
]
}
4.10 查看多条文档 第四种方式 GET/POST请求
localhost:9200/nba/_doc/_mget
{
"ids":["1","2"]
}
{
"docs": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 5,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"found": false
}
]
}
4.11 修改文档 POST请求
localhost:9200/nba/_update/1
{
"doc": {
"name": "哈登",
"team_name": "火箭",
"position": "双能卫",
"play_year": "10",
"jerse_no": "13"
}
}
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 7,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 1
}
4.12 向_source增加字段 POST请求
localhost:9200/nba/_update/1
{
"script": "ctx._source.age = 18"
}
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 8,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 8,
"_primary_term": 1
}
4.13 向_source删除字段 POST请求
localhost:9200/nba/_update/1
{
"script": "ctx._source.remove(\"age\")"
}
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 9,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 9,
"_primary_term": 1
}
4.14 更新指定文档的字段 POST请求
localhost:9200/nba/_update/1
{
"script": {
"source": "ctx._source.age += params.age",
"params": {
"age": 4
}
},
"upsert":{
"age": 1
}
}
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 11,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 11,
"_primary_term": 1
}
localhost:9200/nba/_doc/1
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_version": 11,
"_seq_no": 11,
"_primary_term": 1,
"found": true,
"_source": {
"name": "大胡子",
"team_name": "火箭",
"position": "双能卫",
"play_year": "10",
"jerse_no": "13",
"age": 22
}
}
4.15 upsert介绍
localhost:9200/nba/_update/3
{
"script": {
"source": "ctx._source.allstar += params.allstar",
"params": {
"allstar": 4
}
},
"upsert": {
"allstar": 1
}
}
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 12,
"_primary_term": 1
}
localhost:9200/nba/_doc/3
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_version": 1,
"_seq_no": 12,
"_primary_term": 1,
"found": true,
"_source": {
"allstar": 1
}
}
4.16 删除文档 DELETE请求
localhost:9200/nba/_doc/3
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 13,
"_primary_term": 1
}
五、文档的搜索
-
term(词条)查询和full text(全?文)查询 -
词条查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索 -
全?查询:ElasticSearch引擎会先分析查询字符串,将其拆分成多个分词,只要已分析的字段中包含词条的任意?个,或全部包含,就匹配查询条件,返回该?档;如果不包含任意一个分词,表示没有任何文档匹配查询条件
5.1 单条trem查询 GET/POST请求
-
term 关键字查询,精确查询 -
例如Sql的where条件 -
请求
localhost:9200/nba/_search
{
"query":{
"term":{
"jerse_no":"23"
}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.9808292,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 0.9808292,
"_source": {
"name": "詹姆斯",
"team_name": "湖人",
"position": "小前锋",
"play_year": "15",
"jerse_no": "23"
}
}
]
}
}
5.2 多条trem查询 GET/POST请求
localhost:9200/nba/_search
{
"query":{
"terms":{
"jerse_no": ["23","13"]
}
}
}
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "詹姆斯",
"team_name": "湖人",
"position": "小前锋",
"play_year": "15",
"jerse_no": "23"
}
}
]
}
}
5.2 match_all查询 GET/POST请求
localhost:9200/nba/_search
{
"query":{
"match_all":{}
},
"from": 0,
"size": 100
}
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "库里",
"team_name": "勇士",
"position": "控球后卫",
"play_year": "10",
"jerse_no": "30"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "詹姆斯",
"team_name": "湖人",
"position": "小前锋",
"play_year": "15",
"jerse_no": "23"
}
}
]
}
}
5.3 match查询 GET/POST请求
localhost:9200/nba/_search
{
"query":{
"match":{
"name": "库小里"
}
},
"from": 0,
"size": 100
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.0834165,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 2.0834165,
"_source": {
"name": "库里",
"team_name": "勇士",
"position": "控球后卫",
"play_year": "10",
"jerse_no": "30"
}
}
]
}
}
5.4 multi_match 多个查询 GET/POST请求
localhost:9200/nba/_search
{
"query":{
"multi_match":{
"query": "shooter",
"fields": ["title","name"]
}
}
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.18232156,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 0.18232156,
"_source": {
"name": "库里",
"team_name": "勇士",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30",
"title": "the best shooter"
}
}
]
}
}
5.4 match_phrase 多个查询 GET/POST请求
localhost:9200/nba/_search
{
"query":{
"match_phrase":{
"position": "得分后卫"
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 3.277387,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 3.277387,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": "10",
"jerse_no": "13"
}
}
]
}
}
5.5 match_phrase_profix 多个查询 GET/POST请求
localhost:9200/nba/_search
{
"query":{
"match_phrase_prefix":{
"title": "the"
}
}
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 0.2876821,
"_source": {
"name": "库里",
"team_name": "勇士",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30",
"title": "the best shooter"
}
}
]
}
}
六、ElasticSerach 搜索
6.1 批量导入数据
- ES提供了Bulk的API 来进行批量操作
{"index": {"_index": "book", "_type": "_doc", "_id": 1}}
{"name": "权力的游戏"}
{"index": {"_index": "book", "_type": "_doc", "_id": 2}}
{"name": "疯狂的石头"}
curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @name
{
"query":{
"match_all":{}
},
"from": 0,
"size": 100
}
6.2 trem多种查询
- 这些查询通常?用于结构化的数据,?比如:number, date, keyword等,而不是对text。
- 也就是说,全?本查询之前要先对?本内容进?分词,?单词级别的查询直接在相应字段的反向索引中精确查找,单词级别的查询?般?于数值、?期等类型的字段上
{
"mappings":{
"properties":{
"birthDay":{
"type":"date"
},
"birthDayStr": {
"type":"keyword"
},
"age":{
"type":"integer"
},
"code": {
"type":"text"
},
"country":{
"type":"text"
},
"countryEn": {
"type":"text"
},
"displayAffiliation":{
"type":"text"
},
"displayName": {
"type":"text"
},
"displayNameEn":{
"type":"text"
},
"draft": {
"type":"long"
},
"heightValue":{
"type":"float"
},
"jerseyNo": {
"type":"text"
},
"playYear":{
"type":"long"
},
"playerId": {
"type":"keyword"
},
"position":{
"type":"text"
},
"schoolType": {
"type":"text"
},
"teamCity":{
"type":"text"
},
"teamCityEn": {
"type":"text"
},
"teamConference": {
"type":"keyword"
},
"teamConferenceEn":{
"type":"keyword"
},
"teamName": {
"type":"keyword"
},
"teamNameEn":{
"type":"keyword"
},
"weight": {
"type":"text"
}
}
}
}
6.2.1 term query 精准匹配查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"term": {
"jerseyNo": 23
}
},
"from": 0,
"size": 20
}
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 11,
"relation": "eq"
},
"max_score": 3.6167762,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "73",
"_score": 3.6167762,
"_source": {
"countryEn": "United States",
"teamName": "雄鹿",
"birthDay": 792392400000,
"country": "美国",
"teamCityEn": "Milwaukee",
"code": "sterling_brown",
"displayAffiliation": "Southern Methodist/United States",
"displayName": "斯特林 布朗",
"schoolType": "College",
"teamConference": "东部",
"teamConferenceEn": "Eastern",
"weight": "105.2 公斤",
"teamCity": "密尔沃基",
"playYear": 2,
"jerseyNo": "23",
"teamNameEn": "Bucks",
"draft": 2017,
"displayNameEn": "Sterling Brown",
"heightValue": 1.98,
"birthDayStr": "1995-02-10",
"position": "后卫",
"age": 24,
"playerId": "1628425"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "78",
"_score": 3.6167762,
"_source": {
"countryEn": "United States",
"teamName": "独行侠",
"birthDay": 721544400000,
"country": "美国",
"teamCityEn": "Dallas",
"code": "trey_burke",
"displayAffiliation": "Michigan/United States",
"displayName": "特雷 伯克",
"schoolType": "College",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "79.4 公斤",
"teamCity": "达拉斯",
"playYear": 6,
"jerseyNo": "23",
"teamNameEn": "Mavericks",
"draft": 2013,
"displayNameEn": "Trey Burke",
"heightValue": 1.85,
"birthDayStr": "1992-11-12",
"position": "后卫",
"age": 27,
"playerId": "203504"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "167",
"_score": 3.6167762,
"_source": {
"countryEn": "United States",
"teamName": "雷霆",
"birthDay": 895377600000,
"country": "美国",
"teamCityEn": "Oklahoma City",
"code": "terrance_ferguson",
"displayAffiliation": "United States",
"displayName": "特伦斯 弗格森",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "86.2 公斤",
"teamCity": "俄克拉荷马城",
"playYear": 2,
"jerseyNo": "23",
"teamNameEn": "Thunder",
"draft": 2017,
"displayNameEn": "Terrance Ferguson",
"heightValue": 2.01,
"birthDayStr": "1998-05-17",
"position": "后卫-前锋",
"age": 21,
"playerId": "1628390"
}
}
]
}
}
6.2.2 exsit query 在特定的字段中查找非空值的文档 GET/POST请求
{
"query": {
"exists": {
"field": "teamNameEn"
}
},
"from": 0,
"size": 3
}
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 566,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "老鹰",
"birthDay": 831182400000,
"country": "美国",
"teamCityEn": "Atlanta",
"code": "jaylen_adams",
"displayAffiliation": "United States",
"displayName": "杰伦 亚当斯",
"schoolType": "College",
"teamConference": "东部",
"teamConferenceEn": "Eastern",
"weight": "86.2 公斤",
"teamCity": "亚特兰大",
"playYear": 1,
"jerseyNo": "10",
"teamNameEn": "Hawks",
"draft": 2018,
"displayNameEn": "Jaylen Adams",
"heightValue": 1.88,
"birthDayStr": "1996-05-04",
"position": "后卫",
"age": 23,
"playerId": "1629121"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"countryEn": "New Zealand",
"teamName": "雷霆",
"birthDay": 743140800000,
"country": "新西兰",
"teamCityEn": "Oklahoma City",
"code": "steven_adams",
"displayAffiliation": "Pittsburgh/New Zealand",
"displayName": "斯蒂文 亚当斯",
"schoolType": "College",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "120.2 公斤",
"teamCity": "俄克拉荷马城",
"playYear": 6,
"jerseyNo": "12",
"teamNameEn": "Thunder",
"draft": 2013,
"displayNameEn": "Steven Adams",
"heightValue": 2.13,
"birthDayStr": "1993-07-20",
"position": "中锋",
"age": 26,
"playerId": "203500"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "热火",
"birthDay": 869198400000,
"country": "美国",
"teamCityEn": "Miami",
"code": "bam_adebayo",
"displayAffiliation": "Kentucky/United States",
"displayName": "巴姆 阿德巴约",
"schoolType": "College",
"teamConference": "东部",
"teamConferenceEn": "Eastern",
"weight": "115.7 公斤",
"teamCity": "迈阿密",
"playYear": 2,
"jerseyNo": "13",
"teamNameEn": "Heat",
"draft": 2017,
"displayNameEn": "Bam Adebayo",
"heightValue": 2.08,
"birthDayStr": "1997-07-18",
"position": "中锋-前锋",
"age": 22,
"playerId": "1628389"
}
}
]
}
}
6.2.3 prefix query 查找包含带有指定前缀term的文档 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"prefix": {
"teamNameEn": "Rock"
}
},
"from": 0,
"size": 3
}
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 21,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "86",
"_score": 1.0,
"_source": {
"countryEn": "Switzerland",
"teamName": "火箭",
"birthDay": 769233600000,
"country": "瑞士",
"teamCityEn": "Houston",
"code": "clint_capela",
"displayAffiliation": "Switzerland/Switzerland",
"displayName": "克林特 卡佩拉",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "108.9 公斤",
"teamCity": "休斯顿",
"playYear": 5,
"jerseyNo": "15",
"teamNameEn": "Rockets",
"draft": 2014,
"displayNameEn": "Clint Capela",
"heightValue": 2.08,
"birthDayStr": "1994-05-18",
"position": "中锋",
"age": 25,
"playerId": "203991"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "99",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "火箭",
"birthDay": 816930000000,
"country": "美国",
"teamCityEn": "Houston",
"code": "chris_chiozza",
"displayAffiliation": "University of Florida/United States",
"displayName": "克里斯 Chiozza",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "79.4 公斤",
"teamCity": "休斯顿",
"playYear": 1,
"jerseyNo": "2",
"teamNameEn": "Rockets",
"draft": 2018,
"displayNameEn": "Chris Chiozza",
"heightValue": 1.83,
"birthDayStr": "1995-11-21",
"position": "后卫",
"age": 24,
"playerId": "1629185"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "101",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "火箭",
"birthDay": 784962000000,
"country": "美国",
"teamCityEn": "Houston",
"code": "gary_clark",
"displayAffiliation": "University of Cincinnati/United States",
"displayName": "加里 克拉克",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "102.1 公斤",
"teamCity": "休斯顿",
"playYear": 1,
"jerseyNo": "6",
"teamNameEn": "Rockets",
"draft": 2018,
"displayNameEn": "Gary Clark",
"heightValue": 2.03,
"birthDayStr": "1994-11-16",
"position": "前锋",
"age": 25,
"playerId": "1629109"
}
}
]
}
}
6.2.4 wildcard query 支持通配符查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"wildcard": {
"teamNameEn": "Ro*s"
}
},
"from": 0,
"size": 3
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 21,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "86",
"_score": 1.0,
"_source": {
"countryEn": "Switzerland",
"teamName": "火箭",
"birthDay": 769233600000,
"country": "瑞士",
"teamCityEn": "Houston",
"code": "clint_capela",
"displayAffiliation": "Switzerland/Switzerland",
"displayName": "克林特 卡佩拉",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "108.9 公斤",
"teamCity": "休斯顿",
"playYear": 5,
"jerseyNo": "15",
"teamNameEn": "Rockets",
"draft": 2014,
"displayNameEn": "Clint Capela",
"heightValue": 2.08,
"birthDayStr": "1994-05-18",
"position": "中锋",
"age": 25,
"playerId": "203991"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "99",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "火箭",
"birthDay": 816930000000,
"country": "美国",
"teamCityEn": "Houston",
"code": "chris_chiozza",
"displayAffiliation": "University of Florida/United States",
"displayName": "克里斯 Chiozza",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "79.4 公斤",
"teamCity": "休斯顿",
"playYear": 1,
"jerseyNo": "2",
"teamNameEn": "Rockets",
"draft": 2018,
"displayNameEn": "Chris Chiozza",
"heightValue": 1.83,
"birthDayStr": "1995-11-21",
"position": "后卫",
"age": 24,
"playerId": "1629185"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "101",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "火箭",
"birthDay": 784962000000,
"country": "美国",
"teamCityEn": "Houston",
"code": "gary_clark",
"displayAffiliation": "University of Cincinnati/United States",
"displayName": "加里 克拉克",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "102.1 公斤",
"teamCity": "休斯顿",
"playYear": 1,
"jerseyNo": "6",
"teamNameEn": "Rockets",
"draft": 2018,
"displayNameEn": "Gary Clark",
"heightValue": 2.03,
"birthDayStr": "1994-11-16",
"position": "前锋",
"age": 25,
"playerId": "1629109"
}
}
]
}
}
6.2.5 regexp query 正则表达式查询 GET/POST请求
{
"query": {
"regexp": {
"teamNameEn": "Ro.*s"
}
},
"from": 0,
"size": 3
}
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 21,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "86",
"_score": 1.0,
"_source": {
"countryEn": "Switzerland",
"teamName": "火箭",
"birthDay": 769233600000,
"country": "瑞士",
"teamCityEn": "Houston",
"code": "clint_capela",
"displayAffiliation": "Switzerland/Switzerland",
"displayName": "克林特 卡佩拉",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "108.9 公斤",
"teamCity": "休斯顿",
"playYear": 5,
"jerseyNo": "15",
"teamNameEn": "Rockets",
"draft": 2014,
"displayNameEn": "Clint Capela",
"heightValue": 2.08,
"birthDayStr": "1994-05-18",
"position": "中锋",
"age": 25,
"playerId": "203991"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "99",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "火箭",
"birthDay": 816930000000,
"country": "美国",
"teamCityEn": "Houston",
"code": "chris_chiozza",
"displayAffiliation": "University of Florida/United States",
"displayName": "克里斯 Chiozza",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "79.4 公斤",
"teamCity": "休斯顿",
"playYear": 1,
"jerseyNo": "2",
"teamNameEn": "Rockets",
"draft": 2018,
"displayNameEn": "Chris Chiozza",
"heightValue": 1.83,
"birthDayStr": "1995-11-21",
"position": "后卫",
"age": 24,
"playerId": "1629185"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "101",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "火箭",
"birthDay": 784962000000,
"country": "美国",
"teamCityEn": "Houston",
"code": "gary_clark",
"displayAffiliation": "University of Cincinnati/United States",
"displayName": "加里 克拉克",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "102.1 公斤",
"teamCity": "休斯顿",
"playYear": 1,
"jerseyNo": "6",
"teamNameEn": "Rockets",
"draft": 2018,
"displayNameEn": "Gary Clark",
"heightValue": 2.03,
"birthDayStr": "1994-11-16",
"position": "前锋",
"age": 25,
"playerId": "1629109"
}
}
]
}
}
6.2.6 ids query 通过id批量查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"ids": {
"values": [1,2,3]
}
},
"from": 0,
"size": 3
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "老鹰",
"birthDay": 831182400000,
"country": "美国",
"teamCityEn": "Atlanta",
"code": "jaylen_adams",
"displayAffiliation": "United States",
"displayName": "杰伦 亚当斯",
"schoolType": "College",
"teamConference": "东部",
"teamConferenceEn": "Eastern",
"weight": "86.2 公斤",
"teamCity": "亚特兰大",
"playYear": 1,
"jerseyNo": "10",
"teamNameEn": "Hawks",
"draft": 2018,
"displayNameEn": "Jaylen Adams",
"heightValue": 1.88,
"birthDayStr": "1996-05-04",
"position": "后卫",
"age": 23,
"playerId": "1629121"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"countryEn": "New Zealand",
"teamName": "雷霆",
"birthDay": 743140800000,
"country": "新西兰",
"teamCityEn": "Oklahoma City",
"code": "steven_adams",
"displayAffiliation": "Pittsburgh/New Zealand",
"displayName": "斯蒂文 亚当斯",
"schoolType": "College",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "120.2 公斤",
"teamCity": "俄克拉荷马城",
"playYear": 6,
"jerseyNo": "12",
"teamNameEn": "Thunder",
"draft": 2013,
"displayNameEn": "Steven Adams",
"heightValue": 2.13,
"birthDayStr": "1993-07-20",
"position": "中锋",
"age": 26,
"playerId": "203500"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"countryEn": "United States",
"teamName": "热火",
"birthDay": 869198400000,
"country": "美国",
"teamCityEn": "Miami",
"code": "bam_adebayo",
"displayAffiliation": "Kentucky/United States",
"displayName": "巴姆 阿德巴约",
"schoolType": "College",
"teamConference": "东部",
"teamConferenceEn": "Eastern",
"weight": "115.7 公斤",
"teamCity": "迈阿密",
"playYear": 2,
"jerseyNo": "13",
"teamNameEn": "Heat",
"draft": 2017,
"displayNameEn": "Bam Adebayo",
"heightValue": 2.08,
"birthDayStr": "1997-07-18",
"position": "中锋-前锋",
"age": 22,
"playerId": "1628389"
}
}
]
}
}
6.3 范围查询
6.3.1 第一种范围查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"range": {
"playYear":{
"gte": 2,
"lte" : 10
}
}
},
"from": 0,
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 77,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "热火",
"birthDay" : 869198400000,
"country" : "美国",
"teamCityEn" : "Miami",
"code" : "bam_adebayo",
"displayAffiliation" : "Kentucky/United States",
"displayName" : "巴姆 阿德巴约",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "115.7 公斤",
"teamCity" : "迈阿密",
"playYear" : 2,
"jerseyNo" : "13",
"teamNameEn" : "Heat",
"draft" : 2017,
"displayNameEn" : "Bam Adebayo",
"heightValue" : 2.08,
"birthDayStr" : "1997-07-18",
"position" : "中锋-前锋",
"age" : 22,
"playerId" : "1628389"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "9",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "篮网",
"birthDay" : 893131200000,
"country" : "美国",
"teamCityEn" : "Brooklyn",
"code" : "jarrett_allen",
"displayAffiliation" : "Texas/United States",
"displayName" : "贾瑞特 艾伦",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "107.5 公斤",
"teamCity" : "布鲁克林",
"playYear" : 2,
"jerseyNo" : "31",
"teamNameEn" : "Nets",
"draft" : 2017,
"displayNameEn" : "Jarrett Allen",
"heightValue" : 2.11,
"birthDayStr" : "1998-04-21",
"position" : "中锋",
"age" : 21,
"playerId" : "1628386"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "10",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "尼克斯",
"birthDay" : 727074000000,
"country" : "美国",
"teamCityEn" : "New York",
"code" : "kadeem_allen",
"displayAffiliation" : "Arizona/United States",
"displayName" : "卡迪姆 艾伦",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "90.7 公斤",
"teamCity" : "纽约",
"playYear" : 2,
"jerseyNo" : "0",
"teamNameEn" : "Knicks",
"draft" : 2017,
"displayNameEn" : "Kadeem Allen",
"heightValue" : 1.9,
"birthDayStr" : "1993-01-15",
"position" : "后卫",
"age" : 26,
"playerId" : "1628443"
}
}
]
}
}
6.3.2 第二种范围查询 GET/POST请求
{
"query": {
"range": {
"birthDay":{
"gte": "01/01/1999",
"lte" : "1999",,
"format": "dd/MM/yyyy||yyyy"
}
}
},
"from": 0,
"size": 3
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 521,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "老鹰",
"birthDay" : 831182400000,
"country" : "美国",
"teamCityEn" : "Atlanta",
"code" : "jaylen_adams",
"displayAffiliation" : "United States",
"displayName" : "杰伦 亚当斯",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "86.2 公斤",
"teamCity" : "亚特兰大",
"playYear" : 1,
"jerseyNo" : "10",
"teamNameEn" : "Hawks",
"draft" : 2018,
"displayNameEn" : "Jaylen Adams",
"heightValue" : 1.88,
"birthDayStr" : "1996-05-04",
"position" : "后卫",
"age" : 23,
"playerId" : "1629121"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"countryEn" : "New Zealand",
"teamName" : "雷霆",
"birthDay" : 743140800000,
"country" : "新西兰",
"teamCityEn" : "Oklahoma City",
"code" : "steven_adams",
"displayAffiliation" : "Pittsburgh/New Zealand",
"displayName" : "斯蒂文 亚当斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "120.2 公斤",
"teamCity" : "俄克拉荷马城",
"playYear" : 6,
"jerseyNo" : "12",
"teamNameEn" : "Thunder",
"draft" : 2013,
"displayNameEn" : "Steven Adams",
"heightValue" : 2.13,
"birthDayStr" : "1993-07-20",
"position" : "中锋",
"age" : 26,
"playerId" : "203500"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "热火",
"birthDay" : 869198400000,
"country" : "美国",
"teamCityEn" : "Miami",
"code" : "bam_adebayo",
"displayAffiliation" : "Kentucky/United States",
"displayName" : "巴姆 阿德巴约",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "115.7 公斤",
"teamCity" : "迈阿密",
"playYear" : 2,
"jerseyNo" : "13",
"teamNameEn" : "Heat",
"draft" : 2017,
"displayNameEn" : "Bam Adebayo",
"heightValue" : 2.08,
"birthDayStr" : "1997-07-18",
"position" : "中锋-前锋",
"age" : 22,
"playerId" : "1628389"
}
}
]
}
}
6.4 布尔查询
type | description |
---|
must | 必须出现在匹配文档中 | filter | 必须出现在文档中,但是不打分 | must_not | 不能出现在文档中 | should | 应该出现在文档中 |
6.4.1 must查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"bool": {
"must":[
{
"match": {
"displayNameEn": "james"
}
}
]
}
},
"from": 0,
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 4.699642,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "266",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "国王",
"birthDay" : 854082000000,
"country" : "美国",
"teamCityEn" : "Sacramento",
"code" : "justin_james",
"displayAffiliation" : "United States",
"displayName" : "贾斯汀 詹姆斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "86.2 公斤",
"teamCity" : "萨克拉门托",
"playYear" : 0,
"jerseyNo" : "",
"teamNameEn" : "Kings",
"draft" : 2019,
"displayNameEn" : "Justin James",
"heightValue" : 2.01,
"birthDayStr" : "1997-01-24",
"position" : "后卫-前锋",
"age" : 22,
"playerId" : "1629713"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "267",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "湖人",
"birthDay" : 473230800000,
"country" : "美国",
"teamCityEn" : "Los Angeles",
"code" : "lebron_james",
"displayAffiliation" : "No College/United States",
"displayName" : "勒布朗 詹姆斯",
"schoolType" : "High School",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "113.4 公斤",
"teamCity" : "洛杉矶",
"playYear" : 16,
"jerseyNo" : "23",
"teamNameEn" : "Lakers",
"draft" : 2003,
"displayNameEn" : "LeBron James",
"heightValue" : 2.03,
"birthDayStr" : "1984-12-30",
"position" : "前锋",
"age" : 35,
"playerId" : "2544"
}
}
]
}
}
6.4.2 filter查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"bool": {
"filter":[
{
"match": {
"displayNameEn": "james"
}
}
]
}
},
"from": 0,
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "158",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "76人",
"birthDay" : 646804800000,
"country" : "美国",
"teamCityEn" : "Philadelphia",
"code" : "james_ennis iii",
"displayAffiliation" : "Cal State-Long Beach/United States",
"displayName" : "詹姆斯 恩尼斯三世",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "95.3 公斤",
"teamCity" : "费城",
"playYear" : 5,
"jerseyNo" : "11",
"teamNameEn" : "76ers",
"draft" : 2013,
"displayNameEn" : "James Ennis III",
"heightValue" : 2.01,
"birthDayStr" : "1990-07-01",
"position" : "前锋",
"age" : 29,
"playerId" : "203516"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "266",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "国王",
"birthDay" : 854082000000,
"country" : "美国",
"teamCityEn" : "Sacramento",
"code" : "justin_james",
"displayAffiliation" : "United States",
"displayName" : "贾斯汀 詹姆斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "86.2 公斤",
"teamCity" : "萨克拉门托",
"playYear" : 0,
"jerseyNo" : "",
"teamNameEn" : "Kings",
"draft" : 2019,
"displayNameEn" : "Justin James",
"heightValue" : 2.01,
"birthDayStr" : "1997-01-24",
"position" : "后卫-前锋",
"age" : 22,
"playerId" : "1629713"
}
}
]
}
}
6.4.3 must_not查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"bool": {
"filter":[
{
"match": {
"displayNameEn": "james"
}
}
],
"must_not": [
{
"term": {
"teamConferenceEn": {
"value": "eastern"
}
}
}
]
}
},
"from": 0,
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "158",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "76人",
"birthDay" : 646804800000,
"country" : "美国",
"teamCityEn" : "Philadelphia",
"code" : "james_ennis iii",
"displayAffiliation" : "Cal State-Long Beach/United States",
"displayName" : "詹姆斯 恩尼斯三世",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "95.3 公斤",
"teamCity" : "费城",
"playYear" : 5,
"jerseyNo" : "11",
"teamNameEn" : "76ers",
"draft" : 2013,
"displayNameEn" : "James Ennis III",
"heightValue" : 2.01,
"birthDayStr" : "1990-07-01",
"position" : "前锋",
"age" : 29,
"playerId" : "203516"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "266",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "国王",
"birthDay" : 854082000000,
"country" : "美国",
"teamCityEn" : "Sacramento",
"code" : "justin_james",
"displayAffiliation" : "United States",
"displayName" : "贾斯汀 詹姆斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "86.2 公斤",
"teamCity" : "萨克拉门托",
"playYear" : 0,
"jerseyNo" : "",
"teamNameEn" : "Kings",
"draft" : 2019,
"displayNameEn" : "Justin James",
"heightValue" : 2.01,
"birthDayStr" : "1997-01-24",
"position" : "后卫-前锋",
"age" : 22,
"playerId" : "1629713"
}
}
]
}
}
6.4.4 should 第一种 查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"bool": {
"filter":[
{
"match": {
"displayNameEn": "james"
}
}
],
"must_not": [
{
"term": {
"teamConferenceEn": {
"value": "eastern"
}
}
}
],
"should": [
{
"range": {
"playYear": {
"gte": 11,
"lte": 20
}
}
}
]
}
},
"from": 0,
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "267",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "湖人",
"birthDay" : 473230800000,
"country" : "美国",
"teamCityEn" : "Los Angeles",
"code" : "lebron_james",
"displayAffiliation" : "No College/United States",
"displayName" : "勒布朗 詹姆斯",
"schoolType" : "High School",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "113.4 公斤",
"teamCity" : "洛杉矶",
"playYear" : 16,
"jerseyNo" : "23",
"teamNameEn" : "Lakers",
"draft" : 2003,
"displayNameEn" : "LeBron James",
"heightValue" : 2.03,
"birthDayStr" : "1984-12-30",
"position" : "前锋",
"age" : 35,
"playerId" : "2544"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "158",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "76人",
"birthDay" : 646804800000,
"country" : "美国",
"teamCityEn" : "Philadelphia",
"code" : "james_ennis iii",
"displayAffiliation" : "Cal State-Long Beach/United States",
"displayName" : "詹姆斯 恩尼斯三世",
"schoolType" : "College",
"teamConference" : "东部",
"teamConferenceEn" : "Eastern",
"weight" : "95.3 公斤",
"teamCity" : "费城",
"playYear" : 5,
"jerseyNo" : "11",
"teamNameEn" : "76ers",
"draft" : 2013,
"displayNameEn" : "James Ennis III",
"heightValue" : 2.01,
"birthDayStr" : "1990-07-01",
"position" : "前锋",
"age" : 29,
"playerId" : "203516"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 0.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
}
]
}
}
6.4.5 should 第二种 查询 GET/POST请求
{
"query": {
"bool": {
"filter":[
{
"match": {
"displayNameEn": "james"
}
}
],
"must_not": [
{
"term": {
"teamConferenceEn": {
"value": "eastern"
}
}
}
],
"should": [
{
"range": {
"playYear": {
"gte": 11,
"lte": 20
}
}
}
]
, "minimum_should_match": 1
}
},
"from": 0,
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "267",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "湖人",
"birthDay" : 473230800000,
"country" : "美国",
"teamCityEn" : "Los Angeles",
"code" : "lebron_james",
"displayAffiliation" : "No College/United States",
"displayName" : "勒布朗 詹姆斯",
"schoolType" : "High School",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "113.4 公斤",
"teamCity" : "洛杉矶",
"playYear" : 16,
"jerseyNo" : "23",
"teamNameEn" : "Lakers",
"draft" : 2003,
"displayNameEn" : "LeBron James",
"heightValue" : 2.03,
"birthDayStr" : "1984-12-30",
"position" : "前锋",
"age" : 35,
"playerId" : "2544"
}
}
]
}
}
6.5 排序查询
6.5.1 第一种排序查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"match": {
"teamName": "Rockets"
}
},
"sort": [
{
"playYear": {
"order": "desc"
}
}
],
"from": 0,
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "395",
"_score" : null,
"_source" : {
"countryEn" : "Brazil",
"teamName" : "火箭",
"birthDay" : 400737600000,
"country" : "巴西",
"teamCityEn" : "Houston",
"code" : "nene_hilario",
"displayAffiliation" : "Brazil",
"displayName" : "内内",
"schoolType" : "",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "113.4 公斤",
"teamCity" : "休斯顿",
"playYear" : 17,
"jerseyNo" : "42",
"teamNameEn" : "Rockets",
"draft" : 2002,
"displayNameEn" : "Nene",
"heightValue" : 2.11,
"birthDayStr" : "1982-09-13",
"position" : "中锋-前锋",
"age" : 37,
"playerId" : "2403"
},
"sort" : [
17
]
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "425",
"_score" : null,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 484200000000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "chris_paul",
"displayAffiliation" : "Wake Forest/United States",
"displayName" : "克里斯 保罗",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "79.4 公斤",
"teamCity" : "休斯顿",
"playYear" : 14,
"jerseyNo" : "3",
"teamNameEn" : "Rockets",
"draft" : 2005,
"displayNameEn" : "Chris Paul",
"heightValue" : 1.83,
"birthDayStr" : "1985-05-06",
"position" : "后卫",
"age" : 34,
"playerId" : "101108"
},
"sort" : [
14
]
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "206",
"_score" : null,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 507099600000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "gerald_green",
"displayAffiliation" : "No College/United States",
"displayName" : "杰拉德 格林",
"schoolType" : "High School",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "93.0 公斤",
"teamCity" : "休斯顿",
"playYear" : 12,
"jerseyNo" : "14",
"teamNameEn" : "Rockets",
"draft" : 2005,
"displayNameEn" : "Gerald Green",
"heightValue" : 2.01,
"birthDayStr" : "1986-01-26",
"position" : "后卫-前锋",
"age" : 33,
"playerId" : "101123"
},
"sort" : [
12
]
}
]
}
}
6.5.2 第一种排序查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"match": {
"teamNameEn": "Rockets"
}
},
"sort": [
{
"playYear": {
"order": "desc"
}
},
{
"heightValue": {
"order": "asc"
}
}
],
"from": 0,
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "395",
"_score" : null,
"_source" : {
"countryEn" : "Brazil",
"teamName" : "火箭",
"birthDay" : 400737600000,
"country" : "巴西",
"teamCityEn" : "Houston",
"code" : "nene_hilario",
"displayAffiliation" : "Brazil",
"displayName" : "内内",
"schoolType" : "",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "113.4 公斤",
"teamCity" : "休斯顿",
"playYear" : 17,
"jerseyNo" : "42",
"teamNameEn" : "Rockets",
"draft" : 2002,
"displayNameEn" : "Nene",
"heightValue" : 2.11,
"birthDayStr" : "1982-09-13",
"position" : "中锋-前锋",
"age" : 37,
"playerId" : "2403"
},
"sort" : [
17,
2.11
]
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "425",
"_score" : null,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 484200000000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "chris_paul",
"displayAffiliation" : "Wake Forest/United States",
"displayName" : "克里斯 保罗",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "79.4 公斤",
"teamCity" : "休斯顿",
"playYear" : 14,
"jerseyNo" : "3",
"teamNameEn" : "Rockets",
"draft" : 2005,
"displayNameEn" : "Chris Paul",
"heightValue" : 1.83,
"birthDayStr" : "1985-05-06",
"position" : "后卫",
"age" : 34,
"playerId" : "101108"
},
"sort" : [
14,
1.83
]
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "206",
"_score" : null,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 507099600000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "gerald_green",
"displayAffiliation" : "No College/United States",
"displayName" : "杰拉德 格林",
"schoolType" : "High School",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "93.0 公斤",
"teamCity" : "休斯顿",
"playYear" : 12,
"jerseyNo" : "14",
"teamNameEn" : "Rockets",
"draft" : 2005,
"displayNameEn" : "Gerald Green",
"heightValue" : 2.01,
"birthDayStr" : "1986-01-26",
"position" : "后卫-前锋",
"age" : 33,
"playerId" : "101123"
},
"sort" : [
12,
2.01
]
}
]
}
}
6.6 聚合查询指标聚合
-
聚合分析是数据库中重要的功能特性,完成对?个查询的数据集中数据的聚合计算,如:找出某字段(或计算表达式的结果)的最?值、最?值,计算和、平均值等。ES作为搜索引擎兼数据库,同样提供了强?的聚合分析能力。 -
对?个数据集求最大、最小、和、平均值等指标的聚合,在ES中称为指标聚合 -
而关系型数据库中除了有聚合函数外,还可以对查询出的数据行分组group by,再在组上进行指标聚合。在ES中称为桶聚合
6.6.1 max min sum avg 指标聚合查询 GET/POST
localhost:9200/nba/_search
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
},
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
},
"size": 0
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"avgAge" : {
"value" : 26.761904761904763
}
}
}
6.6.2 value_count 指标聚合查询 GET/POST
localhost:9200/nba/_search
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
},
"aggs": {
"countPlayerYear": {
"value_count": {
"field": "playYear"
}
}
},
"size": 0
}
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"countPlayerYear" : {
"value" : 21
}
}
}
6.6.3 _count 指标聚合查询(严格来说不属于聚合) GET/POST
localhost:9200/nba/_count
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
}
}
{
"count": 21,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
}
}
6.6.4 cardinality 指标聚合查询 GET/POST
localhost:9200/nba/_search
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
},
"aggs": {
"countAge": {
"cardinality": {
"field": "age"
}
}
},
"size": 0
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"countAge" : {
"value" : 13
}
}
}
6.6.5 status 种指标聚合查询 GET/POST
localhost:9200/nba/_search
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
},
"aggs": {
"status": {
"stats": {
"field": "age"
}
}
},
"size": 0
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"status" : {
"count" : 21,
"min" : 21.0,
"max" : 37.0,
"avg" : 26.761904761904763,
"sum" : 562.0
}
}
}
6.6.6 extended status 种指标聚合查询 GET/POST
localhost:9200/nba/_search
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
},
"aggs": {
"extendedStatsAge": {
"extended_stats": {
"field": "age"
}
}
},
"size": 0
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"extendedStatsAge" : {
"count" : 21,
"min" : 21.0,
"max" : 37.0,
"avg" : 26.761904761904763,
"sum" : 562.0,
"sum_of_squares" : 15534.0,
"variance" : 23.5147392290249,
"std_deviation" : 4.84919985451465,
"std_deviation_bounds" : {
"upper" : 36.46030447093406,
"lower" : 17.063505052875463
}
}
}
}
6.6.7 percentiles 种指标聚合查询 GET/POST
-
Percentiles 占比百分位对应的值统计,默认返回[ 1, 5, 25, 50, 75, 95, 99 ]分位上的值 -
请求
localhost:9200/nba/_search
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
},
"aggs": {
"percentilesAge": {
"percentiles": {
"field": "age",
}
}
},
"size": 0
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"percentilesAge" : {
"values" : {
"1.0" : 21.0,
"5.0" : 21.0,
"25.0" : 22.75,
"50.0" : 25.0,
"75.0" : 30.25,
"95.0" : 35.349999999999994,
"99.0" : 37.0
}
}
}
}
6.7 聚合查询桶聚合
6.7.1 terms aggregation 桶聚合查询 GET/POST
localhost:9200/nba/_search
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
},
"aggs": {
"aggrAge": {
"terms": {
"field": "age",
"size": 3
}
}
},
"size": 0
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"aggrAge" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 3,
"buckets" : [
{
"key" : 21,
"doc_count" : 4
},
{
"key" : 25,
"doc_count" : 3
},
{
"key" : 23,
"doc_count" : 2
}
]
}
}
}
6.7.2 order 第一种分组聚合查询 GET/POST
{
"query": {
"term": {
"teamNameEn": "Rockets"
}
},
"aggs": {
"aggrAge": {
"terms": {
"field": "age",
"size": 3,
"order": {
"_key": "desc"
}
}
}
},
"size": 0
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 21,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"aggrAge" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 17,
"buckets" : [
{
"key" : 37,
"doc_count" : 1
},
{
"key" : 34,
"doc_count" : 2
},
{
"key" : 33,
"doc_count" : 1
}
]
}
}
}
6.7.3 order 第二种分组聚合查询 GET/POST
localhost:9200/nba/_search
{
"aggs": {
"aggsTeamName": {
"terms": {
"field": "teamNameEn",
"size": 3,
"order": {
"avgAge": "desc"
}
},
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
}
}
},
"size": 0
}
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 566,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"aggsTeamName" : {
"doc_count_error_upper_bound" : -1,
"sum_other_doc_count" : 511,
"buckets" : [
{
"key" : "Bucks",
"doc_count" : 14,
"avgAge" : {
"value" : 28.142857142857142
}
},
{
"key" : "Mavericks",
"doc_count" : 20,
"avgAge" : {
"value" : 27.85
}
},
{
"key" : "Lakers",
"doc_count" : 21,
"avgAge" : {
"value" : 27.714285714285715
}
}
]
}
}
}
6.7.3 include 筛选分组聚合查询 GET/POST
localhost:9200/nba/_search
{
"aggs": {
"aggsTeamName": {
"terms": {
"field": "teamNameEn",
"include": ["Lakes","Rockets","warriors"],
"exclude": ["warriors"],
"size": 3,
"order": {
"avgAge": "desc"
}
},
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
}
}
},
"size": 0
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 566,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"aggsTeamName" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Rockets",
"doc_count" : 21,
"avgAge" : {
"value" : 26.761904761904763
}
}
]
}
}
}
6.7.4 include 正则筛选分组聚合查询 GET/POST
localhost:9200/nba/_search
{
"aggs": {
"aggsTeamName": {
"terms": {
"field": "teamNameEn",
"include": "Lakers|Ro.*|Warriors.*",
"exclude": "warriors",
"size": 3,
"order": {
"avgAge": "desc"
}
},
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
}
}
},
"size": 0
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 566,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"aggsTeamName" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Lakers",
"doc_count" : 21,
"avgAge" : {
"value" : 27.714285714285715
}
},
{
"key" : "Rockets",
"doc_count" : 21,
"avgAge" : {
"value" : 26.761904761904763
}
},
{
"key" : "Warriors",
"doc_count" : 20,
"avgAge" : {
"value" : 26.25
}
}
]
}
}
}
6.7.4 range aggregation 范围分组聚合查询 GET/POST
localhost:9200/nba/_search
{
"aggs": {
"ageRange": {
"range": {
"field": "age",
"ranges": [
{
"to": 20,
"key": "A"
},
{
"from": 20,
"to": 35,
"key": "B"
},
{
"from": 35,
"key": "C"
}
]
}
}
},
"size": 0
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 566,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"ageRange" : {
"buckets" : [
{
"key" : "A",
"to" : 20.0,
"doc_count" : 15
},
{
"key" : "B",
"from" : 20.0,
"to" : 35.0,
"doc_count" : 531
},
{
"key" : "C",
"from" : 35.0,
"doc_count" : 20
}
]
}
}
}
6.7.5 date rabge aggregation 时间范围分组聚合查询 GET/POST
localhost:9200/nba/_search
{
"aggs": {
"birthDayRange": {
"date_range": {
"field": "birthDay",
"format": "MM-yyy",
"ranges": [
{
"from": "01-1989"
},
{
"from": "01-1989",
"to": "01-1999"
},
{
"from": "01-1999",
"to": "01-2009"
},
{
"from": "01-2009"
}
]
}
}
},
"size": 0
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 566,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"birthDayRange" : {
"buckets" : [
{
"key" : "01-1989-01-1999",
"from" : 5.99616E11,
"from_as_string" : "01-1989",
"to" : 9.151488E11,
"to_as_string" : "01-1999",
"doc_count" : 426
},
{
"key" : "01-1989-*",
"from" : 5.99616E11,
"from_as_string" : "01-1989",
"doc_count" : 469
},
{
"key" : "01-1999-01-2009",
"from" : 9.151488E11,
"from_as_string" : "01-1999",
"to" : 1.230768E12,
"to_as_string" : "01-2009",
"doc_count" : 43
},
{
"key" : "01-2009-*",
"from" : 1.230768E12,
"from_as_string" : "01-2009",
"doc_count" : 0
}
]
}
}
}
6.7.6 date histogram aggregation 时间范围分组聚合查询 GET/POST
-
按天、月、年等进行聚合统计。可按 year (1y), quarter (1q), month (1M), week (1w), day (1d), hour (1h), minute (1m), second (1s) 间隔聚合 -
请求
localhost:9200/nba/_search
{
"aggs": {
"birthday_aggs": {
"date_histogram": {
"field": "birthDay",
"format": "yyyy",
"calendar_interval": "year"
}
}
},
"size": 0
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 566,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"birthday_aggs" : {
"buckets" : [
{
"key_as_string" : "1977",
"key" : 220924800000,
"doc_count" : 1
},
{
"key_as_string" : "1978",
"key" : 252460800000,
"doc_count" : 1
},
{
"key_as_string" : "1979",
"key" : 283996800000,
"doc_count" : 0
},
{
"key_as_string" : "1980",
"key" : 315532800000,
"doc_count" : 3
},
{
"key_as_string" : "1981",
"key" : 347155200000,
"doc_count" : 2
},
{
"key_as_string" : "1982",
"key" : 378691200000,
"doc_count" : 3
},
{
"key_as_string" : "1983",
"key" : 410227200000,
"doc_count" : 2
},
{
"key_as_string" : "1984",
"key" : 441763200000,
"doc_count" : 8
},
{
"key_as_string" : "1985",
"key" : 473385600000,
"doc_count" : 15
},
{
"key_as_string" : "1986",
"key" : 504921600000,
"doc_count" : 19
},
{
"key_as_string" : "1987",
"key" : 536457600000,
"doc_count" : 16
},
{
"key_as_string" : "1988",
"key" : 567993600000,
"doc_count" : 27
},
{
"key_as_string" : "1989",
"key" : 599616000000,
"doc_count" : 24
},
{
"key_as_string" : "1990",
"key" : 631152000000,
"doc_count" : 35
},
{
"key_as_string" : "1991",
"key" : 662688000000,
"doc_count" : 31
},
{
"key_as_string" : "1992",
"key" : 694224000000,
"doc_count" : 36
},
{
"key_as_string" : "1993",
"key" : 725846400000,
"doc_count" : 46
},
{
"key_as_string" : "1994",
"key" : 757382400000,
"doc_count" : 45
},
{
"key_as_string" : "1995",
"key" : 788918400000,
"doc_count" : 57
},
{
"key_as_string" : "1996",
"key" : 820454400000,
"doc_count" : 56
},
{
"key_as_string" : "1997",
"key" : 852076800000,
"doc_count" : 57
},
{
"key_as_string" : "1998",
"key" : 883612800000,
"doc_count" : 39
},
{
"key_as_string" : "1999",
"key" : 915148800000,
"doc_count" : 28
},
{
"key_as_string" : "2000",
"key" : 946684800000,
"doc_count" : 15
}
]
}
}
}
6.8 query_string 查询
- query_string 查询,如果熟悉lucene的查询语法,我们可以直接?用lucene查询语法写?一个查询串进行查询,ES中接到请求后,通过查询解析器 ,解析查询串生成对应的查询。
6.8.1 query_string AND OR 单字段查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"query_string": {
"default_field": "displayNameEn",
"query": "james OR curry"
}
},
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 7,
"relation" : "eq"
},
"max_score" : 5.4989905,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "123",
"_score" : 5.4989905,
"_source" : {
"countryEn" : "United States",
"teamName" : "独行侠",
"birthDay" : 651384000000,
"country" : "美国",
"teamCityEn" : "Dallas",
"code" : "seth_curry",
"displayAffiliation" : "Duke/United States",
"displayName" : "賽斯 库里",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "83.9 公斤",
"teamCity" : "达拉斯",
"playYear" : 6,
"jerseyNo" : "30",
"teamNameEn" : "Mavericks",
"draft" : 2013,
"displayNameEn" : "Seth Curry",
"heightValue" : 1.88,
"birthDayStr" : "1990-08-23",
"position" : "后卫",
"age" : 29,
"playerId" : "203552"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "124",
"_score" : 5.4989905,
"_source" : {
"countryEn" : "United States",
"teamName" : "勇士",
"birthDay" : 574318800000,
"country" : "美国",
"teamCityEn" : "Golden State",
"code" : "stephen_curry",
"displayAffiliation" : "Davidson/United States",
"displayName" : "斯蒂芬 库里",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "86.2 公斤",
"teamCity" : "金州",
"playYear" : 10,
"jerseyNo" : "30",
"teamNameEn" : "Warriors",
"draft" : 2009,
"displayNameEn" : "Stephen Curry",
"heightValue" : 1.9,
"birthDayStr" : "1988-03-14",
"position" : "后卫",
"age" : 31,
"playerId" : "201939"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
}
]
}
}
6.8.2 query_string AND OR 多字段查询 GET/POST请求
localhost:9200/nba/_search
{
"query": {
"query_string": {
"fields": [
"displayNameEn",
"teamNameEn"
],
"query": "james AND Rockets"
}
},
"size": 3
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 7.9719486,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "214",
"_score" : 7.9719486,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
}
}
]
}
}
七、ElasticSerach 高级搜索
7.1 索引别名的使用
- 在开发中,随着业务需求的迭代,较老的业务逻辑就要面临更新甚至是重构,而对于es来说,为了了适应新的业务逻辑,可能就要对原有的索引做一些修改,比如对某些字段做调整,甚至是重建索引。而做这些操作的时候,可能会对业务造成影响,甚至是停机调整等问题。由此,es提供了索引别名来解决这些问题。 索引别名就像一个快捷方式或是软连接,可以指向一个或多个索引,也可以给任意一个需要索引名的API来使?用。别名的应用为程序提供了极大地灵活性
7.1.1 查询别名 GET请求
localhost:9200/nba/_alias
localhost:9200/_aliases
{
"nba": {
"aliases": {}
},
".kibana_task_manager": {
"aliases": {}
},
".kibana_1": {
"aliases": {
".kibana": {}
}
}
}
7.1.2 新增别名 POST请求
localhost:9200/_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
{
"acknowledged": true
}
7.1.3 删除别名 POST请求
localhost:9200/_aliases
{
"actions": [
{
"remove": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
{
"acknowledged": true
}
7.1.4 重命名别名 POST请求
localhost:9200/_aliases
{
"actions": [
{
"remove": {
"index": "nba",
"alias": "nba_v1.0"
}
},
{
"add": {
"index": "nba",
"alias": "nba_v2.0"
}
}
]
}
{
"acknowledged": true
}
7.1.5 通过别名获取索引 GET请求
localhost:9200/nba_v2.0
{
"nba": {
"aliases": {
"nba_v2.0": {}
},
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"code": {
"type": "text"
},
"country": {
"type": "text"
},
"countryEn": {
"type": "text"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "text"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
},
"settings": {
"index": {
"creation_date": "1573300240575",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "X7FLu-8sRcijAYKUkgBZQQ",
"version": {
"created": "7020199"
},
"provided_name": "nba"
}
}
}
}
7.1.6 通过别名写索引 POST请求
localhost:9200/nba_v2.0/_doc/566
{
"countryEn": "Croatia",
"teamName": "快船",
"birthDay": 858661200000,
"country": "克罗地亚",
"teamCityEn": "LA",
"code": "ivica_zubac",
"displayAffiliation": "Croatia",
"displayName": "伊维察 祖巴茨哥哥",
"schoolType": "",
"teamConference": "西部",
"teamConferenceEn": "Western",
"weight": "108.9 公斤",
"teamCity": "洛杉矶",
"playYear": 3,
"jerseyNo": "40",
"teamNameEn": "Clippers",
"draft": 2016,
"displayNameEn": "Ivica Zubac",
"heightValue": 2.16,
"birthDayStr": "1997-03-18",
"position": "中锋",
"age": 22,
"playerId": "1627826"
}
{
"_index": "nba",
"_type": "_doc",
"_id": "566",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 566,
"_primary_term": 1
}
7.2 如何重建索引
- nba取一个别名nba_latest, nba_latest作为对外使用
localhost:9200/_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_latest"
}
}
]
}
{
"acknowledged" : true
}
- 新增一个索引nba_20220101,结构复制于nba索引,根据业务要求修改字段
localhost:9200/nba_20220101
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"code": {
"type": "text"
},
"country": {
"type": "keyword"
},
"countryEn": {
"type": "keyword"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "keyword"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "nba_20220101"
}
- 将nba数据同步到nba_20220101
localhost:9200/_reindex
localhost:9200/_reindex?wait_for_completion=false
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220101"
}
}
{
"took" : 102,
"timed_out" : false,
"total" : 566,
"updated" : 0,
"created" : 566,
"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" : [ ]
}
- 给nba_20220101添加别名nba_latest,删除nba别名nba_latest
localhost:9200/_aliases
{
"actions": [
{
"add": {
"index": "nba_20220101",
"alias": "nba_latest"
}
}
]
}
{
"acknowledged" : true
}
- 删除nba索引
localhost:9200/nba
{
"acknowledged" : true
}
7.3 refresh操作
-
新的数据一添加到索引中立马就能搜索到,但是真实情况不是这样的 -
我们使?用链式命令请求,先添加?一个?文档,再?立刻搜索
curl -X PUT localhost:9200/star/_doc/888 -H 'Content-Type:
application/json' -d '{ "displayName": "蔡徐坤" }'
curl -X GET localhost:9200/star/_doc/_search?pretty
- 强制刷新
curl -X PUT localhost:9200/star/_doc/666?refresh -H 'Content-Type:
application/json' -d '{ "displayName": "杨超越" }'
curl -X GET localhost:9200/star/_doc/_search?pretty
- 修改默认更更新时间(默认时间是1s)
localhost:9200/star/_settings
{
"index":{
"refresh_interval": "5s"
}
}
{
"acknowledged" : true
}
- 将refresh关闭
localhost:9200/star/_settings
{
"index":{
"refresh_interval": "-1"
}
}
{
"acknowledged" : true
}
7.4 高亮查询
localhost:9200/nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
},
"highlight": {
"fields": {
"displayNameEn": {}
}
},
"size": 2
}
{
"query": {
"match": {
"displayNameEn": "james"
}
},
"highlight": {
"fields": {
"displayNameEn": {
"pre_tags": ["<h1>"],
"post_tags": ["</h1>"]
}
}
},
"size": 2
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 4.699642,
"hits" : [
{
"_index" : "nba_20220101",
"_type" : "_doc",
"_id" : "214",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "火箭",
"birthDay" : 620107200000,
"country" : "美国",
"teamCityEn" : "Houston",
"code" : "james_harden",
"displayAffiliation" : "Arizona State/United States",
"displayName" : "詹姆斯 哈登",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "99.8 公斤",
"teamCity" : "休斯顿",
"playYear" : 10,
"jerseyNo" : "13",
"teamNameEn" : "Rockets",
"draft" : 2009,
"displayNameEn" : "James Harden",
"heightValue" : 1.96,
"birthDayStr" : "1989-08-26",
"position" : "后卫",
"age" : 30,
"playerId" : "201935"
},
"highlight" : {
"displayNameEn" : [
"<em>James</em> Harden"
]
}
},
{
"_index" : "nba_20220101",
"_type" : "_doc",
"_id" : "266",
"_score" : 4.699642,
"_source" : {
"countryEn" : "United States",
"teamName" : "国王",
"birthDay" : 854082000000,
"country" : "美国",
"teamCityEn" : "Sacramento",
"code" : "justin_james",
"displayAffiliation" : "United States",
"displayName" : "贾斯汀 詹姆斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "86.2 公斤",
"teamCity" : "萨克拉门托",
"playYear" : 0,
"jerseyNo" : "",
"teamNameEn" : "Kings",
"draft" : 2019,
"displayNameEn" : "Justin James",
"heightValue" : 2.01,
"birthDayStr" : "1997-01-24",
"position" : "后卫-前锋",
"age" : 22,
"playerId" : "1629713"
},
"highlight" : {
"displayNameEn" : [
"Justin <em>James</em>"
]
}
}
]
}
}
7.5 查询建议
- 查询建议,是为了给用户提供更好的搜索体验。包括:词条检查,自动补全
- Term suggester
- Phrase suggester
- Completion suggester
text | 指定搜索文本 |
---|
field | 获取建议词的搜索字段 | analyzer | 指定分词器 | size | 每个词返回的最大建议词数 | sort | 如何对建议词进行排序,可用选项:score:先按评分排序、再按文档频率排、term顺序;frequency:先按文档频率排,再按评分、term顺序排。 | suggest_mode | 建议模式,控制提供建议词的方式:missing:仅在搜索的词项在索引中不存在时才提供建议词,默认值;popular:仅建议文档频率比搜索词项高的词;always:总是提供匹配的建议词 |
7.5.1 term suggester
localhost:9200/nba_latest/_search
{
"suggest": {
"my-suggest": {
"text": "jamse",
"term": {
"suggest_mode": "missing",
"field": "displayNameEn"
}
}
}
}
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"my-suggest" : [
{
"text" : "jamse",
"offset" : 0,
"length" : 5,
"options" : [
{
"text" : "james",
"score" : 0.8,
"freq" : 5
},
{
"text" : "jamal",
"score" : 0.6,
"freq" : 2
},
{
"text" : "jake",
"score" : 0.5,
"freq" : 1
},
{
"text" : "jose",
"score" : 0.5,
"freq" : 1
}
]
},
{
"text" : "hardne",
"offset" : 6,
"length" : 6,
"options" : [
{
"text" : "harden",
"score" : 0.8333333,
"freq" : 1
}
]
}
]
}
}
7.5.2 phrase suggester
localhost:9200/nba_latest/_search
{
"suggest": {
"my-suggest": {
"text": "jamse hardne",
"phrase": {
"field": "displayNameEn"
}
}
}
}
{
"took" : 26,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"my-suggest" : [
{
"text" : "jamse hardne",
"offset" : 0,
"length" : 12,
"options" : [
{
"text" : "james hardne",
"score" : 0.0025682184
},
{
"text" : "jamal hardne",
"score" : 0.0016773979
},
{
"text" : "jamse harden",
"score" : 0.0016222595
},
{
"text" : "jake hardne",
"score" : 0.001299489
},
{
"text" : "jose hardne",
"score" : 0.001299489
}
]
}
]
}
}
7.5.3 completion suggester
localhost:9200/nba_latest/_search
{
"suggest": {
"my-suggest": {
"text": "Miam",
"completion":{
"field": "teamCityEn"
}
}
}
}
八、NBA中国官网实战
- 官方网站
https://china.nba.com/playerindex/
8.1 项目搭建
- springboot 整合 elasticsearch 和 mysql
8.1.1 POM依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.2.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.2.1</version>
</dependency>
8.1.2 YML依赖
elasticsearch:
host: localhost
port: 9200
8.1.3 ElasticSearch配置文件
package com.frame.elasticsearch.config;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
@SpringBootConfiguration
@ConfigurationProperties(prefix = "elasticsearch")
@Data
public class ElasticSearchConfig {
private String host;
private Integer port;
@Bean
public RestHighLevelClient restHighLevelClient() {
HttpHost http = new HttpHost(host, port, "http");
return new RestHighLevelClient(RestClient.builder(http));
}
}
8.1.4 ElasticSearch CRUD入门操作
import org.springframework.cglib.beans.BeanMap;
import java.util.HashMap;
import java.util.Map;
public class BeanUtils {
public static <T> Map<String, Object> beanToMap(T bean) {
HashMap<String, Object> map = new HashMap<>();
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
for (Object o : beanMap.keySet()) {
if (beanMap.get(o) != null) {
map.put(o.toString(), beanMap.get(o));
}
}
}
return map;
}
}
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frame.common.entity.NbaPlayer;
import com.frame.common.utils.BeanUtils;
import com.frame.elasticsearch.mapper.NbaPlayerMapper;
import com.frame.elasticsearch.service.NbaPlayerService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Map;
@Service
public class NbaPlayerServiceImpl implements NbaPlayerService {
private final String NBA_INDEX = "nba_latest";
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public boolean addPlayer(NbaPlayer player, String id) throws IOException {
IndexRequest request = new IndexRequest(NBA_INDEX)
.id(id)
.source(BeanUtils.beanToMap(player));
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response));
return false;
}
@Override
public Map<String, Object> getPlayer(String id) throws IOException {
GetRequest getRequest = new GetRequest(NBA_INDEX, id);
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
return getResponse.getSource();
}
@Override
public boolean updatePlayer(NbaPlayer nbaPlayer, String id) throws IOException {
UpdateRequest updateRequest = new UpdateRequest(NBA_INDEX, id)
.doc(BeanUtils.beanToMap(nbaPlayer));
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(update));
return true;
}
@Override
public boolean deletePlayer(String id) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest(NBA_INDEX, id).id(id);
DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete);
return true;
}
@Override
public boolean deleteAllPlayer() throws IOException {
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(NBA_INDEX);
BulkByScrollResponse bulkByScrollResponse = restHighLevelClient.deleteByQuery(
deleteByQueryRequest,
RequestOptions.DEFAULT
);
return true;
}
}
import com.frame.common.entity.NbaPlayer;
import com.frame.elasticsearch.service.NbaPlayerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class NbaPlayerServiceImplTest {
@Autowired
NbaPlayerService nbaPlayerService;
@Test
public void addPlayer() throws IOException {
NbaPlayer nbaPlayer = new NbaPlayer()
.setId(999)
.setDisplayName("苍井空");
boolean result = nbaPlayerService.addPlayer(nbaPlayer, "999");
}
@Test
public void getPlayer() throws IOException {
Map<String, Object> player = nbaPlayerService.getPlayer("999");
System.out.println(player);
}
@Test
public void updatePlayer() throws IOException {
NbaPlayer nbaPlayer = new NbaPlayer()
.setId(999)
.setDisplayName("小泽玛利亚");
boolean result = nbaPlayerService.updatePlayer(nbaPlayer, "999");
}
@Test
public void deletePlayer() throws IOException {
boolean result = nbaPlayerService.deletePlayer("999");
}
@Test
public void deleteAllPlayer() throws IOException {
boolean result = nbaPlayerService.deleteAllPlayer();
}
}
8.2 接口开发
import com.frame.common.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Result<T> {
private Integer code;
private String msg;
private T data;
public static <T> Result success() {
return success(null);
}
public static <T> Result success(T data) {
return success(ResultEnum.SUCCESS.getCode(), data);
}
public static <T> Result success(Integer code, T data) {
return success(code, ResultEnum.SUCCESS.getMsg(), data);
}
public static <T> Result success(String msg, T data) {
return success(ResultEnum.SUCCESS.getCode(), msg, data);
}
public static <T> Result success(Integer code, String msg, T data) {
return new Result<T>(code, msg, data);
}
public static <T> Result fail() {
return fail(ResultEnum.FAIL.getCode());
}
public static <T> Result fail(Integer code) {
return fail(code, ResultEnum.FAIL.getMsg());
}
public static <T> Result fail(String msg) {
return fail(ResultEnum.FAIL.getCode(), msg);
}
public static <T> Result fail(Integer code, String msg) {
return success(code, msg, null);
}
}
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum ResultEnum {
SUCCESS(200, "操作成功"),
FAIL(400,"操作失败")
;
private Integer code;
private String msg;
}
8.2.1 将数据库数据导入到elastic search
@GetMapping("/import")
public Result importAllPlayer() {
try {
nbaPlayerService.importAllPlayer();
return Result.success("数据导入成功");
} catch (IOException e) {
log.error("ElasticSearch导入数据失败");
e.printStackTrace();
return Result.fail("ElasticSearch导入数据失败");
}
}
@Override
@Transactional
public boolean importAllPlayer() throws IOException {
List<NbaPlayer> nbaPlayers = list();
for (NbaPlayer e : nbaPlayers) {
addPlayer(e, String.valueOf(e.getId()));
}
return true;
}
8.2.2 通过姓名查找球员
@GetMapping("/searchMatch")
public Result searchMatch(@RequestParam("key") String key,
@RequestParam("val") String val,
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
List<NbaPlayer> nbaPlayers = null;
try {
nbaPlayers = nbaPlayerService.searchTerm(key, val, page, limit);
} catch (IOException e) {
log.error("searchMatch失败,参数[key={}]][val={}]]", key, val);
e.printStackTrace();
}
return Result.success(nbaPlayers);
}
@Override
public List<NbaPlayer> searchMatch(String key, String val, Integer page, Integer limit) throws IOException {
SearchRequest searchRequest = new SearchRequest(NBA_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder
.query(QueryBuilders.termQuery(key, val))
.from(page)
.size(limit);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
return Stream.of(hits)
.map(e -> JSON.parseObject(e.getSourceAsString(), NbaPlayer.class))
.collect(Collectors.toList());
}
8.2.3 通过国家或者球队查询球员
@GetMapping("/searchTerm")
public Result searchTerm(@RequestParam(value = "country", required = false) String country,
@RequestParam(value = "teamName", required = false) String teamName,
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
List<NbaPlayer> nbaPlayers = null;
try {
if (StringUtils.isNotBlank(country)) {
nbaPlayers = nbaPlayerService.searchTerm("country", country, page, limit);
} else {
nbaPlayers = nbaPlayerService.searchTerm("teamName", teamName, page, limit);
}
} catch (IOException e) {
log.error("searchTerm失败,参数[country={}]][teamName={}]]", country, teamName);
e.printStackTrace();
}
return CollectionUtils.isNotEmpty(nbaPlayers) ? Result.success(nbaPlayers) : Result.success();
}
@Override
public List<NbaPlayer> searchTerm(String key, String val, Integer page, Integer limit) throws IOException {
SearchRequest searchRequest = new SearchRequest(NBA_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder
.query(QueryBuilders.termQuery(key, val))
.from(page)
.size(limit);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
return Stream.of(hits)
.map(e -> JSON.parseObject(e.getSourceAsString(), NbaPlayer.class))
.collect(Collectors.toList());
}
8.2.4 通过姓名字母查找球员
@GetMapping("/searchPrefix")
public Result searchPrefix(@RequestParam(value = "prefix", required = false, defaultValue = "A") String prefix,
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
List<NbaPlayer> nbaPlayers = null;
try {
nbaPlayers = nbaPlayerService.searchPrefix(prefix, page, limit);
} catch (IOException e) {
log.error("searchPrefix失败,参数[prefix={}]]", prefix);
e.printStackTrace();
}
return CollectionUtils.isNotEmpty(nbaPlayers) ? Result.success(nbaPlayers) : Result.success();
}
@Override
public List<NbaPlayer> searchPrefix(String prefix, Integer page, Integer limit) throws IOException {
SearchRequest searchRequest = new SearchRequest(NBA_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder
.query(QueryBuilders.prefixQuery("displayNameEn.keyword", prefix))
.from(page)
.size(limit);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
return Stream.of(hits)
.map(e -> JSON.parseObject(e.getSourceAsString(), NbaPlayer.class))
.collect(Collectors.toList());
}
九、 走入高可用分布式集群
十、 深入挖掘ElasticSearch原理
附录
Elasticsearch-菜鸟教程
ElasticSearch7j-gitee入门基础
SpringBoot 操作 ElasticSearch 详解
mall整合Elasticsearch实现商品搜索
SpringBoot应用整合ELK实现日志收集
Elasticsearch项目实战,商品搜索功能设计与实现
RequestParam(“val”) String val, @RequestParam(value = “page”, required = false, defaultValue = “0”) Integer page, @RequestParam(value = “limit”, required = false, defaultValue = “10”) Integer limit) { List nbaPlayers = null; try { nbaPlayers = nbaPlayerService.searchTerm(key, val, page, limit); } catch (IOException e) { log.error(“searchMatch失败,参数[key={}]][val={}]]”, key, val); e.printStackTrace(); } return Result.success(nbaPlayers); }
- service层
```java
/**
* 通过姓名查找球员
*/
@Override
public List<NbaPlayer> searchMatch(String key, String val, Integer page, Integer limit) throws IOException {
// 获取 SearchRequest
SearchRequest searchRequest = new SearchRequest(NBA_INDEX);
// 创建 SearchSourceBuilder 用于查询语句
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder
// 查询 通过match查询 key 字段名称 val 字段值
.query(QueryBuilders.termQuery(key, val))
// 起始页
.from(page)
// 显示条数
.size(limit);
// 设置 请求源 理解为设置查询语句
searchRequest.source(searchSourceBuilder);
// 获取 SearchResponse 查询数据
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
// 获取碰撞之后的结果 (固定写法)
SearchHit[] hits = searchResponse.getHits().getHits();
// 进行对象转换并返回
return Stream.of(hits)
.map(e -> JSON.parseObject(e.getSourceAsString(), NbaPlayer.class))
.collect(Collectors.toList());
}
8.2.3 通过国家或者球队查询球员
@GetMapping("/searchTerm")
public Result searchTerm(@RequestParam(value = "country", required = false) String country,
@RequestParam(value = "teamName", required = false) String teamName,
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
List<NbaPlayer> nbaPlayers = null;
try {
if (StringUtils.isNotBlank(country)) {
nbaPlayers = nbaPlayerService.searchTerm("country", country, page, limit);
} else {
nbaPlayers = nbaPlayerService.searchTerm("teamName", teamName, page, limit);
}
} catch (IOException e) {
log.error("searchTerm失败,参数[country={}]][teamName={}]]", country, teamName);
e.printStackTrace();
}
return CollectionUtils.isNotEmpty(nbaPlayers) ? Result.success(nbaPlayers) : Result.success();
}
@Override
public List<NbaPlayer> searchTerm(String key, String val, Integer page, Integer limit) throws IOException {
SearchRequest searchRequest = new SearchRequest(NBA_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder
.query(QueryBuilders.termQuery(key, val))
.from(page)
.size(limit);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
return Stream.of(hits)
.map(e -> JSON.parseObject(e.getSourceAsString(), NbaPlayer.class))
.collect(Collectors.toList());
}
8.2.4 通过姓名字母查找球员
@GetMapping("/searchPrefix")
public Result searchPrefix(@RequestParam(value = "prefix", required = false, defaultValue = "A") String prefix,
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
List<NbaPlayer> nbaPlayers = null;
try {
nbaPlayers = nbaPlayerService.searchPrefix(prefix, page, limit);
} catch (IOException e) {
log.error("searchPrefix失败,参数[prefix={}]]", prefix);
e.printStackTrace();
}
return CollectionUtils.isNotEmpty(nbaPlayers) ? Result.success(nbaPlayers) : Result.success();
}
@Override
public List<NbaPlayer> searchPrefix(String prefix, Integer page, Integer limit) throws IOException {
SearchRequest searchRequest = new SearchRequest(NBA_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder
.query(QueryBuilders.prefixQuery("displayNameEn.keyword", prefix))
.from(page)
.size(limit);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
return Stream.of(hits)
.map(e -> JSON.parseObject(e.getSourceAsString(), NbaPlayer.class))
.collect(Collectors.toList());
}
九、 走入高可用分布式集群
十、 深入挖掘ElasticSearch原理
附录
Elasticsearch-菜鸟教程
ElasticSearch7j-gitee入门基础
SpringBoot 操作 ElasticSearch 详解
mall整合Elasticsearch实现商品搜索
SpringBoot应用整合ELK实现日志收集
Elasticsearch项目实战,商品搜索功能设计与实现
|