esqueleto-textsearch 1.1.4 → 1.1.5
raw patch · 6 files changed
+50/−15 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Database.Esqueleto.TextSearch.Language: instance GHC.Base.Semigroup Database.Esqueleto.TextSearch.Language.SearchTerm
+ Database.Esqueleto.TextSearch.Language: toSearchTermWeighted :: [Weight] -> Text -> Maybe SearchTerm
+ Database.Esqueleto.TextSearch.Types: instance GHC.Show.Show Database.Esqueleto.TextSearch.Types.QueryType
Files
- Changelog.md +4/−0
- README.md +14/−0
- esqueleto-textsearch.cabal +4/−3
- src/Database/Esqueleto/TextSearch/Language.hs +20/−5
- src/Database/Esqueleto/TextSearch/Types.hs +1/−0
- test/Database/Esqueleto/TextSearchSpec.hs +7/−7
Changelog.md view
@@ -1,6 +1,10 @@ # Change log for esqueleto text search +## Version 1.1.5++ enable tests in CI++ allow setting of weights in tsquery+ ## Version 1.1.4 + link to new homepage + add better documentation. rexport important stuff in the main module explicetly
README.md view
@@ -6,6 +6,20 @@ in performance for search as elastic search, without having to deal with elastic search. +## Hacking+install [nix](https://nixos.org/download) (the package manager).+Enter the nix shell.+```+nix develop+cabal build+```++running the tests with nix:+```+nix check+```++ ## Tutorial 1. decide which fields you want to search for,
esqueleto-textsearch.cabal view
@@ -1,5 +1,6 @@+cabal-version: 3.0 name: esqueleto-textsearch-version: 1.1.4+version: 1.1.5 synopsis: PostgreSQL full text search for Esqueleto description: PostgreSQL text search functions for Esqueleto. homepage: https://github.com/jappeace/esqueleto-textsearch@@ -11,8 +12,8 @@ category: Database build-type: Simple extra-source-files: README.md- Changelog.md-cabal-version: >=1.10+extra-doc-files:+ Changelog.md source-repository head type: git
src/Database/Esqueleto/TextSearch/Language.hs view
@@ -1,11 +1,14 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE GeneralisedNewtypeDeriving #-} module Database.Esqueleto.TextSearch.Language ( (@@.) , prefixAndQuery , toSearchTerm+ , toSearchTermWeighted , SearchTerm , to_tsvector , to_tsquery@@ -17,10 +20,11 @@ import Data.String (IsString) import Data.Text (Text)-import Database.Esqueleto (SqlExpr, Value, val) #if MIN_VERSION_esqueleto(3,5,0) import Database.Esqueleto.Internal.Internal (unsafeSqlBinOp, unsafeSqlFunction)+import Database.Esqueleto.Experimental (SqlExpr, Value, val) #else+import Database.Esqueleto (SqlExpr, Value, val) import Database.Esqueleto.Internal.Sql (unsafeSqlBinOp, unsafeSqlFunction) #endif import Database.Esqueleto.TextSearch.Types@@ -127,13 +131,14 @@ prefixAndQueryLang :: RegConfig -> SearchTerm -> SqlExpr (Value (TsQuery Lexemes)) prefixAndQueryLang language (SearchTerm ts) = foldr1 tsquery_and- $ map (to_tsquery (val language) . val . Word Prefix []) $ toList ts+ $ map (to_tsquery (val language) . val) $ toList ts -- | A valid search term. -- created with 'toSearchTerm'.-newtype SearchTerm = SearchTerm { unQuery :: NonEmpty Text }- deriving (Show)+newtype SearchTerm = SearchTerm { unQuery :: NonEmpty (TsQuery Words) }+ deriving stock Show+ deriving newtype Semigroup -- | Constructs a valid search query, removes a bunch of illegal -- characters and splits the terms for better results.@@ -142,7 +147,17 @@ -- using a search term is optional, but it's probably what you want. -- all underlying primitives are exposed. toSearchTerm :: Text -> Maybe SearchTerm-toSearchTerm q = SearchTerm <$> nonEmpty qs+toSearchTerm = toSearchTermWeighted []++-- | create a search term with some weight, this allows tweaking of priority of certain terms.+-- for example if you want to do some post processing on user input.+-- where they insist on typing dashes,+-- you split on the dash and concatinate the search term with lower+-- priority splitted strings on the dash.+-- so the full string is high priority, substrings lower.+-- use the semigroup instance on search term to combine these.+toSearchTermWeighted :: [Weight] -> Text -> Maybe SearchTerm+toSearchTermWeighted weights q = SearchTerm . fmap (Word Prefix weights) <$> nonEmpty qs -- We disallow whitespace, \ and ' for the sake of producing a Text -- that can fit postgresql's requirements for to_tsquery's text -- argument. Note that this is not done nor needed for security reasons
src/Database/Esqueleto/TextSearch/Types.hs view
@@ -126,6 +126,7 @@ sqlType = const (SqlOther "float4[4]") data QueryType = Words | Lexemes+ deriving Show type Lexemes = 'Lexemes type Words = 'Words
test/Database/Esqueleto/TextSearchSpec.hs view
@@ -96,22 +96,22 @@ spec = do describe "TsVector" $ do it "can be persisted and retrieved" $ run $ do- let article = Article "some title" "some content" def+ let article = Article "some title" "some content" defaultTsVector arId <- insert article update $ \a -> do set a [ArticleTextsearch =. to_etsvector (a^.ArticleContent)] ret <- fromJust <$> get arId- liftIO $ articleTextsearch ret /= def `shouldBe` True+ liftIO $ articleTextsearch ret /= defaultTsVector `shouldBe` True it "can be persisted and retrieved with weight" $ run $ do- let article = Article "some title" "some content" def+ let article = Article "some title" "some content" defaultTsVector arId <- insert article update $ \a -> do set a [ ArticleTextsearch =. setweight (to_etsvector (a^.ArticleContent)) (val Highest) ] ret <- fromJust <$> get arId- liftIO $ articleTextsearch ret /= def `shouldBe` True+ liftIO $ articleTextsearch ret /= defaultTsVector `shouldBe` True describe "Weight" $ do it "can be persisted and retrieved" $ run $ do@@ -256,7 +256,7 @@ describe "@@" $ do it "works as expected" $ run $ do- let article = Article "some title" "some content" def+ let article = Article "some title" "some content" defaultTsVector arId <- insert article update $ \a -> do set a [ArticleTextsearch =. to_etsvector (a^.ArticleContent)]@@ -279,7 +279,7 @@ content = "content" :: Text query = to_tsquery (val "english") (val "content") norm = val []- ret <- select $ return $ ts_rank_cd (val def) vector query norm+ ret <- select $ return $ ts_rank_cd (val defaultWeights) vector query norm liftIO $ map unValue ret `shouldBe` [0.1] describe "ts_rank" $ do@@ -288,7 +288,7 @@ content = "content" :: Text query = to_tsquery (val "english") (val "content") norm = val []- ret <- select $ return $ ts_rank (val def) vector query norm+ ret <- select $ return $ ts_rank (val defaultWeights) vector query norm liftIO $ unValue (head ret) `shouldBe` 6.079271e-2 describe "NormalizationOption" $ do