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
, ResolverM
, QUERY
)
import Data.Text (pack)
import Data.Text (pack, unpack)
import qualified Data.Text as Text
import Data.Time.Format.ISO8601 (iso8601Show)
import Gargantext.API.Admin.Types (HasSettings)
import Gargantext.API.Errors.Types ( BackendInternalError )
......@@ -94,7 +95,7 @@ data ContextsForNgramsArgs
= ContextsForNgramsArgs
{ corpus_id :: Int
, ngrams_terms :: [Text]
, and_logic :: Bool
, and_logic :: Text
} deriving (Generic, GQLType)
data NodeContextCategoryMArgs = NodeContextCategoryMArgs
......@@ -150,9 +151,9 @@ dbNodeContext context_id node_id = do
-- | Returns list of `ContextGQL` for given ngrams in given corpus id.
dbContextForNgrams
:: (CmdCommon env)
=> Int -> [Text] -> Bool -> GqlM e env [ContextGQL]
=> Int -> [Text] -> Text -> GqlM e env [ContextGQL]
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
pure $ toContextGQL <$> contextsForNgramsTerms
......
......@@ -152,11 +152,11 @@ data ContextForNgramsTerms =
getContextsForNgramsTerms :: HasNodeError err
=> NodeId
-> [Text]
-> Bool
-> Maybe Bool
-> DBCmd err [ContextForNgramsTerms]
getContextsForNgramsTerms cId ngramsTerms and_logic = do
getContextsForNgramsTerms cId ngramsTerms (Just True) = do
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
, _cfnt_hash
, _cfnt_nodeTypeId
......@@ -168,8 +168,8 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do
, _cfnt_score
, _cfnt_category) -> ContextForNgramsTerms { .. }) <$> res
where
query :: Bool -> 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 :: 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 (
SELECT DISTINCT ON (contexts.id)
contexts.id AS id,
......@@ -188,11 +188,31 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do
JOIN nodes_contexts ON contexts.id = nodes_contexts.context_id
JOIN ngrams ON context_node_ngrams.ngrams_id = ngrams.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.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 (
SELECT DISTINCT ON (contexts.id)
contexts.id AS id,
......@@ -211,9 +231,7 @@ getContextsForNgramsTerms cId ngramsTerms and_logic = do
JOIN nodes_contexts ON contexts.id = nodes_contexts.context_id
JOIN ngrams ON context_node_ngrams.ngrams_id = ngrams.id
WHERE nodes_contexts.node_id = ?
AND ngrams.terms IN ?
GROUP BY contexts.id
HAVING COUNT(DISTINCT ngrams.terms) = ?) t
AND ngrams.terms IN ?) t
-- ORDER BY t.doc_count 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