Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Haskell bindings for postgres full text search. for a good explenation see https://rachbelaid.com/postgres-full-text-search-is-good-enough/
see the readme for a full tutorial.
Synopsis
- toSearchTerm :: Text -> Maybe (NonEmpty (TsQuery Words))
- to_tsquery_en :: SqlExpr (Value (TsQuery Words)) -> SqlExpr (Value (TsQuery Lexemes))
- andWords :: NonEmpty (TsQuery Words) -> TsQuery Words
- orWords :: NonEmpty (TsQuery Words) -> TsQuery Words
- (@@.) :: SqlExpr (Value TsVector) -> SqlExpr (Value (TsQuery Lexemes)) -> SqlExpr (Value Bool)
- ts_rank :: SqlExpr (Value Weights) -> SqlExpr (Value TsVector) -> SqlExpr (Value (TsQuery Lexemes)) -> SqlExpr (Value [NormalizationOption]) -> SqlExpr (Value Double)
- data NormalizationOption
- module Database.Esqueleto.TextSearch.Language
- module Database.Esqueleto.TextSearch.Types
Documentation
toSearchTerm :: Text -> Maybe (NonEmpty (TsQuery Words)) Source #
Constructs a valid search query, removes a bunch of illegal characters and splits the terms for better results. Also checks if there is anything in the search term.
using a search term is optional, but it's probably what you want. all underlying primitives are exposed.
to_tsquery_en :: SqlExpr (Value (TsQuery Words)) -> SqlExpr (Value (TsQuery Lexemes)) Source #
to_tsquery
defaulted to english
searchCompany :: SqlExpr (Entity CompanySearchIndex) -> Text -> SqlQuery () searchCompany company term = do let query = to_tsquery_en $ val $ andWords $ prefixAndQuery term where_ $ (company ^. CompanySearchIndexDocument) @@. query
andWords :: NonEmpty (TsQuery Words) -> TsQuery Words Source #
same as prefixAndQuery
without wrapping to_tsquery
.
orWords :: NonEmpty (TsQuery Words) -> TsQuery Words Source #
same as prefixOrQuery
without wrapping to_tsquery
.
:: SqlExpr (Value TsVector) | the document to search in |
-> SqlExpr (Value (TsQuery Lexemes)) | the query made by |
-> SqlExpr (Value Bool) |
Apply some query to a tsvector document for example:
searchCompany :: SqlExpr (Entity CompanySearchIndex) -> NonEmpty (TsQuery Words) -> SqlQuery () searchCompany company term = do let query = prefixAndQuery term norm = val [] where_ $ (company ^. CompanySearchIndexDocument) @@. query
:: SqlExpr (Value Weights) | relative weighting of a b c and d, see |
-> SqlExpr (Value TsVector) | the document to search in |
-> SqlExpr (Value (TsQuery Lexemes)) | the query made by |
-> SqlExpr (Value [NormalizationOption]) | normalization option to indicate how to deal with document length |
-> SqlExpr (Value Double) |
Organize search result by weights. This allows you to put better matching results higher. for example:
searchCompany :: SqlExpr (Entity CompanySearchIndex) -> NonEmpty (TsQuery Words) -> SqlQuery () searchCompany company term = do let query = prefixAndQuery term norm = val [] where_ $ (company ^. CompanySearchIndexDocument) @@. query orderBy [desc (ts_rank (val defaultWeights) (company ^. CompanySearchIndexDocument) query norm)]
data NormalizationOption Source #
ranking functions take an integer normalization option that specifies whether and how a document's length should impact its rank. The integer option controls several behaviors, so it is a bit mask: you can specify one or more behaviors using | (for example, 2|4). https://www.postgresql.org/docs/current/textsearch-controls.html#TEXTSEARCH-RANKING
NormNone | 0 (the default) ignores the document length |
Norm1LogLength | 1 divides the rank by 1 + the logarithm of the document length |
NormLength | 2 divides the rank by the document length |
NormMeanHarmDist | 4 divides the rank by the mean harmonic distance between extents (this is implemented only by ts_rank_cd) |
NormUniqueWords | 8 divides the rank by the number of unique words in document |
Norm1LogUniqueWords | 16 divides the rank by 1 + the logarithm of the number of unique words in document |
Norm1Self | 32 divides the rank by itself + 1 |