2 Module : Gargantext.Database.TextSearch
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE OverloadedStrings #-}
16 module Gargantext.Database.TextSearch where
18 import Prelude (print)
23 import Data.List (intersperse)
24 import Data.String (IsString(..))
25 import Data.Text (Text, words)
27 import Database.PostgreSQL.Simple
28 import Database.PostgreSQL.Simple.ToField
30 import Gargantext.Database.Utils (connectGargandb)
31 import Gargantext.Prelude
33 newtype TSQuery = UnsafeTSQuery [Text]
35 -- | TODO [""] -> panic "error"
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.search @@ (?::tsquery) \
78 \ AND n.parent_id = ? AND n.typename = 40 \
79 \ ORDER BY n.hyperdata -> 'publication_date' ? \
82 textSearch :: Connection
83 -> TSQuery -> ParentId
84 -> Limit -> Offset -> Order
85 -> IO [(Int,Value,Value,Value, Value, Maybe Int)]
86 textSearch conn q p l o ord = query conn textSearchQuery (q,p,ord, o,l)
88 textSearchTest :: ParentId -> TSQuery -> IO ()
89 textSearchTest pId q = connectGargandb "gargantext.ini"
90 >>= \conn -> textSearch conn q pId 5 0 Asc