es 7.10
注意:使用此方式去重时,不会去除掉不存在去重字段的数据。
去重字段只能是数字类型或keyword。
对text字段进行去重将报错,报错如下。
"unknown type for collapse field `xxx`,only keywords and numbers are accepted"
去重举例如下:
(1)根据approval查询,且根据standardId去重
GET test/_search
{
"query": {
"term": {
"approval":"苏械注准20172091712"
}
},
"collapse": {
"field": "standardId"
}
}
其中standardId的mappings信息如下:
"standardId" : {
"type" : "long"
}
(2)根据approval查询,且根据dosageName去重
GET test/_search
{
"query": {
"term": {
"approval":"苏械注准20172091712"
}
},
"collapse": {
"field": "dosageName.keyword"
}
}
其中dosageName的mappings信息如下:
"dosageName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
注意:使用此方式去重时,会去除掉不存在去重字段的数据。
根据approval查询,且根据standardId去重
GET test/_search
{
"query": {
"term": {
"approval": "苏械注准20172091712"
}
},
"size": 0,
"aggs": {
"age_aggs": {
"terms": {
"field": "standardId",
"size": 10
},
"aggs": {
"age_top": {
"top_hits": {
"sort": [{
"standardId": {
"order": "desc"
}
}],
"size": 1
}
}
}
}
}
}
注意:使用此方式统计去重后的数量时,会去除掉不存在去重字段的数据。
查询根据approval查询,且根据standardId去重后的数量:
# "size": 0 -> 指定es结果中不返回去重前的具体数据,只返回去重后的数量
GET test/_search
{
"query": {
"term": {
"approval": "苏械注准20172091712"
}
},
"size": 0,
"aggs": {
"age_aggs": {
"cardinality": {
"field": "standardId"
}
}
}
}
待完善
(1)https://blog.csdn.net/weixin_44799217/article/details/128509505
(2)https://blog.csdn.net/weixin_43702146/article/details/120533336