{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
-
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RankNTypes #-}
module Gargantext.API.Search
where
-
import GHC.Generics (Generic)
-import Control.Monad.IO.Class (liftIO)
-import Prelude (Bounded, Enum, minBound, maxBound)
-
-
-import Data.Aeson hiding (Error, fieldLabelModifier)
+import Data.Time (UTCTime)
import Data.Aeson.TH (deriveJSON)
-import Data.Eq (Eq())
-import Data.Either
-import Data.List (repeat, permutations)
import Data.Swagger
-import Data.Swagger.SchemaOptions
-import Data.Text (Text, pack)
-import Database.PostgreSQL.Simple (Connection)
-
+import Data.Text (Text)
import Servant
import Test.QuickCheck.Arbitrary
import Test.QuickCheck (elements)
-- import Control.Applicative ((<*>))
-
import Gargantext.Prelude
import Gargantext.Core.Utils.Prefix (unPrefix)
+import Gargantext.Core.Types.Main (Offset, Limit)
+import Gargantext.Database.Types.Node
import Gargantext.Database.TextSearch
-
-
+import Gargantext.Database.Facet
+import Gargantext.Database.Utils (Cmd)
-----------------------------------------------------------------------
+-- | SearchIn [NodesId] if empty then global search
+-- TODO [Int]
data SearchQuery = SearchQuery { sq_query :: [Text]
- , sq_parent_id :: Int
+ , sq_corpus_id :: NodeId
} deriving (Generic)
$(deriveJSON (unPrefix "sq_") ''SearchQuery)
instance ToSchema SearchQuery where
genericDeclareNamedSchema
defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
-
instance Arbitrary SearchQuery where
arbitrary = elements [SearchQuery ["electrodes"] 472764]
------------------------------------------------------------------------
+--
-data SearchResult = SearchResult { sr_id :: Int
- , sr_title :: Text
- , sr_authors :: Text
- } deriving (Generic)
-$(deriveJSON (unPrefix "sr_") ''SearchResult)
-instance Arbitrary SearchResult where
- arbitrary = elements [SearchResult 1 "Title" "Authors"]
-
-instance ToSchema SearchResult where
+data SearchInQuery = SearchInQuery { siq_query :: [Text]
+ } deriving (Generic)
+$(deriveJSON (unPrefix "siq_") ''SearchInQuery)
+instance ToSchema SearchInQuery where
declareNamedSchema =
genericDeclareNamedSchema
- defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 3 fieldLabel}
+ defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 4 fieldLabel}
+
+instance Arbitrary SearchInQuery where
+ arbitrary = SearchInQuery <$> arbitrary
+
-----------------------------------------------------------------------
-data SearchResults = SearchResults { srs_results :: [SearchResult]}
+data SearchResults = SearchResults { srs_results :: [FacetPaired Int UTCTime HyperdataDocument Int [Pair Int Text]]}
deriving (Generic)
$(deriveJSON (unPrefix "srs_") ''SearchResults)
genericDeclareNamedSchema
defaultSchemaOptions {fieldLabelModifier = \fieldLabel -> drop 4 fieldLabel}
-
-
-----------------------------------------------------------------------
+-- TODO-ACCESS: CanSearch? or is it part of CanGetNode
+-- TODO-EVENTS: No event, this is a read-only query.
type SearchAPI = Post '[JSON] SearchResults
-----------------------------------------------------------------------
-search :: Connection -> SearchQuery -> Handler SearchResults
-search c (SearchQuery q pId) =
- liftIO $ SearchResults <$> map (\(i, y, t, s, a, _) -> SearchResult i (cs $ encode t) (cs $ encode a))
- <$> textSearch c (toTSQuery q) pId 5 0 Desc
+search :: SearchQuery -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> Cmd err SearchResults
+search (SearchQuery q pId) o l order =
+ SearchResults <$> searchInCorpusWithContacts pId q o l order
+
+searchIn :: NodeId -> SearchInQuery -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> Cmd err SearchResults
+searchIn nId (SearchInQuery q ) o l order =
+ SearchResults <$> searchInCorpusWithContacts nId q o l order