2 Module : Gargantext.Database.Triggers.NodeNodeNgrams
3 Description : Triggers configuration
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Triggers on NodeNodeNgrams table.
14 {-# LANGUAGE QuasiQuotes #-}
16 module Gargantext.Database.Admin.Trigger.ContextNodeNgrams
19 import Database.PostgreSQL.Simple.SqlQQ (sql)
20 import Gargantext.Core
21 -- import Gargantext.Core.Types.Main (ListType(CandidateTerm))
22 import Gargantext.Database.Admin.Types.Node -- (ListId, CorpusId, NodeId)
23 import Gargantext.Database.Prelude (Cmd, execPGSQuery)
24 import Gargantext.Prelude
25 import qualified Database.PostgreSQL.Simple as DPS
27 triggerCountInsert :: HasDBid NodeType => Cmd err Int64
28 triggerCountInsert = execPGSQuery query (toDBid NodeDocument, toDBid NodeList)
32 CREATE OR REPLACE FUNCTION set_ngrams_global_count() RETURNS trigger AS $$
34 IF pg_trigger_depth() <> 1 THEN
37 IF TG_OP = 'INSERT' THEN
38 INSERT INTO context_node_ngrams (context_id, node_id, ngrams_id, ngrams_type, weight)
39 select n.parent_id, n.id, new0.ngrams_id, new0.ngrams_type, count(*) from NEW as new0
40 INNER JOIN contexts n ON n.id = new0.context_id
41 INNER JOIN nodes n2 ON n2.id = new0.node_id
42 WHERE n2.typename = ? -- not mandatory
43 AND n.typename = ? -- not mandatory
44 AND n.parent_id <> n2.id -- not mandatory
45 GROUP BY n.parent_id, n.id, new0.ngrams_id, new0.ngrams_type
46 ON CONFLICT (context_id, node_id, ngrams_id, ngrams_type)
47 DO UPDATE set weight = context_node_ngrams.weight + excluded.weight
55 -- DROP trigger trigger_count_insert on context_node_ngrams;
57 CREATE TRIGGER trigger_count_insert AFTER INSERT on context_node_ngrams
58 REFERENCING NEW TABLE AS NEW
60 EXECUTE PROCEDURE set_ngrams_global_count();
63 triggerCountInsert2 :: HasDBid NodeType => Cmd err Int64
64 triggerCountInsert2 = execPGSQuery query ( toDBid NodeCorpus
71 CREATE OR REPLACE FUNCTION set_ngrams_global_count2() RETURNS trigger AS $$
73 IF pg_trigger_depth() <> 1 THEN
76 IF TG_OP = 'INSERT' THEN
77 INSERT INTO context_node_ngrams2 (context_id, nodengrams_id, weight)
78 SELECT corpus.id, nng.id, count(*) from NEW as new3
79 INNER JOIN node_ngrams nng ON nng.id = new3.nodengrams_id
80 INNER JOIN nodes list ON list.id = nng.node_id
81 INNER JOIN nodes_nodes nn ON nn.node2_id = new3.context_id
82 INNER JOIN nodes corpus ON corpus.id = nn.node1_id
83 INNER JOIN nodes doc ON doc.id = nn.node2_id
84 WHERE corpus.typename = ? -- 30 -- corpus
85 AND doc.typename = ? -- 4 -- maybe not mandatory
86 AND list.typename = ? -- 5 -- list
87 GROUP BY corpus.id, nng.id
89 ON CONFLICT (context_id, nodengrams_id)
90 DO UPDATE set weight = context_node_ngrams2.weight + excluded.weight
98 -- DROP trigger trigger_count_insert2 on context_node_ngrams2;
100 CREATE TRIGGER trigger_count_insert2 AFTER INSERT on context_node_ngrams2
101 REFERENCING NEW TABLE AS NEW
103 EXECUTE PROCEDURE set_ngrams_global_count2();
106 -- TODO add the groups
107 -- TODO use context instead of nodes of type doc
109 triggerCoocInsert :: HasDBid NodeType => Cmd err Int64
110 triggerCoocInsert = execPGSQuery query ( toDBid NodeCorpus
111 , toDBid NodeDocument
113 , toDBid CandidateTerm
114 , toDBid CandidateTerm
119 CREATE OR REPLACE FUNCTION set_cooc() RETURNS trigger AS $$
121 IF pg_trigger_depth() <> 1 THEN
124 IF TG_OP = 'INSERT' THEN
125 INSERT INTO node_nodengrams_nodengrams (node_id, node_ngrams1_id, node_ngrams2_id, weight)
126 WITH input(corpus_id, nn1, nn2, weight) AS (
127 SELECT corpus.id, nng1.id, nng2.id, count(*) from NEW as new2
128 INNER JOIN node_ngrams nng1 ON nng1.id = new2.nodengrams_id
129 INNER JOIN nodes list ON list.id = nng1.node_id
130 INNER JOIN nodes_nodes nn ON nn.node2_id = new2.node_id
131 INNER JOIN nodes corpus ON corpus.id = nn.node1_id
132 INNER JOIN nodes doc ON doc.id = nn.node2_id
134 INNER JOIN node_node_ngrams2 nnng2 ON nnng2.node_id = doc.id
135 INNER JOIN node_ngrams nng2 ON nng2.id = nnng2.nodengrams_id
137 WHERE corpus.typename = ? -- 30 -- corpus
138 AND doc.typename = ? -- 4 -- maybe not mandatory
139 AND list.typename = ? -- 5 -- list
140 AND nng2.node_id = list.id
141 AND nng1.id < nng2.id
142 AND nng1.node_subtype >= ?
143 AND nng2.node_subtype >= ?
144 GROUP BY corpus.id, nng1.id, nng2.id
146 SELECT * from input where weight > 1
148 ON CONFLICT (node_id, node_ngrams1_id, node_ngrams2_id)
149 DO UPDATE set weight = node_nodengrams_nodengrams.weight + excluded.weight
157 -- DROP trigger trigger_cooc on node_node_ngrams2;
159 CREATE TRIGGER trigger_cooc_insert AFTER INSERT on node_node_ngrams2
160 REFERENCING NEW TABLE AS NEW
162 EXECUTE PROCEDURE set_cooc();