六、ElasticSearch 8.6.0 快速使用 - ES 查询数据
hi,我是温新,一名 PHPer
ES 版本:ElasticSearch 8.6.0
系统版本:Rocky Linux 9.1
学习目标:掌握数据查询
本篇文章结合官方文档编写及参考网络资料编写,虽非全部原创,但也是结合了自己的理解,若转载请附带本文 URL,编写不易,持续编写更不易,谢谢!
查询某个索引下的所有文档
语法:
GET <index_name>/_search
案例:
GET ziruchu/_search
term 查询某个字段
term
不会分词查询,用可用于那些不分词的字段,如 category
案例:
GET ziruchu/_search
{
"query": {
"term": {
"name": {
"value": "张"
}
}
}
}
match 分词并集查询
GET ziruchu/_search
{
"query": {
"match": {
"name": "张无忌"
}
}
}
match 分词交集查询
GET ziruchu/_search
{
"query": {
"match": {
"name": {
"query": "张无忌",
"operator": "and"
}
}
}
}
wildcard 通配符查询
GET ziruchu/_search
{
"query": {
"wildcard": {
"name": {
"value": "张*"
}
}
}
}
prefix 前缀查询
GET ziruchu/_search
{
"query": {
"prefix": {
"name": {
"value": "张"
}
}
}
}
正则查询
GET ziruchu/_search
{
"query": {
"regexp": {
"name": "\\S+"
}
}
}
range 范围查询并排序
GET ziruchu/_search
{
"query": {
"range": {
"age": {
"gte": 1,
"lte": 50
}
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
query_string 查询
GET ziruchu/_search
{
"query": {
"query_string": {
"fields": ["name"],
"query": "张 OR 赵"
}
}
}
simple_query_string 查询
GET ziruchu/_search
{
"query": {
"simple_query_string": {
"query": "三",
"fields": ["name"]
}
}
}
bool 查询
GET ziruchu/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"name": {
"value": "张三丰"
}
}
}
],
"filter": [
{
"term": {
"age": "100"
}
}
]
}
}
}
聚合查询
GET ziruchu/_search
{
"query": {
"match": {
"name": "张无忌"
}
},
"aggs": {
"max_pre": {
"max": {
"field": "age"
}
}
}
}
分组查询
GET ziruchu/_search
{
"query": {
"match": {
"name": "张无忌"
}
},
"aggs": {
"group_name": {
"terms": {
"field": "age",
"size": 100
}
}
}
}
查询高亮
GET ziruchu/_search
{
"query": {
"match": {
"name": "赵敏"
}
},
"highlight": {
"fields": {
"name": {
"pre_tags": "<span color='red'>",
"post_tags": "</span>"
}
}
}
}