@@ -1,3 +1,13 @@
2+Module: Squeal.PostgreSQL.Expression.TextSearch
3+Description: Text search expressions
4+Copyright: (c) Eitan Chatav, 2019
5+Maintainer: eitan@morphism.tech
6+Stability: experimental
8+Text Search Functions and Operators
1{-# LANGUAGE
11{-# LANGUAGE
3 , OverloadedStrings
13 , OverloadedStrings
@@ -32,75 +42,116 @@ import Squeal.PostgreSQL.Expression
32import Squeal.PostgreSQL.List
42import Squeal.PostgreSQL.List
33import Squeal.PostgreSQL.Schema
43import Squeal.PostgreSQL.Schema
45+-- | `Squeal.PostgreSQL.Expression.Type.tsvector` matches tsquery ?
35(@@) :: Operator (null 'PGtsvector) (null 'PGtsquery) ('Null 'PGbool)
46(@@) :: Operator (null 'PGtsvector) (null 'PGtsquery) ('Null 'PGbool)
36(@@) = unsafeBinaryOp "@@"
47(@@) = unsafeBinaryOp "@@"
49+-- | AND `Squeal.PostgreSQL.Expression.Type.tsquery`s together
38(.&) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
50(.&) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
39(.&) = unsafeBinaryOp "&&"
51(.&) = unsafeBinaryOp "&&"
53+-- | OR `Squeal.PostgreSQL.Expression.Type.tsquery`s together
41(.|) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
54(.|) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
42(.|) = unsafeBinaryOp "||"
55(.|) = unsafeBinaryOp "||"
57+-- | negate a `Squeal.PostgreSQL.Expression.Type.tsquery`
44(.!) :: null 'PGtsquery :--> null 'PGtsquery
58(.!) :: null 'PGtsquery :--> null 'PGtsquery
45(.!) = unsafeUnaryOpL "!!"
59(.!) = unsafeUnaryOpL "!!"
61+-- | `Squeal.PostgreSQL.Expression.Type.tsquery` followed by
62+-- `Squeal.PostgreSQL.Expression.Type.tsquery`
47(<->) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
63(<->) :: Operator (null 'PGtsquery) (null 'PGtsquery) (null 'PGtsquery)
48(<->) = unsafeBinaryOp "<->"
64(<->) = unsafeBinaryOp "<->"
66+-- | convert array of lexemes to `Squeal.PostgreSQL.Expression.Type.tsvector`
50arrayToTSvector
67arrayToTSvector
51 :: null ('PGvararray ('NotNull 'PGtext))
68 :: null ('PGvararray ('NotNull 'PGtext))
52 :--> null 'PGtsvector
69 :--> null 'PGtsvector
53arrayToTSvector = unsafeFunction "array_to_tsvector"
70arrayToTSvector = unsafeFunction "array_to_tsvector"
72+-- | number of lexemes in `Squeal.PostgreSQL.Expression.Type.tsvector`
55tsvectorLength :: null 'PGtsvector :--> null 'PGint4
73tsvectorLength :: null 'PGtsvector :--> null 'PGint4
56tsvectorLength = unsafeFunction "length"
74tsvectorLength = unsafeFunction "length"
76+-- | number of lexemes plus operators in `Squeal.PostgreSQL.Expression.Type.tsquery`
58numnode :: null 'PGtsquery :--> null 'PGint4
77numnode :: null 'PGtsquery :--> null 'PGint4
59numnode = unsafeFunction "numnode"
78numnode = unsafeFunction "numnode"
80+-- | produce `Squeal.PostgreSQL.Expression.Type.tsquery` ignoring punctuation
61plainToTSquery :: null 'PGtext :--> null 'PGtsquery
81plainToTSquery :: null 'PGtext :--> null 'PGtsquery
62plainToTSquery = unsafeFunction "plainto_tsquery"
82plainToTSquery = unsafeFunction "plainto_tsquery"
84+-- | produce `Squeal.PostgreSQL.Expression.Type.tsquery` that searches for a phrase,
85+-- ignoring punctuation
64phraseToTSquery :: null 'PGtext :--> null 'PGtsquery
86phraseToTSquery :: null 'PGtext :--> null 'PGtsquery
65phraseToTSquery = unsafeFunction "phraseto_tsquery"
87phraseToTSquery = unsafeFunction "phraseto_tsquery"
89+-- | produce `Squeal.PostgreSQL.Expression.Type.tsquery` from a web search style query
67websearchToTSquery :: null 'PGtext :--> null 'PGtsquery
90websearchToTSquery :: null 'PGtext :--> null 'PGtsquery
68websearchToTSquery = unsafeFunction "websearch_to_tsquery"
91websearchToTSquery = unsafeFunction "websearch_to_tsquery"
93+-- | get indexable part of a `Squeal.PostgreSQL.Expression.Type.tsquery`
70queryTree :: null 'PGtsquery :--> null 'PGtext
94queryTree :: null 'PGtsquery :--> null 'PGtext
71queryTree = unsafeFunction "query_tree"
95queryTree = unsafeFunction "query_tree"
97+-- | normalize words and convert to `Squeal.PostgreSQL.Expression.Type.tsquery`
73toTSquery :: null 'PGtext :--> null 'PGtsquery
98toTSquery :: null 'PGtext :--> null 'PGtsquery
74toTSquery = unsafeFunction "to_tsquery"
99toTSquery = unsafeFunction "to_tsquery"
101+-- | reduce document text to `Squeal.PostgreSQL.Expression.Type.tsvector`
76toTSvector
102toTSvector
77 :: ty `In` '[ 'PGtext, 'PGjson, 'PGjsonb]
103 :: ty `In` '[ 'PGtext, 'PGjson, 'PGjsonb]
78 => null ty :--> null 'PGtsvector
104 => null ty :--> null 'PGtsvector
79toTSvector = unsafeFunction "to_tsvector"
105toTSvector = unsafeFunction "to_tsvector"
107+-- | assign weight to each element of `Squeal.PostgreSQL.Expression.Type.tsvector`
82 :: FunctionN '[null 'PGtsvector, null ('PGchar 1)] (null 'PGtsvector)
109 :: FunctionN '[null 'PGtsvector, null ('PGchar 1)] (null 'PGtsvector)
83setWeight = unsafeFunctionN "set_weight"
110setWeight = unsafeFunctionN "set_weight"
112+-- | remove positions and weights from `Squeal.PostgreSQL.Expression.Type.tsvector`
85strip :: null 'PGtsvector :--> null 'PGtsvector
113strip :: null 'PGtsvector :--> null 'PGtsvector
86strip = unsafeFunction "strip"
114strip = unsafeFunction "strip"
116+-- | @jsonToTSvector (document *: filter)@
117+-- reduce each value in the document, specified by filter to a `Squeal.PostgreSQL.Expression.Type.tsvector`,
118+-- and then concatenate those in document order to produce a single `Squeal.PostgreSQL.Expression.Type.tsvector`.
119+-- filter is a `Squeal.PostgreSQL.Expression.Type.json` array, that enumerates what kind of elements
120+-- need to be included into the resulting `Squeal.PostgreSQL.Expression.Type.tsvector`.
121+-- Possible values for filter are "string" (to include all string values),
122+-- "numeric" (to include all numeric values in the string format),
123+-- "boolean" (to include all Boolean values in the string format "true"/"false"),
124+-- "key" (to include all keys) or "all" (to include all above).
125+-- These values can be combined together to include, e.g. all string and numeric values.
88jsonToTSvector :: FunctionN '[null 'PGjson, null 'PGjson] (null 'PGtsvector)
126jsonToTSvector :: FunctionN '[null 'PGjson, null 'PGjson] (null 'PGtsvector)
89jsonToTSvector = unsafeFunctionN "json_to_tsvector"
127jsonToTSvector = unsafeFunctionN "json_to_tsvector"
129+-- | @jsonbToTSvector (document *: filter)@
130+-- reduce each value in the document, specified by filter to a `Squeal.PostgreSQL.Expression.Type.tsvector`,
131+-- and then concatenate those in document order to produce a single `Squeal.PostgreSQL.Expression.Type.tsvector`.
132+-- filter is a `Squeal.PostgreSQL.Expression.Type.jsonb` array, that enumerates what kind of elements
133+-- need to be included into the resulting `Squeal.PostgreSQL.Expression.Type.tsvector`.
134+-- Possible values for filter are "string" (to include all string values),
135+-- "numeric" (to include all numeric values in the string format),
136+-- "boolean" (to include all Boolean values in the string format "true"/"false"),
137+-- "key" (to include all keys) or "all" (to include all above).
138+-- These values can be combined together to include, e.g. all string and numeric values.
91jsonbToTSvector :: FunctionN '[null 'PGjsonb, null 'PGjsonb] (null 'PGtsvector)
139jsonbToTSvector :: FunctionN '[null 'PGjsonb, null 'PGjsonb] (null 'PGtsvector)
92jsonbToTSvector = unsafeFunctionN "jsonb_to_tsvector"
140jsonbToTSvector = unsafeFunctionN "jsonb_to_tsvector"
142+-- | remove given lexeme from `Squeal.PostgreSQL.Expression.Type.tsvector`
94tsDelete :: FunctionN
143tsDelete :: FunctionN
95 '[null 'PGtsvector, null ('PGvararray ('NotNull 'PGtext))]
144 '[null 'PGtsvector, null ('PGvararray ('NotNull 'PGtext))]
96 (null 'PGtsvector)
145 (null 'PGtsvector)
97tsDelete = unsafeFunctionN "ts_delete"
146tsDelete = unsafeFunctionN "ts_delete"
148+-- | select only elements with given weights from `Squeal.PostgreSQL.Expression.Type.tsvector`
99tsFilter :: FunctionN
149tsFilter :: FunctionN
100 '[null 'PGtsvector, null ('PGvararray ('NotNull ('PGchar 1)))]
150 '[null 'PGtsvector, null ('PGvararray ('NotNull ('PGchar 1)))]
101 (null 'PGtsvector)
151 (null 'PGtsvector)
102tsFilter = unsafeFunctionN "ts_filter"
152tsFilter = unsafeFunctionN "ts_filter"
154+-- | display a `Squeal.PostgreSQL.Expression.Type.tsquery` match
104tsHeadline
155tsHeadline
105 :: document `In` '[ 'PGtext, 'PGjson, 'PGjsonb]
156 :: document `In` '[ 'PGtext, 'PGjson, 'PGjsonb]
106 => FunctionN '[null document, null 'PGtsquery] (null 'PGtext)
157 => FunctionN '[null document, null 'PGtsquery] (null 'PGtext)