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
26 import Gargantext.Database.Config (nodeTypeId)
27 import Gargantext.Database.Types.Node (NodeType(..))
28 import Gargantext.Prelude
30 newtype TSQuery = UnsafeTSQuery [Text]
32 -- | TODO [""] -> panic "error"
33 toTSQuery :: [Text] -> TSQuery
34 toTSQuery txt = UnsafeTSQuery txt
36 instance IsString TSQuery
38 fromString = UnsafeTSQuery . words . cs
41 instance ToField TSQuery
43 toField (UnsafeTSQuery xs)
44 = Many $ intersperse (Plain " && ")
45 $ map (\q -> Many [ Plain "plainto_tsquery("
54 data Order = Asc | Desc
56 instance ToField Order
58 toField Asc = Plain "ASC"
59 toField Desc = Plain "DESC"
65 textSearchQuery :: Query
66 textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
67 \ , n.hyperdata->'title' \
68 \ , n.hyperdata->'source' \
69 \ , n.hyperdata->'authors' \
70 \ , COALESCE(nn.score,null) \
72 \ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
74 \ n.search @@ (?::tsquery) \
75 \ AND (n.parent_id = ? OR nn.node1_id = ?) \
76 \ AND n.typename = ? \
77 \ ORDER BY n.hyperdata -> 'publication_date' ? \
80 -- | Text Search Function for Master Corpus
81 -- TODO : text search for user corpus
83 -- textSearchTest :: ParentId -> TSQuery -> Cmd [(Int, Value, Value, Value, Value, Maybe Int)]
84 -- textSearchTest pId q = mkCmd $ \c -> textSearch c q pId 5 0 Asc
85 textSearch :: Connection
86 -> TSQuery -> ParentId
87 -> Limit -> Offset -> Order
88 -> IO [(Int,Value,Value,Value, Value, Maybe Int)]
89 textSearch conn q p l o ord = query conn textSearchQuery (q,p,p,typeId,ord,o,l)
91 typeId = nodeTypeId NodeDocument