]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/TextSearch.hs
[FEAT] Full Text Search query Doc facet.
[gargantext.git] / src / Gargantext / Database / TextSearch.hs
1 {-|
2 Module : Gargantext.Database.TextSearch
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
12 -}
13
14 --{-# LANGUAGE NoImplicitPrelude #-}
15 --{-# LANGUAGE OverloadedStrings #-}
16
17 module Gargantext.Database.TextSearch where
18
19 import Prelude (print)
20 import Gargantext (connectGargandb)
21 import Data.Aeson
22 import Database.PostgreSQL.Simple
23 import Control.Applicative
24 import Control.Monad
25 import Data.Text (Text)
26 import Gargantext.Prelude
27
28
29 type TextQuery= Text
30 type ParentId = Int
31 type Limit = Int
32 type Offset = Int
33
34 data Order = Asc | Desc
35
36 toQuery :: Order -> Query
37 toQuery Asc = "ASC"
38 toQuery Desc = "DESC"
39
40 -- TODO
41 -- FIX fav
42 -- ADD ngrams count
43 -- TESTS
44 textSearchQuery :: Order -> Query
45 textSearchQuery o = "SELECT n.id, n.hyperdata -> 'publication_date' \
46 \ , n.hyperdata -> 'title' \
47 \ , n.hyperdata -> 'source' \
48 \ , COALESCE(nn.score,null) \
49 \ FROM nodes n \
50 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
51 \ WHERE \
52 \ n.title_abstract @@ to_tsquery(?) \
53 \ AND n.parent_id = ? AND n.typename = 4 \
54 \ ORDER BY n.hyperdata -> 'publication_date' "
55 <> toQuery o <> " offset ? limit ?;"
56
57
58 textSearch :: Connection
59 -> TextQuery -> ParentId
60 -> Limit -> Offset -> Order
61 -> IO [(Int,Value,Value,Value, Maybe Int)]
62 textSearch conn q p l o ord = query conn (textSearchQuery ord) (q,p,o,l)
63
64 textSearchTest :: TextQuery -> IO ()
65 textSearchTest q = connectGargandb "gargantext.ini"
66 >>= \conn -> textSearch conn q 421968 10 0 Asc
67 >>= mapM_ print