Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tsQuery <-> tsQuery -> tsQuery #551

Merged
merged 4 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ trait PgSearchExtensions extends JdbcTypesComponent { driver: PostgresProfile =>
val Or = new SqlOperator("||")
val Negate = new SqlOperator("!!")
val Contains = new SqlOperator("@>")
val Phrase = new SqlOperator("<->")

val GetCurrTsConfig = new SqlFunction("get_current_ts_config")
val ToTsQuery = new SqlFunction("to_tsquery")
Expand Down Expand Up @@ -138,6 +139,8 @@ trait PgSearchExtensions extends JdbcTypesComponent { driver: PostgresProfile =>
def !! = SearchLibrary.Negate.column[TQ](c.toNode)
def @>[P2, R](e: Rep[P2])(implicit om: o#arg[TQ, P2]#to[Boolean, R]) =
om.column(SearchLibrary.Contains, c.toNode, e.toNode)
def <->[P2, R](e: Rep[P2])(implicit om: o#arg[TQ, P2]#to[TQ, R]) =
om.column(SearchLibrary.Phrase, c.toNode, e.toNode)

def numNode = SearchLibrary.NumNode.column[Int](c.toNode)
def queryTree = SearchLibrary.QueryTree.column[String](c.toNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ Supported Search Oper/Functions
-------------------------------

| Slick Oper/Function | PG Oper/Function | Description | Example | Result |
| ------------------- | ---------------- | -------------------------------- | -------------------------------------- | ----------- |
|---------------------|------------------| -------------------------------- | -------------------------------------- | ----------- |
| tsQuery | to_tsquery | normalize words and convert to tsquery | to_tsquery('english', 'The & Fat & Rats') | 'fat' & 'rat' |
| tsVector | to_tsvector | reduce document text to tsvector | to_tsvector('english', 'The Fat Rats') | 'fat':2 'rat':3 |
| @@ | @@ | tsvector matches tsquery ? | to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat') | t |
| @+ | &#124;&#124; | concatenate tsvectors | 'a:1 b:2'::tsvector &#124;&#124; 'c:1 d:2 b:3'::tsvector | 'a':1 'b':2,5 'c':3 'd':4 |
| @& | && | AND tsquerys together | 'fat &#124; rat'::tsquery && 'cat'::tsquery | ( 'fat' &#124; 'rat' ) & 'cat' |
| <-> | <-> | Constructs a phrase query, which matches if the two input queries match at successive lexemes. | to_tsquery('fat') <-> to_tsquery('rat') | 'fat' <-> 'rat' |
| @&#124; | &#124;&#124; | OR tsquerys together | 'fat &#124; rat'::tsquery &#124;&#124; 'cat'::tsquery | ( 'fat' &#124; 'rat' ) &#124; 'cat' |
| !! | !! | negate a tsquery | !! 'cat'::tsquery | !'cat' |
| @> | @> | tsquery contains another ? | 'cat'::tsquery @> 'cat & rat'::tsquery | f |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class PgSearchSupportSuite extends AnyFunSuite with PostgresContainer {

val testRec1 = TestBean(33L, "fat cat ate rat", TsVector("'ate' 'cat' 'fat' 'rat'"), "")
val testRec2 = TestBean(37L, "cat", TsVector("'ca'"), "fat")
val testRec3 = TestBean(38L, "mike rose", TsVector("'mike' 'rose'"), "")
val testRec4 = TestBean(39L, "mike and rose", TsVector("'mike' 'and' 'rose'"), "")

test("Text search Lifted support") {
Await.result(db.run(
DBIO.seq(
Tests.schema create,
///
Tests forceInsertAll List(testRec1, testRec2)
Tests forceInsertAll List(testRec1, testRec2, testRec3, testRec4)
).andThen(
DBIO.seq(
// get_current_ts_config
Expand Down Expand Up @@ -62,14 +64,18 @@ class PgSearchSupportSuite extends AnyFunSuite with PostgresContainer {
),
// @>
Tests.filter(r => plainToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
r => assert(List(testRec1) === r)
r => assert(List(testRec1, testRec3, testRec4).map(_.id) === r.map(_.id))
),
Tests.filter(r => phraseToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
r => assert(List(testRec1) === r)
),
Tests.filter(r => webSearchToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
r => assert(List(testRec1) === r)
),
// <->
Tests.filter(r => toTsVector(r.text, Some("english")) @@ (tsQuery("mike".bind) <-> tsQuery("rose".bind))).sortBy(_.id).to[List].result.map(
r => assert(List(testRec1) === r)
),
// length
Tests.filter(_.id === 33L).map(r => toTsVector(r.text).length).result.head.map(
r => assert(4 === r)
Expand Down