千家信息网

elasticsearch语法有哪些

发表于:2024-11-23 作者:千家信息网编辑
千家信息网最后更新 2024年11月23日,本篇内容主要讲解"elasticsearch语法有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"elasticsearch语法有哪些"吧!语法 ///
千家信息网最后更新 2024年11月23日elasticsearch语法有哪些

本篇内容主要讲解"elasticsearch语法有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"elasticsearch语法有哪些"吧!

语法

 ///

命令

查询所有索引GET /_cat/indices
GET /_cat/indices?v
创建索引PUT /user
删除索引DELETE /user
添加文档
删除文档
查询文档GET /user/_search
name字段包含"张三"或"李四"的

GET /user/_search
{
"query": {
"match":{"name": "张三 李四"}
},
"from": 0,
"size": 20,
"_source": ["name"]
}

匹配所有文档GET /user/_search
{
"query": {
"match_all":{}
}
}
所有name字段,包含"张三"的GET /user/_search
{
"query": {
"match_phrase":{"name": "张三"}
}
}
bool查询
所有数据中,name字段同时
包含"张三"、"李四"的
GET /user/_search
{
"query": {
"bool": {
"must": [
{"match": {"name": "张三"}},
{"match": {"name": "李四"}},
],
}
}
}
bool查询
所有数据中,name字段
包含"张三"或"李四"的
GET /user/_search
{
"query": {
"bool": {
"should": [
{"match": {"name": "张三"}},
{"match": {"name": "李四"}},
],
}
}
}
bool查询
所有数据中,name字段同时
不包含"张三"也不包含"李四"的
GET /user/_search
{
"query": {
"bool": {
"must_not": [
{"match": {"name": "张三"}},
{"match": {"name": "李四"}},
],
}
}
}
bool查询
所有数据中,name字段
包含"张三",但不包含"李四"的
GET /user/_search
{
"query": {
"bool": {
"must": [
{"match": {"name": "张三"}},
],
"must_not": [
{"match": {"name": "李四"}},
],
}
}
}
过滤查询
所有数据中,10<=age<=20的
GET /user/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}},
],
"filter": {
"range": {"age": {"gte":10, "lte": 20}}
}
}
}
}
聚合查询
根据state分组,倒序,统计state、count(id)
等同于:
SELECT state, COUNT(*) FROM bank
GROUP BY state
ORDER BY COUNT(*) DESC

说明:
size=0 是为了不要查询文档,只要聚合结果

GET /user/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}

聚合查询
根据state分组,倒序,统计state、
count(id)、avg(balance)
等同于:
SELECT state, COUNT(*), avg(balance) FROM bank
GROUP BY state
ORDER BY COUNT(*) DESC
GET /user/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
聚合查询
根据state分组,balance平均值倒序,统计state、
count(id)、avg(balance)
等同于:
SELECT state, COUNT(*), avg(balance) FROM bank
GROUP BY state
ORDER BY avg(balance) DESC
GET /user/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
聚合查询
根据age年龄段分组,然后按照性别分组,
然后获取balance平均值
GET /user/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}

到此,相信大家对"elasticsearch语法有哪些"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0