Commit 64dbad9e authored by Alexandre Delanoë's avatar Alexandre Delanoë

Merge branch 'dev-merge' into dev

parents 1667c63e b9028127
......@@ -2,7 +2,8 @@ version: '3'
services:
postgres:
image: 'postgres:latest'
#image: 'postgres:latest'
image: 'postgres:11'
network_mode: host
#ports:
#- 5432:5432
......
......@@ -38,6 +38,9 @@ CREATE TABLE public.nodes (
FOREIGN KEY (user_id) REFERENCES public.auth_user(id) ON DELETE CASCADE
);
ALTER TABLE public.nodes OWNER TO gargantua;
ALTER TABLE nodes ADD COLUMN search_title tsvector;
UPDATE nodes SET search_title = to_tsvector('english', coalesce("hyperdata"->>'title', '') || ' ' || coalesce("hyperdata"->>'abstract', ''));
CREATE INDEX search_title_idx ON nodes USING GIN (search_title);
--------------------------------------------------------------
-- | Ngrams
......@@ -207,5 +210,5 @@ CREATE OR REPLACE function node_pos(int, int) returns bigint
LANGUAGE SQL immutable;
--drop index node_by_pos;
create index node_by_pos on nodes using btree(node_pos(id,typename));
--create index node_by_pos on nodes using btree(node_pos(id,typename));
ALTER TABLE nodes
DROP COLUMN IF EXISTS search_title,
DROP COLUMN IF EXISTS tsvector;
ALTER TABLE nodes ADD COLUMN search_title tsvector;
UPDATE nodes SET search_title = to_tsvector('english', coalesce("hyperdata"->>'title', '') || ' ' || coalesce("hyperdata"->>'abstract', ''));
CREATE INDEX search_title_idx ON nodes USING GIN (search_title);
......@@ -188,9 +188,9 @@ instance FromField HyperdataGraph
where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataGraph
instance DefaultFromField PGJsonb HyperdataGraph
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
-----------------------------------------------------------
-- This type is used to return graph via API
......
......@@ -102,7 +102,7 @@ import qualified Gargantext.Core.Text.Corpus.API as API
import qualified Gargantext.Database.Query.Table.Node.Document.Add as Doc (add)
------------------------------------------------------------------------
-- Impots for upgrade function
-- Imports for upgrade function
import Gargantext.Database.Query.Tree.Root (getRootId)
import Gargantext.Database.Query.Tree (findNodesId)
import qualified Data.List as List
......
......@@ -55,7 +55,7 @@ isPairedWith nId nt = runOpaQuery (selectQuery nt nId)
selectQuery :: NodeType -> NodeId -> Query (Column PGInt4)
selectQuery nt' nId' = proc () -> do
(node, node_node) <- queryJoin -< ()
restrict -< (node^.node_typename) .== (pgInt4 $ toDBid nt')
restrict -< (node^.node_typename) .== (sqlInt4 $ toDBid nt')
restrict -< (node_node^.nn_node1_id) .== (toNullable $ pgNodeId nId')
returnA -< node^.node_id
......
......@@ -14,18 +14,13 @@ module Gargantext.Database.Action.Search where
import Control.Arrow (returnA)
import Control.Lens ((^.))
import Data.Aeson
import Data.List (intersperse)
import Data.Maybe
import Data.String (IsString(..))
import Data.Text (Text, words, unpack, intercalate)
import Data.Text (Text, unpack, intercalate)
import Data.Time (UTCTime)
import Database.PostgreSQL.Simple (Query)
import Database.PostgreSQL.Simple.ToField
import Gargantext.Core
import Gargantext.Core.Types
import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..), HyperdataContact(..))
import Gargantext.Database.Prelude (Cmd, runPGSQuery, runOpaQuery, runCountOpaQuery)
import Gargantext.Database.Prelude (Cmd, runOpaQuery, runCountOpaQuery)
import Gargantext.Database.Query.Facet
import Gargantext.Database.Query.Filter
import Gargantext.Database.Query.Join (leftJoin5)
......@@ -43,14 +38,14 @@ searchDocInDatabase :: HasDBid NodeType
=> ParentId
-> Text
-> Cmd err [(NodeId, HyperdataDocument)]
searchDocInDatabase p t = runOpaQuery (queryDocInDatabase p t)
searchDocInDatabase _p t = runOpaQuery (queryDocInDatabase t)
where
-- | Global search query where ParentId is Master Node Corpus Id
queryDocInDatabase :: ParentId -> Text -> O.Query (Column PGInt4, Column PGJsonb)
queryDocInDatabase _ q = proc () -> do
queryDocInDatabase :: Text -> O.Query (Column PGInt4, Column PGJsonb)
queryDocInDatabase q = proc () -> do
row <- queryNodeSearchTable -< ()
restrict -< (_ns_search row) @@ (pgTSQuery (unpack q))
restrict -< (_ns_typename row) .== (pgInt4 $ toDBid NodeDocument)
restrict -< (_ns_typename row) .== (sqlInt4 $ toDBid NodeDocument)
returnA -< (_ns_id row, _ns_hyperdata row)
------------------------------------------------------------------------
......@@ -88,10 +83,10 @@ queryInCorpus cId t q = proc () -> do
(n, nn) <- joinInCorpus -< ()
restrict -< (nn^.nn_node1_id) .== (toNullable $ pgNodeId cId)
restrict -< if t
then (nn^.nn_category) .== (toNullable $ pgInt4 0)
else (nn^.nn_category) .>= (toNullable $ pgInt4 1)
then (nn^.nn_category) .== (toNullable $ sqlInt4 0)
else (nn^.nn_category) .>= (toNullable $ sqlInt4 1)
restrict -< (n ^. ns_search) @@ (pgTSQuery (unpack q))
restrict -< (n ^. ns_typename ) .== (pgInt4 $ toDBid NodeDocument)
restrict -< (n ^. ns_typename ) .== (sqlInt4 $ toDBid NodeDocument)
returnA -< FacetDoc (n^.ns_id )
(n^.ns_date )
(n^.ns_name )
......@@ -138,14 +133,14 @@ selectContactViaDoc
selectContactViaDoc cId aId q = proc () -> do
(doc, (corpus_doc, (_contact_doc, (annuaire_contact, contact)))) <- queryContactViaDoc -< ()
restrict -< (doc^.ns_search) @@ (pgTSQuery $ unpack q )
restrict -< (doc^.ns_typename) .== (pgInt4 $ toDBid NodeDocument)
restrict -< (doc^.ns_typename) .== (sqlInt4 $ toDBid NodeDocument)
restrict -< (corpus_doc^.nn_node1_id) .== (toNullable $ pgNodeId cId)
restrict -< (annuaire_contact^.nn_node1_id) .== (toNullable $ pgNodeId aId)
restrict -< (contact^.node_typename) .== (toNullable $ pgInt4 $ toDBid NodeContact)
restrict -< (contact^.node_typename) .== (toNullable $ sqlInt4 $ toDBid NodeContact)
returnA -< ( contact^.node_id
, contact^.node_date
, contact^.node_hyperdata
, toNullable $ pgInt4 1
, toNullable $ sqlInt4 1
)
selectGroup :: HasDBid NodeType
......@@ -213,66 +208,3 @@ queryContactViaDoc =
------------------------------------------------------------------------
newtype TSQuery = UnsafeTSQuery [Text]
-- | TODO [""] -> panic "error"
toTSQuery :: [Text] -> TSQuery
toTSQuery txt = UnsafeTSQuery $ map stemIt txt
instance IsString TSQuery
where
fromString = UnsafeTSQuery . words . cs
instance ToField TSQuery
where
toField (UnsafeTSQuery xs)
= Many $ intersperse (Plain " && ")
$ map (\q -> Many [ Plain "plainto_tsquery("
, Escape (cs q)
, Plain ")"
]
) xs
data Order = Asc | Desc
instance ToField Order
where
toField Asc = Plain "ASC"
toField Desc = Plain "DESC"
-- TODO
-- FIX fav
-- ADD ngrams count
-- TESTS
textSearchQuery :: Query
textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
\ , n.hyperdata->'title' \
\ , n.hyperdata->'source' \
\ , n.hyperdata->'authors' \
\ , COALESCE(nn.score,null) \
\ FROM nodes n \
\ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
\ WHERE \
\ n.search @@ (?::tsquery) \
\ AND (n.parent_id = ? OR nn.node1_id = ?) \
\ AND n.typename = ? \
\ ORDER BY n.hyperdata -> 'publication_date' ? \
\ offset ? limit ?;"
-- | Text Search Function for Master Corpus
-- TODO : text search for user corpus
-- Example:
-- textSearchTest :: ParentId -> TSQuery -> Cmd err [(Int, Value, Value, Value, Value, Maybe Int)]
-- textSearchTest pId q = textSearch q pId 5 0 Asc
textSearch :: HasDBid NodeType
=> TSQuery -> ParentId
-> Limit -> Offset -> Order
-> Cmd err [(Int,Value,Value,Value, Value, Maybe Int)]
textSearch q p l o ord = runPGSQuery textSearchQuery (q,p,p,typeId,ord,o,l)
where
typeId = toDBid NodeDocument
module Gargantext.Database.Action.TSQuery where
import Data.Aeson
import Data.List (intersperse)
import Data.Maybe
import Data.String (IsString(..))
import Data.Text (Text, words)
import Database.PostgreSQL.Simple (Query)
import Database.PostgreSQL.Simple.ToField
import Gargantext.Core
import Gargantext.Core.Types
import Gargantext.Database.Prelude (Cmd, runPGSQuery)
import Gargantext.Prelude
import Gargantext.Core.Text.Terms.Mono.Stem.En (stemIt)
newtype TSQuery = UnsafeTSQuery [Text]
-- | TODO [""] -> panic "error"
toTSQuery :: [Text] -> TSQuery
toTSQuery txt = UnsafeTSQuery $ map stemIt txt
instance IsString TSQuery
where
fromString = UnsafeTSQuery . words . cs
instance ToField TSQuery
where
toField (UnsafeTSQuery xs)
= Many $ intersperse (Plain " && ")
$ map (\q -> Many [ Plain "plainto_tsquery("
, Escape (cs q)
, Plain ")"
]
) xs
data Order = Asc | Desc
instance ToField Order
where
toField Asc = Plain "ASC"
toField Desc = Plain "DESC"
-- TODO
-- FIX fav
-- ADD ngrams count
-- TESTS
textSearchQuery :: Query
textSearchQuery = "SELECT n.id, n.hyperdata->'publication_year' \
\ , n.hyperdata->'title' \
\ , n.hyperdata->'source' \
\ , n.hyperdata->'authors' \
\ , COALESCE(nn.score,null) \
\ FROM nodes n \
\ LEFT JOIN nodes_nodes nn ON nn.node2_id = n.id \
\ WHERE \
\ n.search @@ (?::tsquery) \
\ AND (n.parent_id = ? OR nn.node1_id = ?) \
\ AND n.typename = ? \
\ ORDER BY n.hyperdata -> 'publication_date' ? \
\ offset ? limit ?;"
-- | Text Search Function for Master Corpus
-- TODO : text search for user corpus
-- Example:
-- textSearchTest :: ParentId -> TSQuery -> Cmd err [(Int, Value, Value, Value, Value, Maybe Int)]
-- textSearchTest pId q = textSearch q pId 5 0 Asc
textSearch :: HasDBid NodeType
=> TSQuery -> ParentId
-> Limit -> Offset -> Order
-> Cmd err [(Int,Value,Value,Value, Value, Maybe Int)]
textSearch q p l o ord = runPGSQuery textSearchQuery (q,p,p,typeId,ord,o,l)
where
typeId = toDBid NodeDocument
......@@ -45,7 +45,7 @@ instance ToSchema HyperdataAny where
instance FromField HyperdataAny where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataAny
instance DefaultFromField PGJsonb HyperdataAny
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
......@@ -166,12 +166,12 @@ instance FromField HyperdataContact where
fromField = fromField'
-- | Database (Opaleye instance)
instance QueryRunnerColumnDefault PGJsonb HyperdataContact where
queryRunnerColumnDefault = fieldQueryRunnerColumn
instance DefaultFromField PGJsonb HyperdataContact where
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault (Nullable PGJsonb) HyperdataContact where
queryRunnerColumnDefault = fieldQueryRunnerColumn
instance DefaultFromField (Nullable PGJsonb) HyperdataContact where
defaultFromField = fieldQueryRunnerColumn
......
......@@ -90,10 +90,10 @@ instance FromField HyperdataAnnuaire
where
fromField = fromField'
------------------------------------------------------------------------
instance QueryRunnerColumnDefault PGJsonb HyperdataCorpus
instance DefaultFromField PGJsonb HyperdataCorpus
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGJsonb HyperdataAnnuaire
instance DefaultFromField PGJsonb HyperdataAnnuaire
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
......@@ -71,7 +71,7 @@ instance ToSchema HyperdataDashboard where
instance FromField HyperdataDashboard where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataDashboard
instance DefaultFromField PGJsonb HyperdataDashboard
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
......@@ -202,11 +202,11 @@ instance ToField HyperdataDocumentV3 where
toField = toJSONField
------------------------------------------------------------------------
instance QueryRunnerColumnDefault PGJsonb HyperdataDocument
instance DefaultFromField PGJsonb HyperdataDocument
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGJsonb HyperdataDocumentV3
instance DefaultFromField PGJsonb HyperdataDocumentV3
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
------------------------------------------------------------------------
......@@ -54,9 +54,9 @@ instance FromField HyperdataFile
where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataFile
instance DefaultFromField PGJsonb HyperdataFile
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance ToSchema HyperdataFile where
declareNamedSchema proxy =
......
......@@ -53,9 +53,9 @@ instance FromField HyperdataFrame
where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataFrame
instance DefaultFromField PGJsonb HyperdataFrame
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance ToSchema HyperdataFrame where
declareNamedSchema proxy =
......
......@@ -98,12 +98,12 @@ instance FromField HyperdataListCooc
where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataList
instance DefaultFromField PGJsonb HyperdataList
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGJsonb HyperdataListCooc
defaultFromField = fieldQueryRunnerColumn
instance DefaultFromField PGJsonb HyperdataListCooc
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance ToSchema HyperdataList where
......
......@@ -48,9 +48,9 @@ instance FromField HyperdataModel
where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataModel
instance DefaultFromField PGJsonb HyperdataModel
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance ToSchema HyperdataModel where
declareNamedSchema proxy =
......
......@@ -56,6 +56,6 @@ instance ToSchema HyperdataPhylo where
instance FromField HyperdataPhylo where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataPhylo
instance DefaultFromField PGJsonb HyperdataPhylo
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
......@@ -48,7 +48,7 @@ import GHC.Generics (Generic)
import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger, wellNamedSchema)
import Gargantext.Database.Prelude (fromField')
import Gargantext.Prelude
import Opaleye (QueryRunnerColumnDefault, queryRunnerColumnDefault, PGJsonb, fieldQueryRunnerColumn, Nullable)
import Opaleye (DefaultFromField(..), PGJsonb, defaultFromField, fieldQueryRunnerColumn, Nullable)
import Test.QuickCheck (elements)
import Test.QuickCheck.Arbitrary hiding (vector)
......
......@@ -54,7 +54,7 @@ instance ToSchema HyperdataTexts where
instance FromField HyperdataTexts where
fromField = fromField'
instance QueryRunnerColumnDefault PGJsonb HyperdataTexts
instance DefaultFromField PGJsonb HyperdataTexts
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
......@@ -120,12 +120,12 @@ instance FromField HyperdataPublic where
fromField = fromField'
-- | Database (Opaleye instance)
instance QueryRunnerColumnDefault PGJsonb HyperdataUser where
queryRunnerColumnDefault = fieldQueryRunnerColumn
instance DefaultFromField PGJsonb HyperdataUser where
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGJsonb HyperdataPrivate where
queryRunnerColumnDefault = fieldQueryRunnerColumn
instance DefaultFromField PGJsonb HyperdataPrivate where
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGJsonb HyperdataPublic where
queryRunnerColumnDefault = fieldQueryRunnerColumn
instance DefaultFromField PGJsonb HyperdataPublic where
defaultFromField = fieldQueryRunnerColumn
......@@ -33,7 +33,7 @@ import Database.PostgreSQL.Simple.ToField (ToField, toField)
import GHC.Generics (Generic)
import Servant
import qualified Opaleye as O
import Opaleye (QueryRunnerColumnDefault, queryRunnerColumnDefault, PGInt4, PGText, PGTSVector, Nullable, fieldQueryRunnerColumn)
import Opaleye (DefaultFromField, defaultFromField, PGInt4, PGText, PGTSVector, Nullable, fieldQueryRunnerColumn)
import Test.QuickCheck (elements)
import Gargantext.Prelude.Crypto.Hash (Hash)
import Test.QuickCheck.Arbitrary
......@@ -82,18 +82,26 @@ instance (Typeable hyperdata, ToSchema hyperdata) =>
declareNamedSchema = wellNamedSchema "_node_"
instance (Typeable hyperdata, ToSchema hyperdata) =>
ToSchema (NodePolySearch NodeId NodeTypeId
(Maybe UserId)
ParentId NodeName
UTCTime hyperdata (Maybe TSVector)
ToSchema (NodePolySearch NodeId
NodeTypeId
(Maybe UserId)
ParentId
NodeName
UTCTime
hyperdata
(Maybe TSVector)
) where
declareNamedSchema = wellNamedSchema "_ns_"
instance (Typeable hyperdata, ToSchema hyperdata) =>
ToSchema (NodePolySearch NodeId NodeTypeId
UserId
(Maybe ParentId) NodeName
UTCTime hyperdata (Maybe TSVector)
ToSchema (NodePolySearch NodeId
NodeTypeId
UserId
(Maybe ParentId)
NodeName
UTCTime
hyperdata
(Maybe TSVector)
) where
declareNamedSchema = wellNamedSchema "_ns_"
......@@ -115,16 +123,29 @@ instance (Arbitrary hyperdata
,Arbitrary toDBid
,Arbitrary userId
,Arbitrary nodeParentId
) => Arbitrary (NodePolySearch nodeId toDBid userId nodeParentId
NodeName UTCTime hyperdata (Maybe TSVector)) where
) => Arbitrary (NodePolySearch nodeId
toDBid
userId
nodeParentId
NodeName
UTCTime
hyperdata
(Maybe TSVector)
) where
--arbitrary = Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (arbitrary) (Just "")
arbitrary = NodeSearch <$> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary
arbitrary = NodeSearch <$> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
------------------------------------------------------------------------
pgNodeId :: NodeId -> O.Column O.PGInt4
pgNodeId = O.pgInt4 . id2int
pgNodeId = O.sqlInt4 . id2int
where
id2int :: NodeId -> Int
id2int (NodeId n) = n
......@@ -333,28 +354,28 @@ instance FromField (NodeId, Text)
fromField = fromField'
-}
------------------------------------------------------------------------
instance QueryRunnerColumnDefault PGTSVector (Maybe TSVector)
instance DefaultFromField PGTSVector (Maybe TSVector)
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGInt4 (Maybe NodeId)
instance DefaultFromField PGInt4 (Maybe NodeId)
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGInt4 NodeId
instance DefaultFromField PGInt4 NodeId
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault (Nullable PGInt4) NodeId
instance DefaultFromField (Nullable PGInt4) NodeId
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance (QueryRunnerColumnDefault (Nullable O.PGTimestamptz) UTCTime)
instance (DefaultFromField (Nullable O.PGTimestamptz) UTCTime)
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
instance QueryRunnerColumnDefault PGText (Maybe Hash)
instance DefaultFromField PGText (Maybe Hash)
where
queryRunnerColumnDefault = fieldQueryRunnerColumn
defaultFromField = fieldQueryRunnerColumn
......@@ -31,7 +31,7 @@ import Database.PostgreSQL.Simple (Connection, connect)
import Database.PostgreSQL.Simple.FromField ( Conversion, ResultError(ConversionFailed), fromField, returnError)
import Database.PostgreSQL.Simple.Internal (Field)
import Gargantext.Prelude
import Opaleye (Query, Unpackspec, showSqlForPostgres, FromFields, Select, runQuery, PGJsonb, QueryRunnerColumnDefault)
import Opaleye (Query, Unpackspec, showSql, FromFields, Select, runSelect, PGJsonb, DefaultFromField)
import Opaleye.Aggregate (countRows)
import System.IO (FilePath)
import System.IO (stderr)
......@@ -56,7 +56,7 @@ instance HasConfig GargConfig where
hasConfig = identity
-------------------------------------------------------
type JSONB = QueryRunnerColumnDefault PGJsonb
type JSONB = DefaultFromField PGJsonb
-------------------------------------------------------
type CmdM'' env err m =
......@@ -111,11 +111,11 @@ runCmd env m = runExceptT $ runReaderT m env
runOpaQuery :: Default FromFields fields haskells
=> Select fields
-> Cmd err [haskells]
runOpaQuery q = mkCmd $ \c -> runQuery c q
runOpaQuery q = mkCmd $ \c -> runSelect c q
runCountOpaQuery :: Select a -> Cmd err Int
runCountOpaQuery q = do
counts <- mkCmd $ \c -> runQuery c $ countRows q
counts <- mkCmd $ \c -> runSelect c $ countRows q
-- countRows is guaranteed to return a list with exactly one row so DL.head is safe here
pure $ fromInt64ToInt $ DL.head counts
......@@ -189,5 +189,5 @@ fromField' field mb = do
]
printSqlOpa :: Default Unpackspec a a => Query a -> IO ()
printSqlOpa = putStrLn . maybe "Empty query" identity . showSqlForPostgres
printSqlOpa = putStrLn . maybe "Empty query" identity . showSql
......@@ -44,6 +44,8 @@ import Control.Arrow (returnA)
import Control.Lens ((^.))
import Data.Aeson (FromJSON, ToJSON)
import Data.Aeson.TH (deriveJSON)
--import qualified Database.PostgreSQL.Simple as DPS
--import Database.PostgreSQL.Simple.SqlQQ (sql)
import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
import Data.Swagger
import qualified Data.Text as T
......@@ -59,14 +61,17 @@ import qualified Opaleye.Internal.Unpackspec()
import Gargantext.Core
import Gargantext.Core.Types
import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger, wellNamedSchema)
-- import Gargantext.Database.Action.TSQuery (toTSQuery)
import Gargantext.Database.Admin.Types.Hyperdata
import Gargantext.Database.Query.Filter
import Gargantext.Database.Query.Join (leftJoin5)
import Gargantext.Database.Query.Table.Ngrams
import Gargantext.Database.Query.Table.Node (queryNodeSearchTable)
import Gargantext.Database.Query.Table.NodeNode
import Gargantext.Database.Query.Table.NodeNodeNgrams
import Gargantext.Database.Prelude
import Gargantext.Database.Schema.Node
import Gargantext.Prelude (printDebug)
------------------------------------------------------------------------
-- | DocFacet
......@@ -263,13 +268,13 @@ viewAuthorsDoc cId _ nt = proc () -> do
-}
restrict -< _node_id contact' .== (toNullable $ pgNodeId cId)
restrict -< _node_typename doc .== (pgInt4 $ toDBid nt)
restrict -< _node_typename doc .== (sqlInt4 $ toDBid nt)
returnA -< FacetDoc (_node_id doc)
(_node_date doc)
(_node_name doc)
(_node_hyperdata doc)
(toNullable $ pgInt4 1)
(toNullable $ sqlInt4 1)
(toNullable $ pgDouble 1)
(toNullable $ pgDouble 1)
......@@ -303,10 +308,29 @@ runViewDocuments :: HasDBid NodeType
-> Maybe Text
-> Cmd err [FacetDoc]
runViewDocuments cId t o l order query = do
-- docs <- runPGSQuery viewDocuments'
-- ( cId
-- , ntId
-- , (if t then 0 else 1) :: Int
-- , fromMaybe "" query
-- , fromMaybe "" query)
-- pure $ (\(id, date, name', hyperdata, category, score) -> FacetDoc id date name' hyperdata category score score) <$> docs
printDebug "[runViewDocuments] sqlQuery" $ showSql sqlQuery
runOpaQuery $ filterWith o l order sqlQuery
where
ntId = toDBid NodeDocument
sqlQuery = viewDocuments cId t ntId query
-- viewDocuments' :: DPS.Query
-- viewDocuments' = [sql|
-- SELECT n.id, n.date, n.name, n.hyperdata, nn.category, nn.score
-- FROM nodes AS n
-- JOIN nodes_nodes AS nn
-- ON n.id = nn.node2_id
-- WHERE nn.node1_id = ? -- corpusId
-- AND n.typename = ? -- NodeTypeId
-- AND nn.category = ? -- isTrash or not
-- AND (n.search_title @@ to_tsquery(?) OR ? = '') -- query with an OR hack for empty to_tsquery('') results
-- |]
runCountDocuments :: HasDBid NodeType => CorpusId -> IsTrash -> Maybe Text -> Cmd err Int