使用 dmesg 来查看一些硬件或驱动程序的信息或问题。

elasticsearch6.0 buckets key 再过滤

Elasticsearch | 作者 xiaofancn | 发布于2018年06月01日 | 阅读数:3425

使用的是elasticsearch6.0版本
 
 
mapping
{
"mappings": {
"product": {
"properties": {
"id": {
"type": "text",
"fielddata": true
},
"address": {
"type": "keyword"
}
}
}
}
}

 
数据
{"id":"n1", "address":["中国,北京市,北京市","中国,河北省,石家庄"]}
{"id":"n2", "address":["中国,北京市,北京市","中国,江苏省,南京市"]}
{"id":"n3", "address":["中国,北京市,北京市","中国,河北省,石家庄"]}

统计
中国 的记录数
中国,北京市的记录数
中国,河北省的记录数
怎么写
已邀请:

strglee

赞同来自: xiaofancn

address你设置成了keyword 这个没法分词了 你的需求 “中国,北京市的记录数 中国,河北省的记录数 ”也就弄不成了 建议修改一下mapping 
{
"mappings": {
"product": {
"properties": {
"country_province": {
"type": "text",
"fielddata": true,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"address": {
"type": "text",
"fielddata": true,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "text",
"fielddata": true,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
添加一个国家+城市字段 用于聚合省份 如果需要聚合国家 再新建一个country字段
所有字段都改成 可分词(用于搜索) keyword(用于聚合)两种类型 
 
导入数据
POST test/product/1
{
"id": "n1",
"address": [
"中国,北京市,北京市",
"中国,河北省,石家庄"
],
"country_province": [
"中国,北京市",
"中国,河北省"
]
}

POST test/product/2
{
"id": "n2",
"address": [
"中国,江苏省,南京市"
],
"country_province": [
"中国,江苏省"
]
}

POST test/product/3
{
"id": "n3",
"address": [
"中国,北京市,北京市",
"中国,河北省,保定市"
],
"country_province": [
"中国,北京市",
"中国,河北省"
]
}



因为这种es的聚合不支持在嵌套聚合中再筛选 所以 可以通过下面的查询 
GET test/product/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"address": "中国"
}
}
]
}
},
"aggs": {
"province_terms": {
"terms": {
"field": "country_province.keyword",
"size": 34
}
}
},
"size": 0
}


 
 返回的result['hits']['total']就是所有中国的数据
aggs 里面返回的是各个省份的数据 
 

xiaofancn

赞同来自:

存储冗余字段感觉有点怪怪的,有没有Painless Scripting Language 可以提取出来关键字分组呢?

要回复问题请先登录注册