]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Action/TSQuery.hs
[FIX] url in body is the url of the website not the smtp one
[gargantext.git] / src / Gargantext / Database / Action / TSQuery.hs
1 module Gargantext.Database.Action.TSQuery where
2
3 import Data.Aeson
4 import Data.List (intersperse)
5 import Data.Maybe
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)
15
16
17 newtype TSQuery = UnsafeTSQuery [Text]
18
19 -- | TODO [""] -> panic "error"
20 toTSQuery :: [Text] -> TSQuery
21 toTSQuery txt = UnsafeTSQuery $ map stemIt txt
22
23
24 instance IsString TSQuery
25 where
26 fromString = UnsafeTSQuery . words . cs
27
28
29 instance ToField TSQuery
30 where
31 toField (UnsafeTSQuery xs)
32 = Many $ intersperse (Plain " && ")
33 $ map (\q -> Many [ Plain "plainto_tsquery("
34 , Escape (cs q)
35 , Plain ")"
36 ]
37 ) xs
38
39 data Order = Asc | Desc
40
41 instance ToField Order
42 where
43 toField Asc = Plain "ASC"
44 toField Desc = Plain "DESC"
45
46 -- TODO
47 -- FIX fav
48 -- ADD ngrams count
49 -- TESTS
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) \
56 \ FROM nodes n \
57 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
58 \ WHERE \
59 \ n.search @@ (?::tsquery) \
60 \ AND (n.parent_id = ? OR nn.node1_id = ?) \
61 \ AND n.typename = ? \
62 \ ORDER BY n.hyperdata -> 'publication_date' ? \
63 \ offset ? limit ?;"
64
65 -- | Text Search Function for Master Corpus
66 -- TODO : text search for user corpus
67 -- Example:
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)
75 where
76 typeId = toDBid NodeDocument
77
78