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 instance IsString TSQuery
38 fromString = UnsafeTSQuery . words . cs
41 instance ToField TSQuery
43 toField (UnsafeTSQuery xs)
44 = Many $ intersperse (Plain " && ")
45 $ map (\q -> Many [ Plain "plainto_tsquery("
54 data Order = Asc | Desc
56 instance ToField Order
58 toField Asc = Plain "ASC"
59 toField Desc = Plain "DESC"
65 textSearchQuery :: Query
66 textSearchQuery = "SELECT n.id, n.hyperdata->'publication_date' \
67 \ , n.hyperdata->'title' \
68 \ , n.hyperdata->'source' \
69 \ , COALESCE(nn.score,null) \
71 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
73 \ n.title_abstract @@ (?::tsquery) \
74 \ AND n.parent_id = ? AND n.typename = 4 \
75 \ ORDER BY n.hyperdata -> 'publication_date' ? \
79 textSearch :: Connection
80 -> TSQuery -> ParentId
81 -> Limit -> Offset -> Order
82 -> IO [(Int,Value,Value,Value, Maybe Int)]
83 textSearch conn q p l o ord = query conn textSearchQuery (q,p,ord, o,l)
85 textSearchTest :: TSQuery -> IO ()
86 textSearchTest q = connectGargandb "gargantext.ini"
87 >>= \conn -> textSearch conn q 421968 10 0 Asc