esqueleto-textsearch 1.0.0.3 → 1.1.0
raw patch · 6 files changed
+79/−14 lines, 6 filesdep −data-defaultdep ~textPVP ok
version bump matches the API change (PVP)
Dependencies removed: data-default
Dependency ranges changed: text
API changes (from Hackage documentation)
- Database.Esqueleto.TextSearch.Types: def :: Default a => a
- Database.Esqueleto.TextSearch.Types: instance Data.Default.Class.Default Database.Esqueleto.TextSearch.Types.TsVector
- Database.Esqueleto.TextSearch.Types: instance Data.Default.Class.Default Database.Esqueleto.TextSearch.Types.Weights
+ Database.Esqueleto.TextSearch.Language: data SearchTerm
+ Database.Esqueleto.TextSearch.Language: instance GHC.Show.Show Database.Esqueleto.TextSearch.Language.SearchTerm
+ Database.Esqueleto.TextSearch.Language: prefixAndQuery :: SearchTerm -> SqlExpr (Value (TsQuery Lexemes))
+ Database.Esqueleto.TextSearch.Language: toSearchTerm :: Text -> Maybe SearchTerm
+ Database.Esqueleto.TextSearch.Types: defaultTsVector :: TsVector
+ Database.Esqueleto.TextSearch.Types: defaultWeights :: Weights
Files
- Changelog.md +9/−0
- README.md +6/−1
- esqueleto-textsearch.cabal +5/−6
- src/Database/Esqueleto/TextSearch.hs +3/−0
- src/Database/Esqueleto/TextSearch/Language.hs +45/−1
- src/Database/Esqueleto/TextSearch/Types.hs +11/−6
Changelog.md view
@@ -1,5 +1,14 @@ # Change log for esqueleto text search +## Version 1.1.0+++ Removed weird patch numbering++ added a saner api for doing actual search.++ improve documentation.++ bump text++ drop data default dependency.++ ## Version 1.0.0.3 initial version, uploaded from:
README.md view
@@ -1,2 +1,7 @@ # esqueleto-fts-PostgreSQL full text search for Esqueleto+Haskell bindings for postgres full text search in esqueleto.+for a good explenation see https://rachbelaid.com/postgres-full-text-search-is-good-enough/++you can turn postgres into a database that is similar+in performance for search as elastic search, without having +to deal with elastic search.
esqueleto-textsearch.cabal view
@@ -1,12 +1,12 @@ name: esqueleto-textsearch-version: 1.0.0.3+version: 1.1.0 synopsis: PostgreSQL full text search for Esqueleto description: PostgreSQL text search functions for Esqueleto.-homepage: https://github.com/SupercedeTech/esqueleto-textsearch-ii+homepage: https://github.com/jappeace/esqueleto-textsearch license: MIT license-file: LICENSE author: Alberto Valverde González-maintainer: info@supercede.com+maintainer: jappie, hi@jappie.me copyright: 2015 Alberto Valverde González category: Database build-type: Simple@@ -16,7 +16,7 @@ source-repository head type: git- location: https://github.com/SupercedeTech/esqueleto-textsearch-ii + location: https://github.com/jappeace/esqueleto-textsearch library exposed-modules:@@ -26,12 +26,11 @@ build-depends: base >=4.9 && <5- , data-default <0.8 , esqueleto >=3.2 && < 3.6 , parsec <3.2 , persistent >=2.8.2 && <2.15 , persistent-postgresql >=2.10 && <2.15- , text >=1.2 && <2.1+ , text >=1.2 && <2.2 hs-source-dirs: src default-language: Haskell2010
src/Database/Esqueleto/TextSearch.hs view
@@ -1,3 +1,6 @@++-- | Haskell bindings for postgres full text search.+-- for a good explenation see https://rachbelaid.com/postgres-full-text-search-is-good-enough/ module Database.Esqueleto.TextSearch ( module Database.Esqueleto.TextSearch.Language , module Database.Esqueleto.TextSearch.Types
src/Database/Esqueleto/TextSearch/Language.hs view
@@ -4,6 +4,9 @@ module Database.Esqueleto.TextSearch.Language ( (@@.)+ , prefixAndQuery+ , toSearchTerm+ , SearchTerm , to_tsvector , to_tsquery , plainto_tsquery@@ -14,13 +17,15 @@ import Data.String (IsString) import Data.Text (Text)-import Database.Esqueleto (SqlExpr, Value)+import Database.Esqueleto (SqlExpr, Value, val) #if MIN_VERSION_esqueleto(3,5,0) import Database.Esqueleto.Internal.Internal (unsafeSqlBinOp, unsafeSqlFunction) #else import Database.Esqueleto.Internal.Sql (unsafeSqlBinOp, unsafeSqlFunction) #endif import Database.Esqueleto.TextSearch.Types+import qualified Data.Text as T+import Data.List.NonEmpty(nonEmpty, NonEmpty, toList) (@@.) :: SqlExpr (Value TsVector)@@ -68,3 +73,42 @@ -> SqlExpr (Value Weight) -> SqlExpr (Value TsVector) setweight a b = unsafeSqlFunction "setweight" (a, b)++-- | (&&) for tsquery. This function would be called (&&.) but+-- Esqueleto's (&&.) confines that fn to sql boolean expressions.+-- x::tsquery && y::tsquery == to_tsquery('x & y')+tsquery_and :: SqlExpr (Value (TsQuery Lexemes))+ -> SqlExpr (Value (TsQuery Lexemes))+ -> SqlExpr (Value (TsQuery Lexemes))+tsquery_and = unsafeSqlBinOp "&&"++-- | format the query into lexemes+-- the result can be used in '@@.' for example:+--+-- @+-- where_ $ (index ^. UnitSearchIndexDocument) @@. prefixAndQuery query+-- @+--+prefixAndQuery :: SearchTerm -> SqlExpr (Value (TsQuery Lexemes))+prefixAndQuery = prefixAndQueryLang "english"++-- | specify a language to be used with the query.+prefixAndQueryLang :: RegConfig -> SearchTerm -> SqlExpr (Value (TsQuery Lexemes))+prefixAndQueryLang language (SearchTerm ts) =+ foldr1 tsquery_and+ $ map (to_tsquery (val language) . val . Word Prefix []) $ toList ts+++-- | A valid search term+newtype SearchTerm = SearchTerm { unQuery :: NonEmpty Text }+ deriving (Show)++-- | constructs a valid search query, removes a bunch of illegal+-- characters and splits the terms for better results+toSearchTerm :: Text -> Maybe SearchTerm+toSearchTerm q = SearchTerm <$> 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+ where qs = filter (not . T.null) $ T.words+ $ T.filter (`notElem` ['\\', '\'']) $ T.strip q
src/Database/Esqueleto/TextSearch/Types.hs view
@@ -11,15 +11,16 @@ , Words , Lexemes , TsVector+ , defaultTsVector , RegConfig , NormalizationOption (..) , Weight (..)+ , defaultWeights , Weights (..) , Position (..) , word , queryToText , textToQuery- , def ) where import Control.Applicative (pure, many, optional, (<$>), (*>), (<*), (<|>))@@ -33,7 +34,6 @@ ParseError, runParser, char, eof, between, choice, spaces, satisfy, many1) import qualified Text.Parsec.Expr as P -import Data.Default (Default(def)) import Data.Text (Text, singleton) import Data.Text.Encoding (decodeUtf8, encodeUtf8) import Data.Text.Lazy (toStrict)@@ -101,8 +101,8 @@ , aWeight :: !Double } deriving (Eq, Show) -instance Default Weights where- def = Weights 0.1 0.2 0.4 1.0+defaultWeights :: Weights+defaultWeights = Weights 0.1 0.2 0.4 1.0 instance PersistField Weights where toPersistValue (Weights d c b a)@@ -215,8 +215,8 @@ newtype TsVector = TsVector {unTsVector::Text} deriving (Eq, Show, IsString) -instance Default TsVector where- def = TsVector ""+defaultTsVector :: TsVector+defaultTsVector = TsVector "" instance PersistField TsVector where toPersistValue = PersistDbSpecific . encodeUtf8 . unTsVector@@ -228,6 +228,11 @@ sqlType = const (SqlOther "tsvector") ++-- | regconfig is the object identifier type which represents the+-- text search configuration in Postgres: http://www.postgresql.org/docs/9.3/static/datatype-oid.html+--+-- this could for example be a language or simple. newtype RegConfig = RegConfig {unRegConfig::Text} deriving (Eq, Show, IsString) instance PersistField RegConfig where