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 #-}
13 module Gargantext.Database.Action.Search where
15 import Control.Arrow (returnA)
16 import Control.Lens ((^.))
18 import Data.List (intersperse)
20 import Data.String (IsString(..))
21 import Data.Text (Text, words, unpack, intercalate)
22 import Data.Time (UTCTime)
23 import Database.PostgreSQL.Simple (Query)
24 import Database.PostgreSQL.Simple.ToField
25 import Gargantext.Core.Types
26 import Gargantext.Database.Admin.Config (nodeTypeId)
27 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..), HyperdataContact(..))
28 import Gargantext.Database.Admin.Types.Node (NodeType(..))
29 import Gargantext.Database.Prelude (Cmd, runPGSQuery, runOpaQuery, runCountOpaQuery)
30 import Gargantext.Database.Query.Facet
31 import Gargantext.Database.Query.Filter
32 import Gargantext.Database.Query.Join (leftJoin5)
33 import Gargantext.Database.Query.Table.Node
34 import Gargantext.Database.Query.Table.NodeNode
35 import Gargantext.Database.Schema.Node
36 import Gargantext.Prelude
37 import Gargantext.Core.Text.Terms.Mono.Stem.En (stemIt)
38 import Opaleye hiding (Query, Order)
39 import Data.Profunctor.Product (p4)
40 import qualified Opaleye as O hiding (Order)
42 ------------------------------------------------------------------------
43 searchDocInDatabase :: ParentId
45 -> Cmd err [(NodeId, HyperdataDocument)]
46 searchDocInDatabase p t = runOpaQuery (queryDocInDatabase p t)
48 -- | Global search query where ParentId is Master Node Corpus Id
49 queryDocInDatabase :: ParentId -> Text -> O.Query (Column PGInt4, Column PGJsonb)
50 queryDocInDatabase _ q = proc () -> do
51 row <- queryNodeSearchTable -< ()
52 restrict -< (_ns_search row) @@ (pgTSQuery (unpack q))
53 restrict -< (_ns_typename row) .== (pgInt4 $ nodeTypeId NodeDocument)
54 returnA -< (_ns_id row, _ns_hyperdata row)
56 ------------------------------------------------------------------------
57 -- | todo add limit and offset and order
58 searchInCorpus :: CorpusId
65 searchInCorpus cId t q o l order = runOpaQuery
66 $ filterWith o l order
71 searchCountInCorpus :: CorpusId
75 searchCountInCorpus cId t q = runCountOpaQuery
80 queryInCorpus :: CorpusId
83 -> O.Query FacetDocRead
84 queryInCorpus cId t q = proc () -> do
85 (n, nn) <- joinInCorpus -< ()
86 restrict -< (nn^.nn_node1_id) .== (toNullable $ pgNodeId cId)
88 then (nn^.nn_category) .== (toNullable $ pgInt4 0)
89 else (nn^.nn_category) .>= (toNullable $ pgInt4 1)
90 restrict -< (n ^. ns_search) @@ (pgTSQuery (unpack q))
91 restrict -< (n ^. ns_typename ) .== (pgInt4 $ nodeTypeId NodeDocument)
92 returnA -< FacetDoc (n^.ns_id )
99 joinInCorpus :: O.Query (NodeSearchRead, NodeNodeReadNull)
100 joinInCorpus = leftJoin queryNodeSearchTable queryNodeNodeTable cond
102 cond :: (NodeSearchRead, NodeNodeRead) -> Column PGBool
103 cond (n, nn) = nn^.nn_node2_id .== _ns_id n
105 ------------------------------------------------------------------------
106 searchInCorpusWithContacts
113 -> Cmd err [FacetPaired Int UTCTime HyperdataContact Int]
114 searchInCorpusWithContacts cId aId q o l _order =
115 runOpaQuery $ limit' l
117 $ orderBy ( desc _fp_score)
118 $ selectGroup cId aId
127 ( Column (Nullable PGInt4)
128 , Column (Nullable PGTimestamptz)
129 , Column (Nullable PGJsonb)
130 , Column (Nullable PGInt4)
132 selectContactViaDoc cId aId q = proc () -> do
133 (doc, (corpus_doc, (_contact_doc, (annuaire_contact, contact)))) <- queryContactViaDoc -< ()
134 restrict -< (doc^.ns_search) @@ (pgTSQuery $ unpack q )
135 restrict -< (doc^.ns_typename) .== (pgInt4 $ nodeTypeId NodeDocument)
136 restrict -< (corpus_doc^.nn_node1_id) .== (toNullable $ pgNodeId cId)
137 restrict -< (annuaire_contact^.nn_node1_id) .== (toNullable $ pgNodeId aId)
138 restrict -< (contact^.node_typename) .== (toNullable $ pgInt4 $ nodeTypeId NodeContact)
139 returnA -< ( contact^.node_id
141 , contact^.node_hyperdata
142 , toNullable $ pgInt4 1
145 selectGroup :: NodeId
148 -> Select FacetPairedReadNull
149 selectGroup cId aId q = proc () -> do
150 (a, b, c, d) <- aggregate (p4 (groupBy, groupBy, groupBy, O.sum))
151 (selectContactViaDoc cId aId q) -< ()
152 returnA -< FacetPaired a b c d
161 queryContactViaDoc :: O.Query ( NodeSearchRead
182 cond12 :: (NodeNodeRead, NodeRead) -> Column PGBool
183 cond12 (annuaire_contact, contact) = contact^.node_id .== annuaire_contact^.nn_node2_id
185 cond23 :: ( NodeNodeRead
190 cond23 (contact_doc, (annuaire_contact, _)) = contact_doc^.nn_node1_id .== annuaire_contact^.nn_node2_id
192 cond34 :: ( NodeNodeRead
199 cond34 (corpus_doc, (contact_doc, (_,_))) = corpus_doc^.nn_node2_id .== contact_doc^.nn_node2_id
202 cond45 :: ( NodeSearchRead
211 cond45 (doc, (corpus_doc, (_,(_,_)))) = doc^.ns_id .== corpus_doc^.nn_node2_id
214 ------------------------------------------------------------------------
216 newtype TSQuery = UnsafeTSQuery [Text]
218 -- | TODO [""] -> panic "error"
219 toTSQuery :: [Text] -> TSQuery
220 toTSQuery txt = UnsafeTSQuery $ map stemIt txt
223 instance IsString TSQuery
225 fromString = UnsafeTSQuery . words . cs
228 instance ToField TSQuery
230 toField (UnsafeTSQuery xs)
231 = Many $ intersperse (Plain " && ")
232 $ map (\q -> Many [ Plain "plainto_tsquery("
238 data Order = Asc | Desc
240 instance ToField Order
242 toField Asc = Plain "ASC"
243 toField Desc = Plain "DESC"
249 textSearchQuery :: Query
250 textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
251 \ , n.hyperdata->'title' \
252 \ , n.hyperdata->'source' \
253 \ , n.hyperdata->'authors' \
254 \ , COALESCE(nn.score,null) \
256 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
258 \ n.search @@ (?::tsquery) \
259 \ AND (n.parent_id = ? OR nn.node1_id = ?) \
260 \ AND n.typename = ? \
261 \ ORDER BY n.hyperdata -> 'publication_date' ? \
264 -- | Text Search Function for Master Corpus
265 -- TODO : text search for user corpus
267 -- textSearchTest :: ParentId -> TSQuery -> Cmd err [(Int, Value, Value, Value, Value, Maybe Int)]
268 -- textSearchTest pId q = textSearch q pId 5 0 Asc
269 textSearch :: TSQuery -> ParentId
270 -> Limit -> Offset -> Order
271 -> Cmd err [(Int,Value,Value,Value, Value, Maybe Int)]
272 textSearch q p l o ord = runPGSQuery textSearchQuery (q,p,p,typeId,ord,o,l)
274 typeId = nodeTypeId NodeDocument