elasticsearch - 在给定过滤器的弹性搜索中获取不同的值
问题描述
我想在给定过滤器的索引中获取不同的字段,例如
以下数据
|Name |Age |Country|Amount|
================================
|Alfreds |34 |Germeny|100 |
--------------------------------
|Andrew |43 |U.K |333 |
--------------------------------
|Raj |54 |India |4553 |
--------------------------------
|John |43 |U.K |325 |
--------------------------------
|Ana |32 |Mexico |56 |
--------------------------------
|Christina |22 |Sweden |345 |
我想有一个独特的国家名称时说,金额大于 300。
实现相同目标的最佳方法是什么?
解决方案
如果您只对获取不同值的数量感兴趣,则可以使用字段聚合:
{
"query": {
"range": {
"amount": {
"gt": 300
}
}
},
"size": 0,
"aggs" : {
"uniq_country" : {
"terms" : {"field" : "country"}
}
}
}
上面的查询将为您提供以下结果:
"aggregations": {
"uniq_country": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "u.k",
"doc_count": 2
},
{
"key": "india",
"doc_count": 1
},
{
"key": "sweden",
"doc_count": 1
}
]
}
}
推荐阅读
- wordpress - 单个 URL 的 Worpress htaccess 重写规则
- mongodb - 如何使用猫鼬$near?(类型数据位置)
- postgresql - 码头工人 | Postgres 数据库未初始化且未指定超级用户密码
- python - 保存启用宏的电子表格时出现 pywin/win32com 文件访问错误
- java - Spring Security - REST API 的授权
- python - Python:如果使用正则表达式从另一个字符串中提取子字符串
- php - Wordpress+ACF - 通过转发器字段中的多选选项进行过滤
- search-engine - 将 TF-IDF 值存储在倒排索引中
- apache - 现有连接被远程主机强行关闭(net::ERR_HTTP2_PROTOCOL_ERROR)
- php - Nginx - 单一配置中的 Laravel 和 Reactjs