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 #-}
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 {-# LANGUAGE DeriveGeneric #-}
20 {-# LANGUAGE DeriveAnyClass #-}
21 {-# LANGUAGE OverloadedStrings #-}
22 {-# LANGUAGE RankNTypes #-}
24 module Gargantext.API.Search
27 import GHC.Generics (Generic)
28 import Data.Time (UTCTime)
29 import Data.Aeson.TH (deriveJSON)
31 import Data.Text (Text)
33 import Test.QuickCheck.Arbitrary
34 import Test.QuickCheck (elements)
35 -- import Control.Applicative ((<*>))
36 import Gargantext.Prelude
37 import Gargantext.Core.Utils.Prefix (unPrefix)
38 import Gargantext.Core.Types.Main (Offset, Limit)
39 import Gargantext.Database.Types.Node
40 import Gargantext.Database.TextSearch
41 import Gargantext.Database.Facet
42 import Gargantext.Database.Utils (Cmd)
44 -----------------------------------------------------------------------
45 -- | SearchIn [NodesId] if empty then global search
47 data SearchQuery = SearchQuery { sq_query :: [Text]
48 , sq_corpus_id :: NodeId
50 $(deriveJSON (unPrefix "sq_") ''SearchQuery)
51 instance ToSchema SearchQuery where
53 genericDeclareNamedSchema
54 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
56 instance Arbitrary SearchQuery where
57 arbitrary = elements [SearchQuery ["electrodes"] 472764]
61 data SearchInQuery = SearchInQuery { siq_query :: [Text]
63 $(deriveJSON (unPrefix "siq_") ''SearchInQuery)
64 instance ToSchema SearchInQuery where
66 genericDeclareNamedSchema
67 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 4 fieldLabel}
69 instance Arbitrary SearchInQuery where
70 arbitrary = SearchInQuery <$> arbitrary
73 -----------------------------------------------------------------------
75 data SearchResults = SearchResults { srs_results :: [FacetPaired Int UTCTime HyperdataDocument Int [Pair Int Text]]}
77 $(deriveJSON (unPrefix "srs_") ''SearchResults)
79 instance Arbitrary SearchResults where
80 arbitrary = SearchResults <$> arbitrary
82 instance ToSchema SearchResults where
84 genericDeclareNamedSchema
85 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 4 fieldLabel}
87 -----------------------------------------------------------------------
88 type SearchAPI = Post '[JSON] SearchResults
89 -----------------------------------------------------------------------
91 search :: SearchQuery -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> Cmd err SearchResults
92 search (SearchQuery q pId) o l order =
93 SearchResults <$> searchInCorpusWithContacts pId q o l order
95 searchIn :: NodeId -> SearchInQuery -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> Cmd err SearchResults
96 searchIn nId (SearchInQuery q ) o l order =
97 SearchResults <$> searchInCorpusWithContacts nId q o l order