2 Module : Gargantext.Database.TextSearch
3 Description : Postgres text search experimentation
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
20 import Data.List (intersperse)
21 import Data.String (IsString(..))
22 import Data.Text (Text, words)
24 import Database.PostgreSQL.Simple
25 import Database.PostgreSQL.Simple.ToField
27 import Gargantext.Prelude
29 newtype TSQuery = UnsafeTSQuery [Text]
31 -- | TODO [""] -> panic "error"
32 toTSQuery :: [Text] -> TSQuery
33 toTSQuery txt = UnsafeTSQuery txt
35 instance IsString TSQuery
37 fromString = UnsafeTSQuery . words . cs
40 instance ToField TSQuery
42 toField (UnsafeTSQuery xs)
43 = Many $ intersperse (Plain " && ")
44 $ map (\q -> Many [ Plain "plainto_tsquery("
53 data Order = Asc | Desc
55 instance ToField Order
57 toField Asc = Plain "ASC"
58 toField Desc = Plain "DESC"
64 textSearchQuery :: Query
65 textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
66 \ , n.hyperdata->'title' \
67 \ , n.hyperdata->'source' \
68 \ , n.hyperdata->'authors' \
69 \ , COALESCE(nn.score,null) \
71 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
73 \ n.search @@ (?::tsquery) \
74 \ AND n.parent_id = ? AND n.typename = 4 \
75 \ ORDER BY n.hyperdata -> 'publication_date' ? \
78 -- | Text Search Function for Master Corpus
79 -- TODO : text search for user corpus
81 -- textSearchTest :: ParentId -> TSQuery -> Cmd [(Int, Value, Value, Value, Value, Maybe Int)]
82 -- textSearchTest pId q = mkCmd $ \c -> textSearch c q pId 5 0 Asc
83 textSearch :: Connection
84 -> TSQuery -> ParentId
85 -> Limit -> Offset -> Order
86 -> IO [(Int,Value,Value,Value, Value, Maybe Int)]
87 textSearch conn q p l o ord = query conn textSearchQuery (q,p,ord, o,l)