1 module Gargantext.Database.Action.TSQuery where
4 import Data.List (intersperse)
6 import Data.String (IsString(..))
7 import Data.Text (Text, words)
8 import Database.PostgreSQL.Simple (Query)
9 import Database.PostgreSQL.Simple.ToField
10 import Gargantext.Core
11 import Gargantext.Core.Types
12 import Gargantext.Database.Prelude (Cmd, runPGSQuery)
13 import Gargantext.Prelude
14 import Gargantext.Core.Text.Terms.Mono.Stem.En (stemIt)
17 newtype TSQuery = UnsafeTSQuery [Text]
19 -- | TODO [""] -> panic "error"
20 toTSQuery :: [Text] -> TSQuery
21 toTSQuery txt = UnsafeTSQuery $ map stemIt txt
24 instance IsString TSQuery
26 fromString = UnsafeTSQuery . words . cs
29 instance ToField TSQuery
31 toField (UnsafeTSQuery xs)
32 = Many $ intersperse (Plain " && ")
33 $ map (\q -> Many [ Plain "plainto_tsquery("
39 data Order = Asc | Desc
41 instance ToField Order
43 toField Asc = Plain "ASC"
44 toField Desc = Plain "DESC"
50 textSearchQuery :: Query
51 textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
52 \ , n.hyperdata->'title' \
53 \ , n.hyperdata->'source' \
54 \ , n.hyperdata->'authors' \
55 \ , COALESCE(nn.score,null) \
57 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
59 \ n.search @@ (?::tsquery) \
60 \ AND (n.parent_id = ? OR nn.node1_id = ?) \
61 \ AND n.typename = ? \
62 \ ORDER BY n.hyperdata -> 'publication_date' ? \
65 -- | Text Search Function for Master Corpus
66 -- TODO : text search for user corpus
68 -- textSearchTest :: ParentId -> TSQuery -> Cmd err [(Int, Value, Value, Value, Value, Maybe Int)]
69 -- textSearchTest pId q = textSearch q pId 5 0 Asc
70 textSearch :: HasDBid NodeType
71 => TSQuery -> ParentId
72 -> Limit -> Offset -> Order
73 -> Cmd err [(Int,Value,Value,Value, Value, Maybe Int)]
74 textSearch q p l o ord = runPGSQuery textSearchQuery (q,p,p,typeId,ord,o,l)
76 typeId = toDBid NodeDocument