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.Core.Types.Query (Limit, Offset)
13 import Gargantext.Database.Prelude (Cmd, runPGSQuery)
14 import Gargantext.Prelude
15 import Gargantext.Core.Text.Terms.Mono.Stem.En (stemIt)
18 newtype TSQuery = UnsafeTSQuery [Text]
20 -- | TODO [""] -> panic "error"
21 toTSQuery :: [Text] -> TSQuery
22 toTSQuery txt = UnsafeTSQuery $ map stemIt txt
25 instance IsString TSQuery
27 fromString = UnsafeTSQuery . words . cs
30 instance ToField TSQuery
32 toField (UnsafeTSQuery xs)
33 = Many $ intersperse (Plain " && ")
34 $ map (\q -> Many [ Plain "plainto_tsquery("
40 data Order = Asc | Desc
42 instance ToField Order
44 toField Asc = Plain "ASC"
45 toField Desc = Plain "DESC"
51 textSearchQuery :: Query
52 textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
53 \ , n.hyperdata->'title' \
54 \ , n.hyperdata->'source' \
55 \ , n.hyperdata->'authors' \
56 \ , COALESCE(nn.score,null) \
58 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
60 \ n.search @@ (?::tsquery) \
61 \ AND (n.parent_id = ? OR nn.node1_id = ?) \
62 \ AND n.typename = ? \
63 \ ORDER BY n.hyperdata -> 'publication_date' ? \
66 -- | Text Search Function for Master Corpus
67 -- TODO : text search for user corpus
69 -- textSearchTest :: ParentId -> TSQuery -> Cmd err [(Int, Value, Value, Value, Value, Maybe Int)]
70 -- textSearchTest pId q = textSearch q pId 5 0 Asc
71 textSearch :: HasDBid NodeType
72 => TSQuery -> ParentId
73 -> Limit -> Offset -> Order
74 -> Cmd err [(Int,Value,Value,Value, Value, Maybe Int)]
75 textSearch q p l o ord = runPGSQuery textSearchQuery (q,p,p,typeId,ord,o,l)
77 typeId = toDBid NodeDocument