Commit 7a46e772 authored by Karen Konou's avatar Karen Konou

[Graph/Phylo] Doclist filtering

parent e73b0fb3
Pipeline #6323 passed with stages
in 34 minutes and 24 seconds
...@@ -22,7 +22,8 @@ import Data.Morpheus.Types ...@@ -22,7 +22,8 @@ import Data.Morpheus.Types
, ResolverM , ResolverM
, QUERY , QUERY
) )
import Data.Text (pack) import Data.Text (pack, unpack)
import qualified Data.Text as Text
import Data.Time.Format.ISO8601 (iso8601Show) import Data.Time.Format.ISO8601 (iso8601Show)
import Gargantext.API.Admin.Types (HasSettings) import Gargantext.API.Admin.Types (HasSettings)
import Gargantext.API.Errors.Types ( BackendInternalError ) import Gargantext.API.Errors.Types ( BackendInternalError )
...@@ -94,7 +95,7 @@ data ContextsForNgramsArgs ...@@ -94,7 +95,7 @@ data ContextsForNgramsArgs
= ContextsForNgramsArgs = ContextsForNgramsArgs
{ corpus_id :: Int { corpus_id :: Int
, ngrams_terms :: [Text] , ngrams_terms :: [Text]
, and_logic :: Bool , and_logic :: Text
} deriving (Generic, GQLType) } deriving (Generic, GQLType)
data NodeContextCategoryMArgs = NodeContextCategoryMArgs data NodeContextCategoryMArgs = NodeContextCategoryMArgs
...@@ -150,9 +151,9 @@ dbNodeContext context_id node_id = do ...@@ -150,9 +151,9 @@ dbNodeContext context_id node_id = do
-- | Returns list of `ContextGQL` for given ngrams in given corpus id. -- | Returns list of `ContextGQL` for given ngrams in given corpus id.
dbContextForNgrams dbContextForNgrams
:: (CmdCommon env) :: (CmdCommon env)
=> Int -> [Text] -> Bool -> GqlM e env [ContextGQL] => Int -> [Text] -> Text -> GqlM e env [ContextGQL]
dbContextForNgrams node_id ngrams_terms and_logic = do dbContextForNgrams node_id ngrams_terms and_logic = do
contextsForNgramsTerms <- lift $ getContextsForNgramsTerms (UnsafeMkNodeId node_id) ngrams_terms and_logic contextsForNgramsTerms <- lift $ getContextsForNgramsTerms (UnsafeMkNodeId node_id) ngrams_terms ( readMaybe $ unpack $ Text.toTitle and_logic )
--lift $ printDebug "[dbContextForNgrams] contextsForNgramsTerms" contextsForNgramsTerms --lift $ printDebug "[dbContextForNgrams] contextsForNgramsTerms" contextsForNgramsTerms
pure $ toContextGQL <$> contextsForNgramsTerms pure $ toContextGQL <$> contextsForNgramsTerms
......
...@@ -152,11 +152,11 @@ data ContextForNgramsTerms = ...@@ -152,11 +152,11 @@ data ContextForNgramsTerms =
getContextsForNgramsTerms :: HasNodeError err getContextsForNgramsTerms :: HasNodeError err
=> NodeId => NodeId
-> [Text] -> [Text]
-> Bool -> Maybe Bool
-> DBCmd err [ContextForNgramsTerms] -> DBCmd err [ContextForNgramsTerms]
getContextsForNgramsTerms cId ngramsTerms and_logic = do getContextsForNgramsTerms cId ngramsTerms (Just True) = do
let terms_length = length ngramsTerms let terms_length = length ngramsTerms
res <- runPGSQuery (query and_logic) (cId, PGS.In ngramsTerms, terms_length) res <- runPGSQuery query (cId, PGS.In ngramsTerms, terms_length)
pure $ (\( _cfnt_nodeId pure $ (\( _cfnt_nodeId
, _cfnt_hash , _cfnt_hash
, _cfnt_nodeTypeId , _cfnt_nodeTypeId
...@@ -168,8 +168,8 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do ...@@ -168,8 +168,8 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do
, _cfnt_score , _cfnt_score
, _cfnt_category) -> ContextForNgramsTerms { .. }) <$> res , _cfnt_category) -> ContextForNgramsTerms { .. }) <$> res
where where
query :: Bool -> PGS.Query query :: PGS.Query
query False = [sql| SELECT t.id, t.hash_id, t.typename, t.user_id, t.parent_id, t.name, t.date, t.hyperdata, t.score, t.category query = [sql| SELECT t.id, t.hash_id, t.typename, t.user_id, t.parent_id, t.name, t.date, t.hyperdata, t.score, t.category
FROM ( FROM (
SELECT DISTINCT ON (contexts.id) SELECT DISTINCT ON (contexts.id)
contexts.id AS id, contexts.id AS id,
...@@ -188,11 +188,31 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do ...@@ -188,11 +188,31 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do
JOIN nodes_contexts ON contexts.id = nodes_contexts.context_id JOIN nodes_contexts ON contexts.id = nodes_contexts.context_id
JOIN ngrams ON context_node_ngrams.ngrams_id = ngrams.id JOIN ngrams ON context_node_ngrams.ngrams_id = ngrams.id
WHERE nodes_contexts.node_id = ? WHERE nodes_contexts.node_id = ?
AND ngrams.terms IN ?) t AND ngrams.terms IN ?
GROUP BY
contexts.id,
nodes_contexts.score,
nodes_contexts.category
HAVING COUNT(DISTINCT ngrams.terms) = ?) t
-- ORDER BY t.doc_count DESC -- ORDER BY t.doc_count DESC
ORDER BY t.score DESC ORDER BY t.score DESC
|] |]
query True = [sql| SELECT t.id, t.hash_id, t.typename, t.user_id, t.parent_id, t.name, t.date, t.hyperdata, t.score, t.category
getContextsForNgramsTerms cId ngramsTerms _ = do
res <- runPGSQuery query (cId, PGS.In ngramsTerms)
pure $ (\( _cfnt_nodeId
, _cfnt_hash
, _cfnt_nodeTypeId
, _cfnt_userId
, _cfnt_parentId
, _cfnt_c_title
, _cfnt_date
, _cfnt_hyperdata
, _cfnt_score
, _cfnt_category) -> ContextForNgramsTerms { .. }) <$> res
where
query :: PGS.Query
query = [sql| SELECT t.id, t.hash_id, t.typename, t.user_id, t.parent_id, t.name, t.date, t.hyperdata, t.score, t.category
FROM ( FROM (
SELECT DISTINCT ON (contexts.id) SELECT DISTINCT ON (contexts.id)
contexts.id AS id, contexts.id AS id,
...@@ -211,9 +231,7 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do ...@@ -211,9 +231,7 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do
JOIN nodes_contexts ON contexts.id = nodes_contexts.context_id JOIN nodes_contexts ON contexts.id = nodes_contexts.context_id
JOIN ngrams ON context_node_ngrams.ngrams_id = ngrams.id JOIN ngrams ON context_node_ngrams.ngrams_id = ngrams.id
WHERE nodes_contexts.node_id = ? WHERE nodes_contexts.node_id = ?
AND ngrams.terms IN ? AND ngrams.terms IN ?) t
GROUP BY contexts.id
HAVING COUNT(DISTINCT ngrams.terms) = ?) t
-- ORDER BY t.doc_count DESC -- ORDER BY t.doc_count DESC
ORDER BY t.score DESC ORDER BY t.score DESC
|] |]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment