Elasticsearch去重查询/过滤重复数据(聚合)

2023-05-16

目录

    • 聚合(Aggregations)
    • top_hits指标聚合器
    • 使用script进行聚合
    • Java实现
    • 总结

带家好,我是马儿,这次来讲一下最近遇到的一个问题

我司某个环境的es中被导入了重复数据,导致查询的时候会出现一些重复数据,所以要我们几个开发想一些解决方案,我们聊了聊,出了下面一些方案:
1.从源头解决:导入数据时进行唯一性校验
2.从数据解决:清洗数据,将重复的数据查出后清理,然后入库
3.从查询解决:查询时筛选重复数据

我就从查询着手,找到了聚合查询的方法
在这里插入图片描述

聚合(Aggregations)

聚合功能为ES带来了统计分析的能力,类似于SQL语言中的group by,avg,sum等函数

桶(Buckets):符合条件的文档的集合,相当于SQL中的group by

桶的概念在很多地方有应用,比如桶排序,HashMap的实现中数组也可看作桶,等等等等

示例:
根据city,对twitter索引的文档进行分组
aggs:聚合
my:自定义名称
terms:根据结果分类
field:筛选字段
city:需要分类的字段

GET /twitter/doc/_search
{
	"from": 0,
	"size": 0,
	"aggs": {
	  "my":{
	    "terms":{
	      "field": "city"
	    }
	  }
	}
}

结果中聚合的部分:
计算出了类型和命中的数量

"aggregations": {
    "my": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "北京",
          "doc_count": 105
        },
        {
          "key": "上海",
          "doc_count": 1
        }
      ]
    }
  }

但这不是只有统计结果吗,我要的是筛选后的数据啊
在这里插入图片描述

top_hits指标聚合器

top_hits指标聚合器跟踪要聚合的最相关文档,可以有效地用于通过存储桶聚合器按某些字段对结果集进行分组。
选项:
from-要获取的第一个结果的偏移量。
size-每个存储桶要返回的最匹配匹配项的最大数目。 默认情况下,返回前三个匹配项。
排序-匹配的热门匹配的排序方式。 默认情况下,命中按主要查询的分数排序。

示例:
根据city,对twitter索引的文档进行分组、根据age进行排序、结果只包含user+age+city,然后显示每组的一条数据
aggs:聚合
my:自定义名称
terms:根据结果分类
field:筛选字段
city:需要分类的字段
sort:排序
age:排序依据字段
order:排序方式
desc:降序
_source includes:结果包含的字段
size:每组显示的数量

{
	"from": 0,
	"size": 0,
	"aggs": {
	  "my":{
	    "terms":{
	      "field": "city"
	    },
	    "aggs":{
	      "my_top_hits":{
	        "top_hits":{
	          "sort": [
              {
                "age": {
                  "order": "desc"
                }
              }
            ],
            "_source": {
              "includes": [
                "user",
                "age",
                "city"
              ]
            },
	          "size":1
	        }
	      }
	    }
	  }
	}
}

结果中聚合的部分:

