]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Search.hs
[FIX] Score by Doc or Corpus.
[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 NoImplicitPrelude #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 {-# LANGUAGE DeriveGeneric #-}
20 {-# LANGUAGE DeriveAnyClass #-}
21 {-# LANGUAGE OverloadedStrings #-}
22 {-# LANGUAGE RankNTypes #-}
23
24 module Gargantext.API.Search
25 where
26
27 import GHC.Generics (Generic)
28 import Data.Time (UTCTime)
29 import Data.Aeson.TH (deriveJSON)
30 import Data.Swagger
31 import Data.Text (Text)
32 import Servant
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)
43
44 -----------------------------------------------------------------------
45 -- | SearchIn [NodesId] if empty then global search
46 -- TODO [Int]
47 data SearchQuery = SearchQuery { sq_query :: [Text]
48 , sq_corpus_id :: NodeId
49 } deriving (Generic)
50 $(deriveJSON (unPrefix "sq_") ''SearchQuery)
51 instance ToSchema SearchQuery where
52 declareNamedSchema =
53 genericDeclareNamedSchema
54 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
55
56 instance Arbitrary SearchQuery where
57 arbitrary = elements [SearchQuery ["electrodes"] 472764]
58
59 --
60
61 data SearchInQuery = SearchInQuery { siq_query :: [Text]
62 } deriving (Generic)
63 $(deriveJSON (unPrefix "siq_") ''SearchInQuery)
64 instance ToSchema SearchInQuery where
65 declareNamedSchema =
66 genericDeclareNamedSchema
67 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 4 fieldLabel}
68
69 instance Arbitrary SearchInQuery where
70 arbitrary = SearchInQuery <$> arbitrary
71
72
73 -----------------------------------------------------------------------
74
75 data SearchResults = SearchResults { srs_results :: [FacetPaired Int UTCTime HyperdataDocument Int [Pair Int Text]]}
76 deriving (Generic)
77 $(deriveJSON (unPrefix "srs_") ''SearchResults)
78
79 instance Arbitrary SearchResults where
80 arbitrary = SearchResults <$> arbitrary
81
82 instance ToSchema SearchResults where
83 declareNamedSchema =
84 genericDeclareNamedSchema
85 defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 4 fieldLabel}
86
87 -----------------------------------------------------------------------
88 -- TODO-ACCESS: CanSearch? or is it part of CanGetNode
89 -- TODO-EVENTS: No event, this is a read-only query.
90 type SearchAPI = Post '[JSON] SearchResults
91 -----------------------------------------------------------------------
92
93 search :: SearchQuery -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> Cmd err SearchResults
94 search (SearchQuery q pId) o l order =
95 SearchResults <$> searchInCorpusWithContacts pId q o l order
96
97 searchIn :: NodeId -> SearchInQuery -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> Cmd err SearchResults
98 searchIn nId (SearchInQuery q ) o l order =
99 SearchResults <$> searchInCorpusWithContacts nId q o l order
100
101