2 Module : Gargantext.Database.TextSearch
3 Description : Postgres text search experimentation
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# LANGUAGE Arrows #-}
12 {-# LANGUAGE NoImplicitPrelude #-}
13 {-# LANGUAGE OverloadedStrings #-}
15 module Gargantext.Database.TextSearch where
18 import Data.Map.Strict hiding (map, drop, take)
20 import Data.List (intersperse, take, drop)
21 import Data.String (IsString(..))
22 import Data.Text (Text, words, unpack, intercalate)
23 import Data.Time (UTCTime)
24 import Database.PostgreSQL.Simple -- (Query, Connection)
25 import Database.PostgreSQL.Simple.ToField
26 import Gargantext.Database.Config (nodeTypeId)
27 import Gargantext.Database.Types.Node (NodeType(..))
28 import Gargantext.Prelude
29 --import Gargantext.Database.Node.Contact
30 import Gargantext.Database.Facet
31 import Gargantext.Database.Schema.Node
32 import Gargantext.Database.Schema.Ngrams
33 import Gargantext.Database.Schema.NodeNode
34 import Gargantext.Database.Schema.NodeNgram
35 import Gargantext.Database.Queries.Join (leftJoin6)
36 import Gargantext.Text.Terms.Mono.Stem.En (stemIt)
37 import Gargantext.Core.Types
38 import Control.Arrow (returnA)
39 import qualified Opaleye as O hiding (Order)
40 import Opaleye hiding (Query, Order)
43 ------------------------------------------------------------------------
44 searchInDatabase :: Connection -> ParentId -> Text -> IO [(NodeId, HyperdataDocument)]
45 searchInDatabase c p t = runQuery c (queryInDatabase p t)
47 -- | Global search query where ParentId is Master Node Corpus Id
48 queryInDatabase :: ParentId -> Text -> O.Query (Column PGInt4, Column PGJsonb)
49 queryInDatabase _ q = proc () -> do
50 row <- queryNodeSearchTable -< ()
51 restrict -< (_ns_search row) @@ (pgTSQuery (unpack q))
52 restrict -< (_ns_typename row) .== (pgInt4 $ nodeTypeId NodeDocument)
53 returnA -< (_ns_id row, _ns_hyperdata row)
55 ------------------------------------------------------------------------
56 -- | todo add limit and offset and order
57 searchInCorpus :: Connection -> CorpusId -> [Text] -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> IO [FacetDoc]
58 searchInCorpus c cId q o l order = runQuery c (filterWith o l order $ queryInCorpus cId q')
60 q' = intercalate " | " $ map stemIt q
62 queryInCorpus :: CorpusId -> Text -> O.Query FacetDocRead
63 queryInCorpus cId q = proc () -> do
64 (n, nn) <- joinInCorpus -< ()
65 restrict -< ( nodeNode_node1_id nn) .== (toNullable $ pgInt4 cId)
66 restrict -< (_ns_search n) @@ (pgTSQuery (unpack q))
67 restrict -< (_ns_typename n) .== (pgInt4 $ nodeTypeId NodeDocument)
68 returnA -< FacetDoc (_ns_id n) (_ns_date n) (_ns_name n) (_ns_hyperdata n) (pgBool True) (pgInt4 1)
70 joinInCorpus :: O.Query (NodeSearchRead, NodeNodeReadNull)
71 joinInCorpus = leftJoin queryNodeSearchTable queryNodeNodeTable cond
73 cond :: (NodeSearchRead, NodeNodeRead) -> Column PGBool
74 cond (n, nn) = nodeNode_node2_id nn .== _ns_id n
76 ------------------------------------------------------------------------
77 type AuthorName = Text
79 -- | TODO Optim: Offset and Limit in the Query
80 searchInCorpusWithContacts :: Connection -> CorpusId -> [Text] -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> IO [FacetPaired Int UTCTime HyperdataDocument Int [Pair Int Text]]
81 searchInCorpusWithContacts c cId q o l order = take (maybe 5 identity l) <$> drop (maybe 0 identity o)
82 <$> map (\((i,u,h,s), ps) -> FacetPaired i u h s (catMaybes ps))
83 <$> toList <$> fromListWith (<>)
84 <$> map (\(FacetPaired i u h s p) -> ((i,u,h,s), [maybePair p]))
85 <$> searchInCorpusWithContacts' c cId q o l order
87 maybePair (Pair Nothing Nothing) = Nothing
88 maybePair (Pair _ Nothing) = Nothing
89 maybePair (Pair Nothing _) = Nothing
90 maybePair (Pair (Just p_id) (Just p_label)) = Just $ Pair p_id p_label
92 searchInCorpusWithContacts' :: Connection -> CorpusId -> [Text] -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> IO [(FacetPaired Int UTCTime HyperdataDocument Int (Pair (Maybe Int) (Maybe Text)))]
93 searchInCorpusWithContacts' c cId q o l order = runQuery c $ queryInCorpusWithContacts cId q' o l order
95 q' = intercalate " | " $ map stemIt q
99 queryInCorpusWithContacts :: CorpusId -> Text -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> O.Query FacetPairedRead
100 queryInCorpusWithContacts cId q _ _ _ = proc () -> do
101 (docs, (corpusDoc, (docNgrams, (ngrams', (_, contacts))))) <- joinInCorpusWithContacts -< ()
102 restrict -< (_ns_search docs) @@ (pgTSQuery $ unpack q )
103 restrict -< (_ns_typename docs) .== (pgInt4 $ nodeTypeId NodeDocument)
104 restrict -< (nodeNode_node1_id corpusDoc) .== (toNullable $ pgInt4 cId)
105 restrict -< (nodeNgram_type docNgrams) .== (toNullable $ pgInt4 $ ngramsTypeId Authors)
106 restrict -< (_node_typename contacts) .== (toNullable $ pgInt4 $ nodeTypeId NodeContact)
107 -- let contact_id = ifThenElse (isNull $ _node_id contacts) (toNullable $ pgInt4 0) (_node_id contacts)
108 returnA -< FacetPaired (_ns_id docs) (_ns_date docs) (_ns_hyperdata docs) (pgInt4 0) (Pair (_node_id contacts) (ngrams_terms ngrams'))
110 joinInCorpusWithContacts :: O.Query (NodeSearchRead, (NodeNodeReadNull, (NodeNgramReadNull, (NgramsReadNull, (NodeNgramReadNull, NodeReadNull)))))
111 joinInCorpusWithContacts = leftJoin6 queryNodeTable queryNodeNgramTable queryNgramsTable queryNodeNgramTable queryNodeNodeTable queryNodeSearchTable cond12 cond23 cond34 cond45 cond56
113 cond12 :: (NodeNgramRead, NodeRead) -> Column PGBool
114 cond12 (ng3, n2) = _node_id n2 .== nodeNgram_node_id ng3
116 cond23 :: (NgramsRead, (NodeNgramRead, NodeReadNull)) -> Column PGBool
117 cond23 (ng2, (nng2, _)) = nodeNgram_ngrams_id nng2 .== ngrams_id ng2
119 cond34 :: (NodeNgramRead, (NgramsRead, (NodeNgramReadNull, NodeReadNull))) -> Column PGBool
120 cond34 (nng, (ng, (_,_))) = ngrams_id ng .== nodeNgram_ngrams_id nng
122 cond45 :: (NodeNodeRead, (NodeNgramRead, (NgramsReadNull, (NodeNgramReadNull, NodeReadNull)))) -> Column PGBool
123 cond45 (nn, (nng, (_,(_,_)))) = nodeNgram_node_id nng .== nodeNode_node2_id nn
125 cond56 :: (NodeSearchRead, (NodeNodeRead, (NodeNgramReadNull, (NgramsReadNull, (NodeNgramReadNull, NodeReadNull))))) -> Column PGBool
126 cond56 (n, (nn, (_,(_,(_,_))))) = _ns_id n .== nodeNode_node2_id nn
130 queryGraphCorpusAuthors' :: O.Query (NodeSearchRead, (NodeNodeReadNull, (NodeNgramReadNull, NgramsReadNull)))
131 queryGraphCorpusAuthors' = leftJoin4 queryNgramsTable queryNodeNgramTable queryNodeNodeTable queryNodeSearchTable cond12 cond23 cond34
133 cond23 :: (NgramsRead, (NodeNgramRead, NodeReadNull)) -> Column PGBool
134 cond23 (ng2, (nng2, _)) = nodeNgram_ngrams_id nng2 .== ngrams_id ng2
136 cond34 :: (NodeNgramRead, (NgramsRead, (NodeNgramReadNull, NodeReadNull))) -> Column PGBool
137 cond34 (nng, (ng, (_,_))) = ngrams_id ng .== nodeNgram_ngrams_id nng
139 cond45 :: (NodeNodeRead, (NodeNgramRead, (NgramsReadNull, (NodeNgramReadNull, NodeReadNull)))) -> Column PGBool
140 cond45 (nn, (nng, (_,(_,_)))) = nodeNgram_node_id nng .== nodeNode_node2_id nn
142 cond56 :: (NodeSearchRead, (NodeNodeRead, (NodeNgramReadNull, (NgramsReadNull, (NodeNgramReadNull, NodeReadNull))))) -> Column PGBool
143 cond56 (n, (nn, (_,(_,(_,_))))) = _ns_id n .== nodeNode_node2_id nn
148 newtype TSQuery = UnsafeTSQuery [Text]
150 -- | TODO [""] -> panic "error"
151 toTSQuery :: [Text] -> TSQuery
152 toTSQuery txt = UnsafeTSQuery $ map stemIt txt
155 instance IsString TSQuery
157 fromString = UnsafeTSQuery . words . cs
160 instance ToField TSQuery
162 toField (UnsafeTSQuery xs)
163 = Many $ intersperse (Plain " && ")
164 $ map (\q -> Many [ Plain "plainto_tsquery("
170 data Order = Asc | Desc
172 instance ToField Order
174 toField Asc = Plain "ASC"
175 toField Desc = Plain "DESC"
181 textSearchQuery :: Query
182 textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
183 \ , n.hyperdata->'title' \
184 \ , n.hyperdata->'source' \
185 \ , n.hyperdata->'authors' \
186 \ , COALESCE(nn.score,null) \
188 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
190 \ n.search @@ (?::tsquery) \
191 \ AND (n.parent_id = ? OR nn.node1_id = ?) \
192 \ AND n.typename = ? \
193 \ ORDER BY n.hyperdata -> 'publication_date' ? \
196 -- | Text Search Function for Master Corpus
197 -- TODO : text search for user corpus
199 -- textSearchTest :: ParentId -> TSQuery -> Cmd [(Int, Value, Value, Value, Value, Maybe Int)]
200 -- textSearchTest pId q = mkCmd $ \c -> textSearch c q pId 5 0 Asc
201 textSearch :: Connection
202 -> TSQuery -> ParentId
203 -> Limit -> Offset -> Order
204 -> IO [(Int,Value,Value,Value, Value, Maybe Int)]
205 textSearch conn q p l o ord = query conn textSearchQuery (q,p,p,typeId,ord,o,l)
207 typeId = nodeTypeId NodeDocument