Commit e3241819 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Allow more results from partial match search

parent d6c03dc3
Pipeline #6503 failed with stages
in 13 minutes and 22 seconds
......@@ -29,7 +29,7 @@ source-repository-package
source-repository-package
type: git
location: https://gitlab.iscpif.fr/gargantext/opaleye-textsearch.git
tag: cb07b604bfb7a22aa21dd8918de5cb65c8a4bdf1
tag: 04b5c9044fef44393b66bffa258ca0b0f59c1087
source-repository-package
type: git
......
......@@ -387,7 +387,7 @@ constraints: any.Cabal ==3.8.1.0,
any.old-locale ==1.0.0.7,
any.old-time ==1.1.0.3,
any.opaleye ==0.9.6.1,
any.opaleye-textsearch ==0.1.0.0,
any.opaleye-textsearch ==0.2.0.0,
any.openalex ==0.1.0.0,
any.optics-core ==0.4.1.1,
optics-core -explicit-generic-labels,
......
......@@ -605,7 +605,7 @@ library
, network >= 3.1.4.0
, network-uri ^>= 2.6.4.1
, opaleye ^>= 0.9.6.1
, opaleye-textsearch >= 0.1.0.0
, opaleye-textsearch >= 0.2.0.0
, openalex
, pandoc ^>= 2.14.0.3
, parallel ^>= 3.2.2.0
......
......@@ -61,7 +61,9 @@ import Opaleye.TextSearch
--
queryToTsSearch :: API.Query -> Field SqlTSQuery
queryToTsSearch q = sqlToTSQuery $ T.unpack $ API.interpretQuery q transformAST
queryToTsSearch q =
let (dictionary, transformed) = API.interpretQuery q transformAST
in sqlToTSQuery dictionary (T.unpack transformed)
where
-- It's important to understand how things work under the hood: When we perform
......@@ -97,29 +99,45 @@ queryToTsSearch q = sqlToTSQuery $ T.unpack $ API.interpretQuery q transformAST
API.QT_partial_match (Term term)
-> stem EN GargPorterAlgorithm term <> ":*"
transformAST :: BoolExpr [API.QueryTerm] -> T.Text
-- Transforms the input query terms and returns the full SQL query to feed Postgres AND
-- the dictionary to use, see: https://www.postgresql.org/docs/current/textsearch-dictionaries.html
-- In a nutshell, if we have a partial match operator in our query, we use the \"simple\" dictionary
-- under the hood, which won't filter stop words, which are sometimes useful, see issue #265.
transformAST :: BoolExpr [API.QueryTerm] -> (Maybe Dictionary, T.Text)
transformAST ast = case ast of
BAnd sub1 sub2
-> " (" <> transformAST sub1 <> " & " <> transformAST sub2 <> ") "
-> let (d1, sub1Expr) = transformAST sub1
(d2, sub2Expr) = transformAST sub2
in (d1 <|> d2, " (" <> sub1Expr <> " & " <> sub2Expr <> ") ")
BOr sub1 sub2
-> " (" <> transformAST sub1 <> " | " <> transformAST sub2 <> ") "
-> let (d1, sub1Expr) = transformAST sub1
(d2, sub2Expr) = transformAST sub2
in (d1 <|> d2, " (" <> sub1Expr <> " | " <> sub2Expr <> ") ")
BNot (BConst (Negative term))
-> transformAST (BConst (Positive term)) -- double negation
BNot sub
-> "!" <> transformAST sub
-> second (\e -> "!" <> e) $ transformAST sub
-- BTrue cannot happen is the query parser doesn't support parsing 'TRUE' alone.
BTrue
-> T.empty
-> (Nothing, T.empty)
-- BTrue cannot happen is the query parser doesn't support parsing 'FALSE' alone.
BFalse
-> T.empty
-> (Nothing, T.empty)
BConst (Positive queryTerms)
-> renderQueryTerms queryTerms
-> (pickDictionary queryTerms, renderQueryTerms queryTerms)
-- We can handle negatives via `ANDNOT` with itself.
BConst (Negative queryTerms)
-> "!" <> renderQueryTerms queryTerms
-> (pickDictionary queryTerms, "!" <> renderQueryTerms queryTerms)
pickDictionary :: [API.QueryTerm] -> Maybe Dictionary
pickDictionary qs = if any isPartialMatch qs then Just (Dictionary "simple") else Nothing
where
isPartialMatch :: API.QueryTerm -> Bool
isPartialMatch = \case
API.QT_partial_match{} -> True
_ -> False
------------------------------------------------------------------------
searchDocInDatabase :: HasDBid NodeType
=> ParentId
......
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