textacular 5.2.0 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f9a8ae35c8edfea5bfe856e76614e2689eed90d4c41beee8a3271d1c3749586e
4
- data.tar.gz: 4e88e50d311cdbccf5aae2fc71f0c3ef2d0af22b284a5bb772c7687f311eb50b
3
+ metadata.gz: 39d5e5ef4609c4ad498b48ba0b2553b5e211cca399fd6df820db27b2925114d6
4
+ data.tar.gz: b0b76e8c23b19fa21f6e283c449099d5f6e8978d50e788f49a4cf80bc0664ea9
5
5
  SHA512:
6
- metadata.gz: 3c5f2531575beb68748b2c4458ea7e4e57af212f901270c45e08c359c7e535c81f18a785ea95c8679e8a0be688472f4d798bdec0329212cb89d52789fae785ff
7
- data.tar.gz: '0811ce97f78f63a84d41f4576115a07b8b2f01896e8a2d3d9904ff293cf8b95235919a83c66ab02c3f81a9c48653f798bb59d1cd7facb03d0d1f7ba24a97ef1c'
6
+ metadata.gz: 10bf037b59c151203a7f2dd962f7a4acb9abb0638f09fea15eba6f887c34635a28cc4aaa931df5a052da712b858e65080449a04e320b97e88e3b3cf997728182
7
+ data.tar.gz: e807fa30b6bf512418a06e97833fd48577be25aadd34d26d289df1005f83b0b9f1980e296ad818e05ee7a72357ae0453c2f8cd5eb2aed73161c85fac77e7ec05
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 5.3.0
6
+
7
+ * Add `#web_search` method to use Postgres' 11+ `websearch_to_tsquery`
8
+
5
9
  ## 5.2.0
6
10
 
7
11
  * Active Record 6.0 compatibility
data/README.md CHANGED
@@ -75,6 +75,20 @@ Game.advanced_search(title: 'Street|Fantasy')
75
75
  Game.advanced_search(system: '!PS2')
76
76
  ```
77
77
 
78
+ The `#web_search` method lets you use Postgres' 11+ `websearch_to_tsquery` function
79
+ supporting websearch like syntax:
80
+
81
+ - unquoted text: text not inside quote marks will be converted to terms separated by & operators, as if processed by plainto_tsquery.
82
+ - "quoted text": text inside quote marks will be converted to terms separated by <-> operators, as if processed by phraseto_tsquery.
83
+ - OR: logical or will be converted to the | operator.
84
+ - -: the logical not operator, converted to the the ! operator.
85
+
86
+ ```ruby
87
+ Game.web_search(title: '"Street Fantasy"')
88
+ Game.web_search(title: 'Street OR Fantasy')
89
+ Game.web_search(system: '-PS2')
90
+ ```
91
+
78
92
  Finally, the `#fuzzy_search` method lets you use Postgres's trigram search
79
93
  functionality.
80
94
 
@@ -37,6 +37,13 @@ module Textacular
37
37
  assemble_query(similarities, conditions, exclusive, rank_alias)
38
38
  end
39
39
 
40
+ def web_search(query = '', exclusive = true, rank_alias = nil)
41
+ exclusive, query = munge_exclusive_and_query(exclusive, query)
42
+ parsed_query_hash = parse_query_hash(query)
43
+ similarities, conditions = web_similarities_and_conditions(parsed_query_hash)
44
+ assemble_query(similarities, conditions, exclusive, rank_alias)
45
+ end
46
+
40
47
  private
41
48
 
42
49
  def munge_exclusive_and_query(exclusive, query)
@@ -120,6 +127,24 @@ module Textacular
120
127
  "(#{table_name}.#{column}::text % #{search_term})"
121
128
  end
122
129
 
130
+
131
+ def web_similarities_and_conditions(parsed_query_hash)
132
+ parsed_query_hash.inject([[], []]) do |(similarities, conditions), query_args|
133
+ similarities << web_similarity_string(*query_args)
134
+ conditions << web_condition_string(*query_args)
135
+
136
+ [similarities, conditions]
137
+ end
138
+ end
139
+
140
+ def web_similarity_string(table_name, column, search_term)
141
+ "COALESCE(ts_rank(to_tsvector(#{quoted_language}, #{table_name}.#{column}::text), websearch_to_tsquery(#{quoted_language}, #{search_term}::text)), 0)"
142
+ end
143
+
144
+ def web_condition_string(table_name, column, search_term)
145
+ "to_tsvector(#{quoted_language}, #{table_name}.#{column}::text) @@ websearch_to_tsquery(#{quoted_language}, #{search_term}::text)"
146
+ end
147
+
123
148
  def assemble_query(similarities, conditions, exclusive, rank_alias)
124
149
  rank_alias ||= 'rank' + rand(100000000000000000).to_s
125
150
  rank = connection.quote_column_name(rank_alias)
@@ -1,5 +1,5 @@
1
1
  module Textacular
2
- VERSION = '5.2.0'
2
+ VERSION = '5.3.0'
3
3
 
4
4
  def self.version
5
5
  VERSION
@@ -3,6 +3,6 @@ timeout: 5000
3
3
  host: localhost
4
4
  adapter: postgresql
5
5
  username: postgres
6
- password:
6
+ password: password
7
7
  database: textacular_test
8
- min_messages: ERROR
8
+ min_messages: ERROR
@@ -113,6 +113,14 @@ RSpec.describe "Searchable" do
113
113
  end
114
114
  end
115
115
 
116
+ describe "web search" do # Uses websearch_to_tsquery
117
+ ["hello \\", "tebow!" , "food &"].each do |search_term|
118
+ it "works with interesting term \"#{search_term}\"" do
119
+ expect(WebComicWithSearchableName.web_search(search_term)).to be_empty
120
+ end
121
+ end
122
+ end
123
+
116
124
  it "does fuzzy searching" do
117
125
  expect(
118
126
  WebComicWithSearchableName.fuzzy_search('Questio')
@@ -182,6 +190,14 @@ RSpec.describe "Searchable" do
182
190
  expect(
183
191
  WebComicWithSearchableNameAndAuthor.advanced_search("Tycho")
184
192
  ).to eq([penny_arcade])
193
+
194
+ expect(
195
+ WebComicWithSearchableNameAndAuthor.web_search("Penny")
196
+ ).to eq([penny_arcade])
197
+
198
+ expect(
199
+ WebComicWithSearchableNameAndAuthor.web_search("Tycho")
200
+ ).to eq([penny_arcade])
185
201
  end
186
202
 
187
203
  it "allows includes" do
@@ -218,6 +234,11 @@ RSpec.describe "Searchable" do
218
234
  search_result = WebComicWithSearchableNameAndAuthor.fuzzy_search('Questionable Content', true, 'my_rank')
219
235
  expect(search_result.first.attributes['my_rank']).to be_truthy
220
236
  end
237
+
238
+ it "is selected for web_search" do
239
+ search_result = WebComicWithSearchableNameAndAuthor.web_search('Questionable Content', true, 'my_rank')
240
+ expect(search_result.first.attributes['my_rank']).to be_truthy
241
+ end
221
242
  end
222
243
  end
223
244
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: textacular
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.0
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hamill
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2019-08-20 00:00:00.000000000 Z
14
+ date: 2020-06-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: pg
@@ -194,8 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  - !ruby/object:Gem::Version
195
195
  version: '0'
196
196
  requirements: []
197
- rubyforge_project:
198
- rubygems_version: 2.7.6
197
+ rubygems_version: 3.0.3
199
198
  signing_key:
200
199
  specification_version: 4
201
200
  summary: Textacular exposes full text search capabilities from PostgreSQL