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.Node (Cmd(..), mkCmd, CorpusId, DocId)
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 = 4 \
79 \ ORDER BY n.hyperdata -> 'publication_date' ? \
82 -- | Text Search Function for Master Corpus
83 -- TODO : text search for user corpus
85 -- textSearchTest :: ParentId -> TSQuery -> Cmd [(Int, Value, Value, Value, Value, Maybe Int)]
86 -- textSearchTest pId q = mkCmd $ \c -> textSearch c q pId 5 0 Asc
87 textSearch :: Connection
88 -> TSQuery -> ParentId
89 -> Limit -> Offset -> Order
90 -> IO [(Int,Value,Value,Value, Value, Maybe Int)]
91 textSearch conn q p l o ord = query conn textSearchQuery (q,p,ord, o,l)