java—如何在postgresql表中对与输入值匹配的行或与任何其他匹配行中的值匹配的行进行聚类?

ybzsozfc  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(273)

在我的postgresql数据库中有一个这样的表

如果群集中的每个联系人都与群集中的另一个联系人共享contact\u id\u a或contact\u id\u b值(或两者都共享),如何恢复联系人群?
在上面截图中的示例中,第1-6行将位于同一个集群中,第8行不属于任何集群。
如何使用sql查询或sql查询结合java代码来实现这一点?
对于上下文,此表列出了联系人列表中所有可能的重复联系人。我们希望向列表所有者显示所有可能重复的联系人,以便用户可以手动管理这些重复。
这是我的起始代码:

DuplicateCandidate firstDuplicate = db.sql("select * from duplicates where list_id = "+list_id+ " and ignore_duplicate is not true").first(DuplicateCandidate);
        String sql = "select * from duplicates where list_id = "+list_id+ "and ignore_duplicate is not true "
                + "and (contact_id_a = ? or contact_id_b = ? or contact_id_a = ? or contact_id_b = ?";
        List<DuplicateCandidate> groupOfDuplicates  = db.sql(sql, firstDuplicate.contact_id_a,firstDuplicate.contact_id_a, firstDuplicate.contact_id_b, firstDuplicate.contact_id_b).results(DuplicateCandidate.class);

这将带回第一行和包含16247096或16247097的任何其他行,但不会从第二个查询的结果中带回与联系人标识匹配的其他基本行。
干杯。

t3psigkw

t3psigkw1#

像这样的聚类是一个迭代过程,步骤数未知。我从来没有找到一个可以在递归查询中完成的解决方案。
我已经六年没有从事crm工作了,但是下面的功能与我们以前生成匹配组的方式类似。对于我们的工作负载来说,一行一行地完成这项工作并没有表现得足够好,而使用java等宿主语言来完成这项工作 HashMap() 以及 HashSet() 反向索引会产生非常混乱的代码。
假设此架构:

\d contact_info 
                 Table "public.contact_info"
      Column      |  Type   | Collation | Nullable | Default 
------------------+---------+-----------+----------+---------
 contact_id_a     | bigint  |           |          | 
 contact_id_b     | bigint  |           |          | 
 ignore_duplicate | boolean |           |          | false
 list_id          | integer |           |          | 496

select * from contact_info ;
 contact_id_a | contact_id_b | ignore_duplicate | list_id 
--------------+--------------+------------------+---------
     16247096 |     16247097 | f                |     496
     16247096 |     16247098 | f                |     496
     16247096 |     16247099 | f                |     496
     16247097 |     16247098 | f                |     496
     16247097 |     16247099 | f                |     496
     16247098 |     16247099 | f                |     496
     16247094 |     16247095 | f                |     496
(7 rows)

此函数创建两个临时表来保存中间集群,然后在不再可能进行集群时返回结果。

create or replace function cluster_contact() 
  returns table (clust_id bigint, contact_id bigint) 
  language plpgsql as $$
declare 
  last_count bigint := 1;
  this_count bigint := 0;
begin
  create temp table contact_match (clust_id bigint, contact_id bigint) on commit drop;
  create index cm_1 on contact_match (contact_id, clust_id);
  create index cm_2 on contact_match using hash (clust_id);
  create temp table contact_hold (clust_id bigint, contact_id bigint) on commit drop;

  with dedup as (
    select distinct least(ci.contact_id_a) as clust_id,
           greatest(ci.contact_id_b) as contact_id
      from contact_info ci
     where not ci.ignore_duplicate
  )
  insert into contact_match
    select d.clust_id, d.clust_id from dedup d
    union
    select d.clust_id, d.contact_id from dedup d;

  while last_count > this_count loop

    if this_count = 0 then 
      select count(distinct cm.clust_id) into last_count from contact_match cm;
    else 
      last_count := this_count;
    end if;

    with new_cid as (
      select cm.contact_id as clust_id_old,
             min(cm.clust_id) as clust_id_new
        from contact_match cm
       group by cm.contact_id
    )
    update contact_match
       set clust_id = nc.clust_id_new
      from new_cid nc
     where contact_match.clust_id = nc.clust_id_old;

    truncate table contact_hold;
    insert into contact_hold 
      select distinct * from contact_match;

    truncate table contact_match;
    insert into contact_match
      select * from contact_hold;

    select count(distinct cm.clust_id) into this_count from contact_match cm;

  end loop;

  return query select * from contact_match order by clust_id, contact_id;
end $$;

我见过的开发人员面临的最大的心理障碍之一就是忽略了 contact_id 对自己。这导致了不相交的处理和不必要的复杂的心理模型的左侧和右侧。

select * from cluster_contact();
 clust_id | contact_id 
----------+------------
 16247094 |   16247094
 16247094 |   16247095
 16247096 |   16247096
 16247096 |   16247097
 16247096 |   16247098
 16247096 |   16247099
(6 rows)

如果您需要澄清此解决方案中的任何步骤,或者它不适合您,请发表意见。
另外,要知道levenshtein在 fuzzystrmatch ,效果很好。
如果你愿意 clust_id 开始于 1 ,更改您的 return query 在此函数中:

return query 
    select dense_rank() over (order by cm.clust_id) as clust_id, 
           cm.contact_id 
      from contact_match cm 
     order by clust_id, contact_id;

它将产生:

select * from cluster_contact();
 clust_id | contact_id 
----------+------------
        1 |   16247094
        1 |   16247095
        2 |   16247096
        2 |   16247097
        2 |   16247098
        2 |   16247099
(6 rows)
dy1byipe

dy1byipe2#

可以使用递归cte。这将遍历图形,然后为每一行分配图形中的最小标识符。请注意,您的数据没有每行的唯一标识符,因此首先生成一个:

with recursive d as (
      select row_number() over (order by contact_id_a, contact_id_b) as id, d.*
      from duplicates d
     ),
     cte (id, contact_id_a, contact_id_b, min_id, ids, lev) as (
      select id, contact_id_a, contact_id_b, id as min_id, array[id] as ids, 1 as lev
      from d
      union all
      select d.id, d.contact_id_a, d.contact_id_b, least(d.id, cte.min_id), ids || d.id, lev + 1
      from cte join
           d
           on cte.contact_id_a = d.contact_id_a or cte.contact_id_b = d.contact_id_b
      where d.id <> ALL (cte.ids)
     )
select distinct on (id) cte.*
from cte
order by id, min_id;

min_id 包含所需的分组。
下面是一个db<>小提琴来演示代码。

相关问题