]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Search.hs
[Community] specs + database schema adapted to link doc/contact
[gargantext.git] / src / Gargantext / API / Search.hs
1 {-|
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
8 Portability : POSIX
9
10 Count API part of Gargantext.
11 -}
12
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE DeriveAnyClass #-}
18
19 module Gargantext.API.Search
20 where
21
22 import Data.Aeson.TH (deriveJSON)
23 import Data.Swagger
24 import Data.Text (Text)
25 import Data.Time (UTCTime)
26 import GHC.Generics (Generic)
27 import Gargantext.API.Prelude (GargServer)
28 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
29 import Gargantext.Database.Query.Facet
30 import Gargantext.Database.Action.Search
31 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument)
32 import Gargantext.Database.Admin.Types.Node
33 import Gargantext.Prelude
34 import Servant
35 import Test.QuickCheck (elements)
36 import Test.QuickCheck.Arbitrary
37
38 -----------------------------------------------------------------------
39 data SearchQuery = SearchQuery
40 { sq_query :: [Text]
41 } deriving (Generic)
42
43 $(deriveJSON (unPrefix "sq_") ''SearchQuery)
44
45 instance ToSchema SearchQuery where
46 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "sq_")
47
48 instance Arbitrary SearchQuery where
49 arbitrary = elements [SearchQuery ["electrodes"]]
50
51 -----------------------------------------------------------------------
52 data SearchDocResults = SearchDocResults { sdr_results :: [FacetDoc]}
53 deriving (Generic)
54 $(deriveJSON (unPrefix "sdr_") ''SearchDocResults)
55
56 instance Arbitrary SearchDocResults where
57 arbitrary = SearchDocResults <$> arbitrary
58
59 instance ToSchema SearchDocResults where
60 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "sdr_")
61
62 data SearchPairedResults =
63 SearchPairedResults { spr_results :: [FacetPaired Int UTCTime HyperdataDocument Int [Pair Int Text]] }
64 deriving (Generic)
65 $(deriveJSON (unPrefix "spr_") ''SearchPairedResults)
66
67 instance Arbitrary SearchPairedResults where
68 arbitrary = SearchPairedResults <$> arbitrary
69
70 instance ToSchema SearchPairedResults where
71 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "spr_")
72
73 -----------------------------------------------------------------------
74 -- TODO-ACCESS: CanSearch? or is it part of CanGetNode
75 -- TODO-EVENTS: No event, this is a read-only query.
76 type SearchAPI results = Summary "Search endpoint"
77 :> ReqBody '[JSON] SearchQuery
78 :> QueryParam "offset" Int
79 :> QueryParam "limit" Int
80 :> QueryParam "order" OrderBy
81 :> Post '[JSON] results
82
83 type SearchDocsAPI = SearchAPI SearchDocResults
84 searchDocs :: NodeId -> GargServer SearchDocsAPI
85 searchDocs nId (SearchQuery q) o l order =
86 SearchDocResults <$> searchInCorpus nId False q o l order
87 --SearchResults <$> searchInCorpusWithContacts nId q o l order
88
89 -----------------------------------------------------------------------
90 type SearchPairsAPI = Summary ""
91 :> "list"
92 :> Capture "list" ListId
93 :> SearchAPI SearchPairedResults
94 searchPairs :: NodeId -> GargServer SearchPairsAPI
95
96 searchPairs pId lId (SearchQuery q) o l order =
97 SearchPairedResults <$> searchInCorpusWithContacts pId lId q o l order
98
99 -----------------------------------------------------------------------
100