"aggregations": {
    "my": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "北京",
          "doc_count": 105,
          "my_top_hits": {
            "hits": {
              "total": 105,
              "max_score": null,
              "hits": [
                {
                  "_index": "twitter",
                  "_type": "doc",
                  "_id": "AW5jwgirrweXGTc7-cPA",
                  "_score": null,
                  "_source": {
                    "city": "北京",
                    "user": "朝阳区-老王",
                    "age": 50
                  },
                  "sort": [
                    50
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "上海",
          "doc_count": 1,
          "my_top_hits": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "twitter",
                  "_type": "doc",
                  "_id": "AW5jwiM1rweXGTc7-cPB",
                  "_score": null,
                  "_source": {
                    "city": "上海",
                    "user": "虹桥-老吴",
                    "age": 90
                  },
                  "sort": [
                    90
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }

但是光使用terms,我添加了多个字段后查不出来东西了都,难道这样还不行吗
在这里插入图片描述

使用script进行聚合

常规的聚合无法在聚合中进行复杂操作,所以要加入脚本
示例:
修改terms中内容为下,将三个条件拼接起来

"terms":{
	      "script": "doc['user.keyword'].value + '#' + doc['age'].value + '#' +doc['city'].value"
	    },

查询结果:
key:拼接的条件
doc_count:每组重复的数目

"aggregations": {
    "my": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "双榆树-张三#20#北京",
          "doc_count": 101,
          "my_top_hits": {
            "hits": {
              "total": 101,
              "max_score": null,
              "hits": [
                {
                  "_index": "twitter",
                  "_type": "doc",
                  "_id": "AW9lr8sBP5iHlpen8GYt",
                  "_score": null,
                  "_source": {
                    "city": "北京",
                    "user": "双榆树-张三",
                    "age": 20
                  },
                  "sort": [
                    20
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "东城区-李四#30#北京",
          "doc_count": 1,
          "my_top_hits": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "twitter",
                  "_type": "doc",
                  "_id": "AW5jwaOIrweXGTc7-cO-",
                  "_score": null,
                  "_source": {
                    "city": "北京",
                    "user": "东城区-李四",
                    "age": 30
                  },
                  "sort": [
                    30
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "东城区-老刘#30#北京",
          "doc_count": 1,
          "my_top_hits": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "twitter",
                  "_type": "doc",
                  "_id": "AW5jwXhcrweXGTc7-cO9",
                  "_score": null,
                  "_source": {
                    "city": "北京",
                    "user": "东城区-老刘",
                    "age": 30
                  },
                  "sort": [
                    30
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "朝阳区-老王#50#北京",
          "doc_count": 1,
          "my_top_hits": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "twitter",
                  "_type": "doc",
                  "_id": "AW5jwgirrweXGTc7-cPA",
                  "_score": null,
                  "_source": {
                    "city": "北京",
                    "user": "朝阳区-老王",
                    "age": 50
                  },
                  "sort": [
                    50
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "朝阳区-老贾#35#北京",
          "doc_count": 1,
          "my_top_hits": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "twitter",
                  "_type": "doc",
                  "_id": "AW5jwcvBrweXGTc7-cO_",
                  "_score": null,
                  "_source": {
                    "city": "北京",
                    "user": "朝阳区-老贾",
                    "age": 35
                  },
                  "sort": [
                    35
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "虹桥-老吴#90#上海",
          "doc_count": 1,
          "my_top_hits": {
            "hits": {
              "total": 1,
              "max_score": null,
              "hits": [
                {
                  "_index": "twitter",
                  "_type": "doc",
                  "_id": "AW5jwiM1rweXGTc7-cPB",
                  "_score": null,
                  "_source": {
                    "city": "上海",
                    "user": "虹桥-老吴",
                    "age": 90
                  },
                  "sort": [
                    90
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }

可以看到,每组都不一样,我们script真是太强大了
在这里插入图片描述

Java实现

elasticsearch包中的工具类
使用elasticsearch包中的工具类,将索引中所有字段进行拼接,作为aggregation参数传入查询即可

总结

本文介绍了es的聚合功能,aggs+top_hits+script就能过滤重复数据,得到唯一结果。

–02020728
补充分页
Elasticsearch聚合后分页

这个分页以后有机会再说
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Elasticsearch去重查询/过滤重复数据(聚合) 的相关文章

  • ubuntu安装ApiPost以及生成快捷桌面启动图标

    ubuntu安装ApiPost以及生成快捷桌面启动图标 接口测试工具 xff0c 生成接口文档 ApiPost下载地址 xff1a https www apipost cn download html 选择自己对应的版本 存放位置自己选择设
  • 脱敏工具类。手机号、银行卡号、身份证号、关键信息脱敏

    手机号 银行卡号 身份证号 关键信息脱敏 老规矩 xff0c 话不多说直接上代码 span class token keyword package span span class token namespace com span class
  • vue3.x+ts项目创建,配置流程

    一 创建项目 安装组件库 1 创建vue3 x项目 npm init vue 64 latest 2 项目中安装 element plus NPM npm install element plus save Yarn yarn add el
  • mybatis-plus条件构造器以及QueryWrapper用法学习

    QueryWrapper的基本用法 话不多说 xff0c 直接上代码 更多使用方法传送门 span class token keyword public span span class token keyword void span spa
  • linux下安装aMule

    linux下安装aMule 在Linux下安装aMule xff0c 亲测有效 xff0c 话不多说 xff0c 直接上命令 cd span class token operator span opt span class token op
  • idea中debug启动项目特别慢的解决方法

    问题描述 问题 xff1a idea中使用debug方式启动项目 xff0c 原本30s不到的启动时间 xff0c 现在启动的时间超过了3分钟甚至更久 我自己的问题以及解决方案 xff0c 你们也有可能是其他原因 我本人的问题是因为打了太多
  • MacBook安装jdk1.8方便快捷稳定的方法

    MacBook安装jdk方便快捷稳定的方法 最简单快捷的jdk1 8安装 先查看有没有安装jdk xff1a java version 这样就是没有安装 然后去官网下载 下载地址 xff1a https www java com zh CN
  • RubyMine创建第一个ruby项目并运行

    第一步 xff1a 创建一个项目 RubyMine已分享网盘 xff1a 链接 https pan baidu com s 1MTbjYmuS1SxrgR1wBAmsoA pwd 61 5dh3 提取码 5dh3 Location xff1
  • 创建一个rails入门项目并运行

    创建第一个rails项目 从java转到ruby xff0c 正在学习中 xff0c 创建第一个rails项目 xff0c 做一些记录 首先要检查一下前置条件 检查是否安装ruby xff1a ruby version 我这里是已经安装了
  • rails/ruby字符串与数组之间的转换

    rails ruby字符串与数组之间的转换 初步接触ruby xff0c 点点滴滴记录下来 xff0c 希望能帮助同样是新加入ruby的同学 字符串与数组之间的转换 span class token comment ruby字符串转数组 s
  • this is incompatible with sql_mode=only_full_group_by解决方案

    MySQL查询时报错 xff1a this is incompatible with sql mode 61 only full group by 报错原因分析 xff1a 一 原理层面 这个错误发生在mysql 5 7 5 版本及以上版本
  • macbook安装Redis客户端another-redis-desktop-manager

    macbook安装Redis客户端another redis desktop manager 不讲废话 xff0c 直接上命令 macbook下使用brew安装another redis desktop manager brew insta
  • ruby on rails读取excel文件完整流程

    前端上传excel文件 xff0c 后端读取文件并写入数据库 话不多说 xff0c 直接上代码 代码复制可直接使用 xff0c 经过多次测试验证 Gemfile文件中引入 xff1a gem span class token string
  • vue3 + vite + ts + setup , 第二十一练 vue3 中使用函数式编程,h函数(一)

    之前跟大家介绍了两种vue编写风格分别是template模板方式 xff0c 和JSX方式感觉JSX被大家吐槽的很厉害 xff0c 其实用习惯还挺好用的今天介绍第三种函数式编程 主要会用到h函数 h 函数是一个用于创建 VNode 的实用程
  • 线程安全与实现方法

    线程安全 文章目录 线程安全线程安全的定义线程安全的分类补充 xff1a this引用逃逸什么是this引用逃逸逃逸场景场景一场景二 解决方案 线程安全的实现互斥同步 xff08 阻塞同步 xff09 实现举例 非阻塞同步实现举例 无同步方
  • github api 连接出错的几种情况和解决办法

    网络连接正常 xff0c github也能连接 xff0c 但是idea terminal 将代码push到远端时无法与github获取连接 情况一 使用git pus命令报错 fatal HttpRequestException enco
  • 【Proteus仿真】| 05——问题记录

    系列文章目录 Proteus仿真 01 软件安装 Proteus仿真 02 基础使用 Proteus仿真 03 超详细使用教程 Proteus仿真 04 绘制原理图模板 Proteus仿真 05 问题记录 文章目录 前言1 51单片机仿真2
  • 在Winxp虚拟机上安装Vxworks虚拟机,完成VxWorks_Tornado开发环境搭建

    前言 关于在虚拟机上安装虚拟机的做法并不普遍 xff0c 因为其实用性并不大 xff0c 对于很多人来说 xff0c 并没有必要再虚拟机上安装虚拟机 xff0c 以来这种做法确实很浪费内存资源 xff0c 二来没有具体的实用价值 因为主机装
  • centos7 搭建httpd服务

    查看是否有httpd root 64 Cheney yum list httpd 已加载插件 xff1a fastestmirror langpacks Loading mirror speeds from cached hostfile
  • 邮件系统被退回的原因及解决办法

    邮件为什么会被退回经常上网发送邮件的人可能会有邮件被退回的经历 xff0c 收到被退回的邮件要具体分析 xff0c 退回的信件一般都会有简短的说明 xff0c 结合这些说明你可以进一步了解具体的退信原因并作出相应处理 一 退信由哪些内容组成

随机推荐