2 Module : Gargantext.Database.TextSearch
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# LANGUAGE OverloadedStrings #-}
17 module Gargantext.Database.TextSearch where
19 import Prelude (print)
24 import Data.List (intersperse)
25 import Data.String (IsString(..))
26 import Data.Text (Text, words)
28 import Database.PostgreSQL.Simple
29 import Database.PostgreSQL.Simple.ToField
31 import Gargantext (connectGargandb)
32 import Gargantext.Prelude
34 newtype TSQuery = UnsafeTSQuery [Text]
36 toTSQuery :: [Text] -> TSQuery
37 toTSQuery txt = UnsafeTSQuery txt
39 instance IsString TSQuery
41 fromString = UnsafeTSQuery . words . cs
44 instance ToField TSQuery
46 toField (UnsafeTSQuery xs)
47 = Many $ intersperse (Plain " && ")
48 $ map (\q -> Many [ Plain "plainto_tsquery("
57 data Order = Asc | Desc
59 instance ToField Order
61 toField Asc = Plain "ASC"
62 toField Desc = Plain "DESC"
68 textSearchQuery :: Query
69 textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
70 \ , n.hyperdata->'title' \
71 \ , n.hyperdata->'source' \
72 \ , n.hyperdata->'authors' \
73 \ , COALESCE(nn.score,null) \
75 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
77 \ n.title_abstract @@ (?::tsquery) \
78 \ AND n.parent_id = ? AND n.typename = 40 \
79 \ ORDER BY n.hyperdata -> 'publication_date' ? \
83 textSearch :: Connection
84 -> TSQuery -> ParentId
85 -> Limit -> Offset -> Order
86 -> IO [(Int,Value,Value,Value, Value, Maybe Int)]
87 textSearch conn q p l o ord = query conn textSearchQuery (q,p,ord, o,l)
89 textSearchTest :: ParentId -> TSQuery -> IO ()
90 textSearchTest pId q = connectGargandb "gargantext.ini"
91 >>= \conn -> textSearch conn q pId 5 0 Asc