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 TemplateHaskell #-}
30 module Gargantext.Database.Schema.NodeNgram where
32 import Data.Text (Text)
33 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
34 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
35 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
36 import Database.PostgreSQL.Simple.SqlQQ (sql)
37 import Gargantext.Core.Types.Main (ListId, ListTypeId)
38 import Gargantext.Database.Utils (mkCmd, Cmd(..))
39 import Gargantext.Prelude
41 import qualified Database.PostgreSQL.Simple as PGS (Connection, query, Only(..))
44 data NodeNgramPoly id node_id ngram_id weight ngrams_type
45 = NodeNgram { nodeNgram_NodeNgramId :: id
46 , nodeNgram_NodeNgramNodeId :: node_id
47 , nodeNgram_NodeNgramNgramId :: ngram_id
48 , nodeNgram_NodeNgramWeight :: weight
49 , nodeNgram_NodeNgramType :: ngrams_type
54 (Maybe (Column PGInt4 ))
68 type NodeNgramReadNull =
70 (Column (Nullable PGInt4 ))
71 (Column (Nullable PGInt4 ))
72 (Column (Nullable PGInt4 ))
73 (Column (Nullable PGFloat8))
74 (Column (Nullable PGInt4 ))
77 NodeNgramPoly (Maybe Int) Int Int Double Int
79 $(makeAdaptorAndInstance "pNodeNgram" ''NodeNgramPoly)
80 $(makeLensesWith abbreviatedFields ''NodeNgramPoly)
83 nodeNgramTable :: Table NodeNgramWrite NodeNgramRead
84 nodeNgramTable = Table "nodes_ngrams"
85 ( pNodeNgram NodeNgram
86 { nodeNgram_NodeNgramId = optional "id"
87 , nodeNgram_NodeNgramNodeId = required "node_id"
88 , nodeNgram_NodeNgramNgramId = required "ngram_id"
89 , nodeNgram_NodeNgramWeight = required "weight"
90 , nodeNgram_NodeNgramType = required "ngrams_type"
94 queryNodeNgramTable :: Query NodeNgramRead
95 queryNodeNgramTable = queryTable nodeNgramTable
97 insertNodeNgrams :: [NodeNgram] -> Cmd Int
98 insertNodeNgrams = insertNodeNgramW
99 . map (\(NodeNgram _ n g w t) ->
100 NodeNgram Nothing (pgInt4 n) (pgInt4 g)
101 (pgDouble w) (pgInt4 t)
104 insertNodeNgramW :: [NodeNgramWrite] -> Cmd Int
105 insertNodeNgramW nns =
106 mkCmd $ \c -> fromIntegral
107 <$> runInsertMany c nodeNgramTable nns
109 type NgramsText = Text
111 updateNodeNgrams :: PGS.Connection -> [(ListId, NgramsText, ListTypeId)] -> IO [PGS.Only Int]
112 updateNodeNgrams c input = PGS.query c updateQuery (PGS.Only $ Values fields $ input)
114 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","text","int4"]
115 updateQuery = [sql| UPDATE nodes_ngrams as old SET
116 ngrams_type = new.typeList
117 from (?) as new(node_id,terms,typeList)
118 JOIN ngrams ON ngrams.terms = new.terms
119 WHERE old.node_id = new.node_id
120 AND old.ngram_id = ngrams.id;
121 -- RETURNING new.ngram_id