[search] implement ts_query search

This has couple of drawbacks:
- we use postgres-simple and a raw sql query, opaleye would be better
I guess
- there is a hack for empty query, this should be replaced by
removing the WHERE clause in this case
parent 0cc7bfc3
Pipeline #1846 passed with stage
in 34 minutes and 21 seconds
...@@ -38,6 +38,9 @@ CREATE TABLE public.nodes ( ...@@ -38,6 +38,9 @@ CREATE TABLE public.nodes (
FOREIGN KEY (user_id) REFERENCES public.auth_user(id) ON DELETE CASCADE FOREIGN KEY (user_id) REFERENCES public.auth_user(id) ON DELETE CASCADE
); );
ALTER TABLE public.nodes OWNER TO gargantua; ALTER TABLE public.nodes OWNER TO gargantua;
ALTER TABLE nodes
ADD COLUMN search_title tsvector
GENERATED ALWAYS AS (to_tsvector('english', coalesce("name", '') || ' ' || coalesce("hyperdata"->>'abstract', ''))) STORED;
-------------------------------------------------------------- --------------------------------------------------------------
-- | Ngrams -- | Ngrams
......
...@@ -24,7 +24,6 @@ import Gargantext.Database.Prelude (Cmd, execPGSQuery) ...@@ -24,7 +24,6 @@ import Gargantext.Database.Prelude (Cmd, execPGSQuery)
import Gargantext.Prelude import Gargantext.Prelude
import qualified Database.PostgreSQL.Simple as DPS import qualified Database.PostgreSQL.Simple as DPS
triggerSearchUpdate :: HasDBid NodeType => Cmd err Int64 triggerSearchUpdate :: HasDBid NodeType => Cmd err Int64
triggerSearchUpdate = execPGSQuery query ( toDBid NodeDocument triggerSearchUpdate = execPGSQuery query ( toDBid NodeDocument
, toDBid NodeDocument , toDBid NodeDocument
......
...@@ -44,6 +44,8 @@ import Control.Arrow (returnA) ...@@ -44,6 +44,8 @@ import Control.Arrow (returnA)
import Control.Lens ((^.)) import Control.Lens ((^.))
import Data.Aeson (FromJSON, ToJSON) import Data.Aeson (FromJSON, ToJSON)
import Data.Aeson.TH (deriveJSON) 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.Profunctor.Product.TH (makeAdaptorAndInstance)
import Data.Swagger import Data.Swagger
import qualified Data.Text as T import qualified Data.Text as T
...@@ -68,6 +70,9 @@ import Gargantext.Database.Query.Table.NodeNodeNgrams ...@@ -68,6 +70,9 @@ import Gargantext.Database.Query.Table.NodeNodeNgrams
import Gargantext.Database.Prelude import Gargantext.Database.Prelude
import Gargantext.Database.Schema.Node import Gargantext.Database.Schema.Node
--import qualified Opaleye.Internal.Column as C
--import qualified Opaleye.Internal.HaskellDB.PrimQuery as HPQ
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | DocFacet -- | DocFacet
...@@ -302,11 +307,30 @@ runViewDocuments :: HasDBid NodeType ...@@ -302,11 +307,30 @@ runViewDocuments :: HasDBid NodeType
-> Maybe OrderBy -> Maybe OrderBy
-> Maybe Text -> Maybe Text
-> Cmd err [FacetDoc] -> Cmd err [FacetDoc]
runViewDocuments cId t o l order query = do runViewDocuments cId t _o _l _order query = do
runOpaQuery $ filterWith o l order sqlQuery 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
-- runOpaQuery $ filterWith o l order sqlQuery
where where
ntId :: Int
ntId = toDBid NodeDocument ntId = toDBid NodeDocument
sqlQuery = viewDocuments cId t ntId query -- 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 runCountDocuments :: HasDBid NodeType => CorpusId -> IsTrash -> Maybe Text -> Cmd err Int
runCountDocuments cId t mQuery = do runCountDocuments cId t mQuery = do
......
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