【ElasticSearch】ElasticSearch | ES常用查询命令汇总
CrazyPanda发表于:2024-07-29 17:49:04浏览:241次
原文链接https://zhuanlan.zhihu.com/p/651152005
今天和大家分享ES中的查询命令。相信大家对SQL查询很熟悉,但是你知道如何在ES中实现SQL的select、where、group by等功能吗?
下面分享一些ES中常用的查询命令,希望对你有所帮助。
1.实现select功能:_source
1.1 选择需要的字段
GET dws_person_info/_search
{
"_source": ["name","phone_number","address"]
}
1.2 剔除不需要的字段
GET dws_person_info/_search
{
"_source": {
"excludes": ["age","email"]
}
}
1.3 选择需要的,剔除不需要的
GET dws_person_info/_search
{
"_source": {
"includes": ["name","phone_number","address"],
"excludes": ["age","email"]
}
}
2.实现like功能:match
2.1 模数匹配
GET dws_person_info/_search
{
"query":{
"match": {
"address": "成都市"
}
}
}
上面的功能,类似于SQL中的address like '%成都市%'。但是此功能对text类型才生效,当查询的是非字符串时,为精确匹配。
2.2 精确匹配
GET dws_person_info/_search
{
"query":{
"match": {
"address.keyword": "成都市高新区"
}
}
}
当然,term也可以实现精确查询。
GET dws_person_info/_search
{
"query":{
"term": {
"address": "成都市高新区"
}
}
}
多值精确匹配,类似于SQL中的in功能。
GET dws_person_info/_search
{
"query": {
"terms": {
"address": ["成都市高新区", "成都市锦江区", "成都市成华区"]
}
}
}
3.实现between…and…功能:range
3.1 范围查询
gte大于等于,g为greatest首字母;lte小于等于,l为least首字母。
GET dws_person_info/_search
{
"query":{
"range": {
"age": {
"gte": 23,
"lte": 30
}
}
}
}
4.实现where中的and功能:bool
4.1 must
必须匹配,类似于and。must_not 为不匹配。
GET dws_person_info/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "男性"
}
},
{
"match": {
"address": "成都市成华区"
}
}
]
}
}
}
4.2 should
类似于or功能
GET dws_person_info/_search
{
"query": {
"bool": {
"minimum_should_match": 1, --满足任意一个即可
"should": [
{
"match": {
"age": 28
}
},
{
"match": {
"gender": "男性"
}
},
{
"match": {
"address": "成都市金牛区"
}
}
]
}
}
}
5.实现group by 功能:aggs
5.1 分组聚合
GET dws_person_info/_search
{
"query":{
"match": {
"address": "成都市"
}
},
"aggs": {
"addressAgg": {
"terms": {
"field": "address", --按地址分组
"size": 10,
"min_doc_count": 55
}
},
"ageAvg":{
"avg": {
"field": "age" --年龄平均值
}
}
},
"size": 20
}
6.实现order by 功能:sort
6.1 排序
按照age降序,height升序。
GET dws_person_info/_search
{
"query": {
"match_all": {}
},
"sort":[ {
"age": "desc"
},{
"height": "asc"
}]
}
7.实现limit功能:from size
7.1 从哪条开始查询多少条
GET dws_person_info/_search
{
"query": {
"match_all": {}
},
"from": 1, --从1开始
"size": 10 --查10条
}
8.实现count功能:_count
8.1 查询表中有多少条数据
GET dws_person_info/_count
猜你喜欢
- 【数据库】MongoDB特点
- MongoDB是一个基于分布式文件存储的开源数据库系统,其中主要特点包括:高性能:MongoDB提供高性能的数据插入和查询操作。高扩展性:MongoDB支持自动分片,可以横向扩展数据库的大小。易于使用:MongoDB支持多种编程语言,并提供简单易用的查询语言。五模式:MongoDB是无模式的,意味着它不要求数据结构严格遵守预定义的模式。复制集:MongoDB提供复制集功能,可以保证数据的高可用性。内置分析功能:MongoDB提供内置的数据分析功能,可以帮助开发者分析数据库性能。以下是一个简单的M
- 【ElasticSearch】Elaticsearch8.9.0安装配置集群
- 下载安装包从官网下载8.9.0安装包# elasticsearch-8.9.0-x86_64.rpm filebeat-8.9.0-x86_64.rpm kibana-8.9.0-x86_64.rpm安装系统环境查看# cat /etc/redhat-release Rocky Linux release 9.3 (Blue Onyx) # uname
- 【Elasticsearch】linux安装Elasticsearch及ik分词器
- 1. 资料准备环境:docker创建的宝塔lnmpes下载地址https://www.elastic.co/cn/downloads/past-releases#elasticsearchik下载地址https://release.infinilabs.com/analysis-ik/stable/ es和ik下载对应的版本,这里我下载的版本都是8.13.42. 安装elasticsearch由于我是宝塔部署的环境,所以直接将es解压到了/www/elasticsearch目录,解压完成后,在e
栏目分类全部>