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 {-# LANGUAGE TemplateHaskell #-}
14 {-# LANGUAGE TypeOperators #-}
15 {-# LANGUAGE DeriveAnyClass #-}
17 module Gargantext.API.Search
20 import Data.Aeson hiding (defaultTaggedObject)
21 -- import Data.List (concat)
22 import Data.Swagger hiding (fieldLabelModifier, Contact)
23 import Data.Text (Text)
24 import GHC.Generics (Generic)
25 import Gargantext.API.Prelude (GargServer)
26 import Gargantext.Core.Types.Search
27 import Gargantext.Core.Utils.Prefix (unPrefixSwagger)
28 import Gargantext.Database.Action.Flow.Pairing (isPairedWith)
29 import Gargantext.Database.Action.Search
30 import Gargantext.Database.Admin.Types.Node
31 import Gargantext.Database.Query.Facet
32 import Gargantext.Prelude
33 import Gargantext.Utils.Aeson (defaultTaggedObject)
35 import Test.QuickCheck (elements)
36 import Test.QuickCheck.Arbitrary
38 -----------------------------------------------------------------------
39 -- TODO-ACCESS: CanSearch? or is it part of CanGetNode
40 -- TODO-EVENTS: No event, this is a read-only query.
41 type API results = Summary "Search endpoint"
42 :> ReqBody '[JSON] SearchQuery
43 :> QueryParam "offset" Int
44 :> QueryParam "limit" Int
45 :> QueryParam "order" OrderBy
46 :> Post '[JSON] results
47 -----------------------------------------------------------------------
48 -- | Api search function
49 api :: NodeId -> GargServer (API SearchResult)
50 api nId (SearchQuery q SearchDoc) o l order =
51 SearchResult <$> SearchResultDoc
53 <$> searchInCorpus nId False q o l order
54 -- <$> searchInCorpus nId False (concat q) o l order
55 api nId (SearchQuery q SearchContact) o l order = do
56 -- printDebug "isPairedWith" nId
57 aIds <- isPairedWith nId NodeAnnuaire
58 -- TODO if paired with several corpus
60 Nothing -> pure $ SearchResult
61 $ SearchNoResult "[G.A.Search] pair corpus with an Annuaire"
62 Just aId -> SearchResult
63 <$> SearchResultContact
65 <$> searchInCorpusWithContacts nId aId q o l order
66 api _nId (SearchQuery _q SearchDocWithNgrams) _o _l _order = undefined
68 -----------------------------------------------------------------------
69 -----------------------------------------------------------------------
71 -----------------------------------------------------------------------
72 data SearchType = SearchDoc | SearchContact | SearchDocWithNgrams
74 instance FromJSON SearchType where
75 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
76 instance ToJSON SearchType where
77 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
78 instance ToSchema SearchType
79 instance Arbitrary SearchType where
80 arbitrary = elements [SearchDoc, SearchContact]
82 -----------------------------------------------------------------------
84 SearchQuery { query :: ![Text]
85 , expected :: !SearchType
88 instance FromJSON SearchQuery where
89 parseJSON = genericParseJSON defaultOptions
90 instance ToJSON SearchQuery where
91 toJSON = genericToJSON defaultOptions
92 instance ToSchema SearchQuery
95 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "")
98 instance Arbitrary SearchQuery where
99 arbitrary = elements [SearchQuery ["electrodes"] SearchDoc]
100 -- arbitrary = elements [SearchQuery "electrodes" 1 ] --SearchDoc]
101 -----------------------------------------------------------------------
103 SearchResult { result :: !SearchResultTypes}
106 instance FromJSON SearchResult where
107 parseJSON = genericParseJSON defaultOptions
109 instance ToJSON SearchResult where
110 toJSON = genericToJSON defaultOptions
112 instance ToSchema SearchResult
115 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "")
118 instance Arbitrary SearchResult where
119 arbitrary = SearchResult <$> arbitrary
122 data SearchResultTypes =
123 SearchResultDoc { docs :: ![Row] }
124 | SearchResultContact { contacts :: ![Row] }
125 | SearchNoResult { message :: !Text }
127 instance FromJSON SearchResultTypes where
128 parseJSON = genericParseJSON (defaultOptions { sumEncoding = defaultTaggedObject })
129 instance ToJSON SearchResultTypes where
130 toJSON = genericToJSON (defaultOptions { sumEncoding = defaultTaggedObject })
132 instance Arbitrary SearchResultTypes where
134 srd <- SearchResultDoc <$> arbitrary
135 src <- SearchResultContact <$> arbitrary
136 srn <- pure $ SearchNoResult "No result because.."
137 elements [srd, src, srn]
139 instance ToSchema SearchResultTypes where
140 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "")
143 --------------------------------------------------------------------