2 Module : Gargantext.API.Count
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Count API part of Gargantext.
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
16 {-# LANGUAGE NoImplicitPrelude #-}
17 {-# LANGUAGE DataKinds #-}
18 {-# LANGUAGE TemplateHaskell #-}
19 {-# LANGUAGE TypeOperators #-}
20 {-# LANGUAGE DeriveGeneric #-}
21 {-# LANGUAGE DeriveAnyClass #-}
22 {-# LANGUAGE OverloadedStrings #-}
24 module Gargantext.API.Search
28 import GHC.Generics (Generic)
29 import Control.Monad.IO.Class (liftIO)
32 import Data.Aeson hiding (Error, fieldLabelModifier)
33 import Data.Aeson.TH (deriveJSON)
35 import Data.Text (Text)
36 import Database.PostgreSQL.Simple (Connection)
39 import Test.QuickCheck.Arbitrary
40 import Test.QuickCheck (elements)
41 -- import Control.Applicative ((<*>))
43 import Gargantext.Prelude
44 import Gargantext.Core.Utils.Prefix (unPrefix)
45 import Gargantext.Database.TextSearch
49 -----------------------------------------------------------------------
50 data SearchQuery = SearchQuery { sq_query :: [Text]
53 $(deriveJSON (unPrefix "sq_") ''SearchQuery)
54 instance ToSchema SearchQuery where
56 genericDeclareNamedSchema
57 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
60 instance Arbitrary SearchQuery where
61 arbitrary = elements [SearchQuery ["electrodes"] 472764]
63 -----------------------------------------------------------------------
65 data SearchResult = SearchResult { sr_id :: Int
69 $(deriveJSON (unPrefix "sr_") ''SearchResult)
70 instance Arbitrary SearchResult where
71 arbitrary = elements [SearchResult 1 "Title" "Authors"]
73 instance ToSchema SearchResult where
75 genericDeclareNamedSchema
76 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
78 -----------------------------------------------------------------------
80 data SearchResults = SearchResults { srs_results :: [SearchResult]}
82 $(deriveJSON (unPrefix "srs_") ''SearchResults)
84 instance Arbitrary SearchResults where
85 arbitrary = SearchResults <$> arbitrary
87 instance ToSchema SearchResults where
89 genericDeclareNamedSchema
90 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 4 fieldLabel}
94 -----------------------------------------------------------------------
95 type SearchAPI = Post '[JSON] SearchResults
96 -----------------------------------------------------------------------
98 search :: Connection -> SearchQuery -> Handler SearchResults
99 search c (SearchQuery q pId) =
100 liftIO $ SearchResults <$> map (\(i, _, t, _, a, _) -> SearchResult i (cs $ encode t) (cs $ encode a))
101 <$> textSearch c (toTSQuery q) pId 5 0 Desc