search_query_parser 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +4 -4
- data/lib/search_query_parser.rb +4 -0
- data/lib/search_query_parser/grammar.rb +4 -2
- data/lib/search_query_parser/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a22c4a198ee6692de837e0af3252257436bf5c7
|
4
|
+
data.tar.gz: 3d761b1c78bf1c2be79c1e61f6e4fb4c1c287e15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d34b521cd22c1c4a415bc8d2d91999bf3ed92a0d544cf2242610172e599684f88483c8985729d142e6ba74b7c0bc93cc2e799b51208d5f63a90368eeb60e9ee
|
7
|
+
data.tar.gz: db7665fa7c4fbd470411afe28cc17426153bc4f366cd813b1d4ea6b1a35acc1b14740d257eb13ded83d6ced1e312fbd26bae9aecb7809b08a554bdcfd2dbcf1a
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Search Query Parser
|
2
2
|
|
3
3
|
This gem allows you to create PostgreSQL 9.6 compatible ts_query expressions from simple-to-use "query language".
|
4
4
|
|
@@ -9,7 +9,7 @@ See for reference:
|
|
9
9
|
|
10
10
|
Example:
|
11
11
|
|
12
|
-
```
|
12
|
+
```ruby
|
13
13
|
require 'search_query_parser'
|
14
14
|
|
15
15
|
q = SearchQueryParser.to_ts_query("
|
@@ -34,7 +34,7 @@ When you use PostgreSQL full-text search you want your users to be able to utili
|
|
34
34
|
|
35
35
|
Unfortunately, you can't use Postgres' `plain_tosquery` ([link](https://www.postgresql.org/docs/9.6/static/functions-textsearch.html)) function for that, which would be the closest match to our goal. If you give the string `cates & !dogs` to `plainto_tsquery`, the result will be:
|
36
36
|
|
37
|
-
```
|
37
|
+
```sql
|
38
38
|
=> select plainto_tsquery('cats & !dogs');
|
39
39
|
plainto_tsquery
|
40
40
|
-----------------
|
@@ -51,7 +51,7 @@ Use `SearchQueryParser.to_ts_query(q)` to produce PostgreSQL-compatible expressi
|
|
51
51
|
|
52
52
|
Use it in your Rails finders for models with `tsdata` columns like that:
|
53
53
|
|
54
|
-
```
|
54
|
+
```ruby
|
55
55
|
q = SearchQueryParser.to_ts_query('кошки & !собаки', 'russian')
|
56
56
|
Document.where("tsdata @@ #{q}")
|
57
57
|
```
|
data/lib/search_query_parser.rb
CHANGED
@@ -4,6 +4,10 @@ require "search_query_parser/interpreter"
|
|
4
4
|
module SearchQueryParser
|
5
5
|
@interpreter = SearchQueryParser::Interpreter.new
|
6
6
|
|
7
|
+
def self.to_string(str)
|
8
|
+
@interpreter.parse(str).to_s
|
9
|
+
end
|
10
|
+
|
7
11
|
def self.to_ts_query(str, language = 'english', prefix = true)
|
8
12
|
language = language.gsub(/[^a-zA-Z\-]/, '')
|
9
13
|
@interpreter.parse(str).reduce do |op, x, y|
|
@@ -7,8 +7,10 @@ module SearchQueryParser
|
|
7
7
|
gsub(/[^[:alnum:]\-()&|!]+/, ' ').
|
8
8
|
gsub(/([^[:alnum:]]|^)\-/, '\1').
|
9
9
|
gsub(/\-([^[:alnum:]]|$)/, '\1').
|
10
|
-
gsub(/ ?([
|
11
|
-
gsub(/
|
10
|
+
gsub(/ ?([|&]) ?/, '\1').
|
11
|
+
gsub(/(!) /, '\1').
|
12
|
+
gsub(/(\() /, '\1').
|
13
|
+
gsub(/ (\))/, '\1').
|
12
14
|
strip
|
13
15
|
end
|
14
16
|
|