pg-searchable 0.1.0 → 0.1.1

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: eaf2e17acef704a8fd936d20b9716dace4e9f4319a26f8d72e4f4a79fa515bde
4
- data.tar.gz: 8153451549b6fd494227f9c01377b0021241026188509778b2c3c430b087fa23
3
+ metadata.gz: 98b4ca6590b2d5fe5f93fd7d1aa02b4e0f893c8cfc599bffd25ef57d20eb5376
4
+ data.tar.gz: acdc3e6009e2e25583f88f5ec133df9a11948a2dda1d691162e2c5fa295f140d
5
5
  SHA512:
6
- metadata.gz: 70964f08dad60214f6d87f7f5f2497ce7118ad92d3ced7df0d79bcf31435bb45d9d269ff096e700d27c840df4b67383435b1ef96978e6d95429756d9d5ab6e99
7
- data.tar.gz: 4904c0b55c7c847f15a1f2b2097a684a577c3ae28d1380bd9fed5f290db3fc899e5b2e8fde7f26212e5a13921a1fb05228213e272683589ccf15e05718d157a3
6
+ metadata.gz: '0827967f7cfb2114b101d4f4125e7f28d26590f64bb26adde121053bb6b7515c7cb26ecc1deb9f73a21c242458f93ba31701ef199e73401c8f24d42e39089e41'
7
+ data.tar.gz: f2a20d8703128d5b0cb2af32e76eaedf2c7c6991ef425c5c9ab1de586105223b421fec90ff4deedf535383404a88b42602d055d3b5f42d02cd700f79f0644713
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 wyozi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -26,7 +26,7 @@ italian_pasta = Product.create!(name: "Italian pasta")
26
26
  rye_bread = Product.create!(name: "Rye bread")
27
27
  sushi = Product.create!(name: "Sour Sushi Itamae")
28
28
 
29
- # Basic options
29
+ # Basic usage
30
30
  bread_condition = Product.search_name("bread")
31
31
  expect(Product.where(bread_condition)).to match_array [sour_bread, rye_bread]
32
32
 
@@ -36,12 +36,20 @@ expect(Product.where(ita_condition)).to match_array [italian_pasta, sushi]
36
36
  nonsour_condition = Product.search_name("!sour")
37
37
  expect(Product.where(nonsour_condition)).to match_array [italian_pasta, rye_bread]
38
38
 
39
- # Composing searches
39
+ # Use Postgres built-in websearch
40
+ ws_condition = Product.search_name("sushi or pasta", websearch: true)
41
+ expect(Product.where(ws_condition)).to match_array [sushi, italian_pasta]
42
+
43
+ # Composing basic searches
40
44
  non_sour_breads = Product.where(bread_condition.and(nonsour_condition))
41
45
  expect(non_sour_breads).to match_array [rye_bread]
42
46
 
43
47
  nonsour_or_ita = Product.where(nonsour_condition.or(ita_condition))
44
48
  expect(nonsour_or_ita).to match_array [rye_bread, italian_pasta, sushi]
49
+
50
+ # Advanced options
51
+ raw_condition = Product.search_name("sushi | pasta", raw: true)
52
+ expect(Product.where(raw_condition)).to match_array [sushi, italian_pasta]
45
53
  ```
46
54
 
47
55
  ### Development
@@ -24,15 +24,21 @@ module PgSearchable
24
24
  Arel::Nodes.build_quoted(:simple)
25
25
  end
26
26
 
27
- def exec(column_name, query, raw:false, prefix:false)
27
+ def exec(column_name, query, raw:false, prefix:false, websearch:false)
28
28
  column_ref = "#{@model.quoted_table_name}.#{column_name}"
29
29
 
30
30
  vector = Arel::Nodes::NamedFunction.new(
31
31
  "to_tsvector",
32
32
  [dictionary, Arel.sql(column_ref)]
33
33
  )
34
+
35
+ opts = {
36
+ prefix: prefix,
37
+ raw: raw,
38
+ websearch: websearch
39
+ }
34
40
 
35
- query = Builder.tsquery(query, prefix: prefix, raw: raw)
41
+ query = Builder.tsquery(query, opts)
36
42
 
37
43
  condition = Arel::Nodes::Grouping.new(
38
44
  Arel::Nodes::InfixOperation.new("@@", vector, query)
@@ -54,6 +60,21 @@ module PgSearchable
54
60
  x # TODO
55
61
  end
56
62
 
63
+ def self.tsquery(query, opts)
64
+ query_terms = query.split(" ").compact
65
+ tsq =
66
+ if query.blank?
67
+ Arel::Nodes.build_quoted("")
68
+ elsif opts[:raw] || opts[:websearch]
69
+ Arel::Nodes.build_quoted(query)
70
+ else
71
+ tsquery_for_terms(query_terms, prefix: opts[:prefix])
72
+ end
73
+
74
+ fn_name = opts[:websearch] ? "websearch_to_tsquery" : "to_tsquery"
75
+ Arel::Nodes::NamedFunction.new(fn_name, [dictionary, tsq])
76
+ end
77
+
57
78
  def self.tsquery_for_terms(terms, prefix:)
58
79
  terms = terms.map { |term| tsquery_term(term, prefix: prefix) }
59
80
  terms.inject do |memo, term|
@@ -90,20 +111,6 @@ module PgSearchable
90
111
  Arel::Nodes::InfixOperation.new("||", memo, Arel::Nodes.build_quoted(term))
91
112
  end
92
113
  end
93
-
94
- def self.tsquery(query, prefix:, raw:)
95
- query_terms = query.split(" ").compact
96
- tsq =
97
- if query.blank?
98
- Arel::Nodes.build_quoted("")
99
- elsif raw
100
- Arel::Nodes.build_quoted(query)
101
- else
102
- tsquery_for_terms(query_terms, prefix: prefix)
103
- end
104
-
105
- Arel::Nodes::NamedFunction.new("to_tsquery", [dictionary, tsq])
106
- end
107
114
  end
108
115
  end
109
116
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgSearchable
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
@@ -4,6 +4,8 @@ Gem::Specification.new do |gem|
4
4
  gem.authors = ["wyozi"]
5
5
  gem.name = "pg-searchable"
6
6
  gem.summary = "Simple full-text searching for Rails"
7
+ gem.homepage = "https://github.com/wyozi/pg-searchable"
8
+ gem.licenses = ["MIT"]
7
9
 
8
10
  gem.files = `git ls-files | grep -Ev '^(spec)'`.split("\n")
9
11
  gem.version = PgSearchable::VERSION
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-searchable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - wyozi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-21 00:00:00.000000000 Z
11
+ date: 2020-03-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -22,6 +22,7 @@ files:
22
22
  - ".rspec"
23
23
  - Gemfile
24
24
  - Gemfile.lock
25
+ - LICENSE
25
26
  - README.md
26
27
  - bin/local-docker-testing
27
28
  - config.ru
@@ -30,8 +31,9 @@ files:
30
31
  - lib/pg-searchable/model.rb
31
32
  - lib/pg-searchable/version.rb
32
33
  - pg-searchable.gemspec
33
- homepage:
34
- licenses: []
34
+ homepage: https://github.com/wyozi/pg-searchable
35
+ licenses:
36
+ - MIT
35
37
  metadata: {}
36
38
  post_install_message:
37
39
  rdoc_options: []