2 Module : Gargantext.Database.Schema.NodeNgrams
3 Description : NodeNgram for Ngram indexation or Lists
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 NodeNgram: relation between a Node and a Ngrams
12 if Node is a Document then it is indexing
13 if Node is a List then it is listing (either Stop, Candidate or Map)
17 {-# OPTIONS_GHC -fno-warn-orphans #-}
19 {-# LANGUAGE Arrows #-}
20 {-# LANGUAGE FlexibleInstances #-}
21 {-# LANGUAGE FunctionalDependencies #-}
22 {-# LANGUAGE MultiParamTypeClasses #-}
23 {-# LANGUAGE NoImplicitPrelude #-}
24 {-# LANGUAGE OverloadedStrings #-}
25 {-# LANGUAGE QuasiQuotes #-}
26 {-# LANGUAGE RankNTypes #-}
27 {-# LANGUAGE TemplateHaskell #-}
31 module Gargantext.Database.Schema.NodeNgram where
33 import Data.Text (Text)
34 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
35 import Control.Monad (void)
36 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
37 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
38 import Database.PostgreSQL.Simple.SqlQQ (sql)
39 import Gargantext.Core.Types.Main (ListTypeId)
40 import Gargantext.Database.Utils (mkCmd, Cmd, execPGSQuery)
41 import Gargantext.Database.Types.Node (NodeId, ListId)
42 import Gargantext.Database.Schema.Node (pgNodeId)
43 import Gargantext.Database.Schema.NodeNgramsNgrams (NgramsChild, NgramsParent, ngramsGroup, Action(..))
44 import Gargantext.Prelude
46 import qualified Database.PostgreSQL.Simple as PGS (Only(..))
49 data NodeNgramPoly id node_id ngram_id weight ngrams_type
50 = NodeNgram { nodeNgram_id :: id
51 , nodeNgram_node_id :: node_id
52 , nodeNgram_ngrams_id :: ngram_id
53 , nodeNgram_weight :: weight
54 , nodeNgram_type :: ngrams_type
59 (Maybe (Column PGInt4 ))
73 type NodeNgramReadNull =
75 (Column (Nullable PGInt4 ))
76 (Column (Nullable PGInt4 ))
77 (Column (Nullable PGInt4 ))
78 (Column (Nullable PGFloat8))
79 (Column (Nullable PGInt4 ))
82 NodeNgramPoly (Maybe NodeId) NodeId Int Double Int
84 $(makeAdaptorAndInstance "pNodeNgram" ''NodeNgramPoly)
85 $(makeLensesWith abbreviatedFields ''NodeNgramPoly)
88 nodeNgramTable :: Table NodeNgramWrite NodeNgramRead
89 nodeNgramTable = Table "nodes_ngrams"
90 ( pNodeNgram NodeNgram
91 { nodeNgram_id = optional "id"
92 , nodeNgram_node_id = required "node_id"
93 , nodeNgram_ngrams_id = required "ngram_id"
94 , nodeNgram_weight = required "weight"
95 , nodeNgram_type = required "ngrams_type"
99 queryNodeNgramTable :: Query NodeNgramRead
100 queryNodeNgramTable = queryTable nodeNgramTable
102 insertNodeNgrams :: [NodeNgram] -> Cmd err Int
103 insertNodeNgrams = insertNodeNgramW
104 . map (\(NodeNgram _ n g w t) ->
105 NodeNgram Nothing (pgNodeId n) (pgInt4 g)
106 (pgDouble w) (pgInt4 t)
109 insertNodeNgramW :: [NodeNgramWrite] -> Cmd err Int
110 insertNodeNgramW nns =
111 mkCmd $ \c -> fromIntegral <$> runInsert_ c insertNothing
113 insertNothing = (Insert { iTable = nodeNgramTable
115 , iReturning = rCount
116 , iOnConflict = (Just DoNothing)
119 type NgramsText = Text
121 updateNodeNgrams' :: [(ListId, NgramsText, ListTypeId)] -> Cmd err ()
122 updateNodeNgrams' [] = pure ()
123 updateNodeNgrams' input = void $ execPGSQuery updateQuery (PGS.Only $ Values fields input)
125 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","text","int4"]
126 updateQuery = [sql| UPDATE nodes_ngrams as old SET
127 ngrams_type = new.typeList
128 from (?) as new(node_id,terms,typeList)
129 JOIN ngrams ON ngrams.terms = new.terms
130 WHERE old.node_id = new.node_id
131 AND old.ngram_id = ngrams.id;
134 data NodeNgramsUpdate = NodeNgramsUpdate
135 { _nnu_lists_update :: [(ListId, NgramsText, ListTypeId)]
136 , _nnu_add_children :: [(ListId, NgramsParent, NgramsChild, Maybe Double)]
137 , _nnu_rem_children :: [(ListId, NgramsParent, NgramsChild, Maybe Double)]
140 -- TODO wrap these updates in a transaction.
141 updateNodeNgrams :: NodeNgramsUpdate -> Cmd err ()
142 updateNodeNgrams nnu = do
143 updateNodeNgrams' $ _nnu_lists_update nnu
144 ngramsGroup Del $ _nnu_rem_children nnu
145 ngramsGroup Add $ _nnu_add_children nnu