elasticsearch - 在弹性搜索中查找不同的值
问题描述
弹性搜索 7.10.0
动态映射:
{
"mappings": {
"dynamic_templates": [{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
]
}
}
Kibana 显示了以下索引映射:
{
"mappings": {
"_doc": {
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"fields": {
"raw": {
"type": "keyword"
}
},
"type": "text"
}
}
}
],
"properties": {
....filtered out other properties....
"Registry": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
....filtered out other properties....
}
}
} }
GET /iptree_index_base/_search?filter_path=hits.total.value,took,hits.hits._source.Registry
{
"aggs": {
"values": {
"terms": { "field": "Registry.raw" }
}
},
"sort" : [
{"Registry.raw" : {"order" : "asc"}}
]
}
结果:
{
"took" : 8,
"hits" : {
"total" : {
"value" : 19
},
"hits" : [
{
"_source" : {
"Registry" : "AFRINIC"
}
},
{
"_source" : {
"Registry" : "AFRINIC"
}
},
{
"_source" : {
"Registry" : "ARIN"
}
},
{
"_source" : {
"Registry" : "ARIN"
}
},
..Rest of duplicate results filtered out
]
}
}
期望的结果:
{
"took" : 8,
"hits" : {
"total" : {
"value" : 2
},
"hits" : [
{
"_source" : {
"Registry" : "AFRINIC"
}
},
{
"_source" : {
"Registry" : "ARIN"
}
}
]
}
}
Registry.raw 是一个关键字。我错过了什么?
解决方案
您对命中不感兴趣,但对聚合存储桶感兴趣。所以你正在寻找的查询是这个:
GET /iptree_index_base/_search?filter_path=hits.total.value,took,aggregations.values.buckets.key
{
"size": 0,
"aggs": {
"values": {
"terms": {
"field": "Registry.raw",
"order": {
"_key": "asc"
}
}
}
}
}
推荐阅读
- mysql - MySQL:如何并排显示列名和行字段值
- google-cloud-platform - 如何在 Google Cloud AI Platform 中的 Jupyter notebook 上使用 GPU
- android - 动态recyclerView Android kotlin
- c# - 如何根据首次激活的地区将应用程序设置为默认语言
- javascript - Express GET 路由无法识别
- python - 检查嵌套列表中是否存在位置
- java - How to get a dynamic proxy for a class using ByteBuddy
- sql - Access多表查询返回一个表的空白
- swift - 从 Objective-C 中的引用复制适应 Swift 中的值复制的策略
- powerbi - Power BI 安装:“您确定要取消 Microsoft Power BI Desktop 安装吗?”