search_query_parser 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +87 -0
- data/Rakefile +11 -0
- data/bin/console +22 -0
- data/bin/setup +8 -0
- data/lib/search_query_parser/grammar.rb +93 -0
- data/lib/search_query_parser/interpreter.rb +195 -0
- data/lib/search_query_parser/interpreter.y +52 -0
- data/lib/search_query_parser/version.rb +3 -0
- data/lib/search_query_parser.rb +28 -0
- data/search_query_parser.gemspec +27 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b9603da0b877d7fea0467d4faa78080ca7994c70
|
4
|
+
data.tar.gz: b6e9dc1c95f6b56ab5a7efc749ce54baf5be19b4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70764ba808723f89d94c0c2b2d8b06938ee8e3001357c7ac98aa33e4f2462d8c70e3cb25f70377b99bb693776cbad60c064ecb361a81c1cd6177550bf8bfa0cd
|
7
|
+
data.tar.gz: 0b4d974c8cf774fe3d612a1268e7e237866182fd2946c1b28033ef972983a1c27d75ff8badbc2dfa377659ae295e1d23a2fbd938b553077b73e8e22a9364449a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
search_query_parser (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
racc (1.4.14)
|
11
|
+
rake (10.5.0)
|
12
|
+
rspec (3.7.0)
|
13
|
+
rspec-core (~> 3.7.0)
|
14
|
+
rspec-expectations (~> 3.7.0)
|
15
|
+
rspec-mocks (~> 3.7.0)
|
16
|
+
rspec-core (3.7.0)
|
17
|
+
rspec-support (~> 3.7.0)
|
18
|
+
rspec-expectations (3.7.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.7.0)
|
21
|
+
rspec-mocks (3.7.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.7.0)
|
24
|
+
rspec-support (3.7.0)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler (~> 1.16)
|
31
|
+
racc (~> 1.4)
|
32
|
+
rake (~> 10.0)
|
33
|
+
rspec (~> 3.0)
|
34
|
+
search_query_parser!
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
1.16.0
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Eugene Zolotarev
|
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/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Eugene Zolotarev
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# SearchQueryParser
|
2
|
+
|
3
|
+
This gem allows you to create PostgreSQL 9.6 compatible ts_query expressions from simple-to-use "query language".
|
4
|
+
|
5
|
+
See for reference:
|
6
|
+
|
7
|
+
* [Text Search Types](https://www.postgresql.org/docs/9.6/static/datatype-textsearch.html) on PostgreSQL docs
|
8
|
+
* [Text Search Functions and Operators](https://www.postgresql.org/docs/9.6/static/functions-textsearch.html) on PostgreSQL docs
|
9
|
+
|
10
|
+
Example:
|
11
|
+
|
12
|
+
```
|
13
|
+
require 'search_query_parser'
|
14
|
+
|
15
|
+
q = SearchQueryParser.to_ts_query("
|
16
|
+
(cats | !(angry dogs)) &
|
17
|
+
(кошки | !(злые собаки))
|
18
|
+
")
|
19
|
+
puts q
|
20
|
+
# ((to_tsquery('cats:*') || (!! (to_tsquery('angry:*') <-> to_tsquery('dogs:*')))) && (to_tsquery('кошки:*') || (!! (to_tsquery('злые:*') <-> to_tsquery('собаки:*')))))
|
21
|
+
```
|
22
|
+
|
23
|
+
You can use `&`, `|`, `!` operators in the source query, which are transformed into corresponding PostgreSQL operators. Spaces are transformed into `<->` ("follows") PostgreSQL operator.
|
24
|
+
|
25
|
+
`.to_ts_query` accepts 3 arguments, only first is required:
|
26
|
+
|
27
|
+
* Search string
|
28
|
+
* Language ('enlgish' by default, will be given as first argument to Postgres' `to_tsquery`)
|
29
|
+
* Whether to use prefix (`true` by default, `:*` will be added to each word)
|
30
|
+
|
31
|
+
## What's the problem
|
32
|
+
|
33
|
+
When you use PostgreSQL full-text search you want your users to be able to utilize full power of expressions in a convinient manner. Let's say, you want your users to be able to issue a request to find all documents with `cats` but without `dogs`.
|
34
|
+
|
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
|
+
|
37
|
+
```
|
38
|
+
=> select plainto_tsquery('cats & !dogs');
|
39
|
+
plainto_tsquery
|
40
|
+
-----------------
|
41
|
+
'cat' & 'dog'
|
42
|
+
```
|
43
|
+
|
44
|
+
...so, it just drops all the punctuation and concatenate words with logical AND (`&`), just as the documentation says.
|
45
|
+
|
46
|
+
## The solution
|
47
|
+
|
48
|
+
Use `SearchQueryParser.to_ts_query(q)` to produce PostgreSQL-compatible expression. All non-alphanumeric and non-operator symbols will be dropped from the `q`.
|
49
|
+
|
50
|
+
## How can I use it?
|
51
|
+
|
52
|
+
Use it in your Rails finders for models with `tsdata` columns like that:
|
53
|
+
|
54
|
+
```
|
55
|
+
q = SearchQueryParser.to_ts_query('кошки & !собаки', 'russian')
|
56
|
+
Document.where("tsdata @@ #{q}")
|
57
|
+
```
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
|
61
|
+
Add this line to your application's Gemfile:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
gem 'search_query_parser'
|
65
|
+
```
|
66
|
+
|
67
|
+
And then execute:
|
68
|
+
|
69
|
+
$ bundle
|
70
|
+
|
71
|
+
Or install it yourself as:
|
72
|
+
|
73
|
+
$ gem install search_query_parser
|
74
|
+
|
75
|
+
## Development
|
76
|
+
|
77
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
78
|
+
|
79
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/search_query_parser.
|
84
|
+
|
85
|
+
## License
|
86
|
+
|
87
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc "Regenerate interpreter file from definitions"
|
9
|
+
task "interpreter:generate" do
|
10
|
+
puts `bundle exec racc -o #{__dir__}/lib/search_query_parser/interpreter.rb #{__dir__}/lib/search_query_parser/interpreter.y`
|
11
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "search_query_parser"
|
5
|
+
require "search_query_parser/grammar"
|
6
|
+
require "search_query_parser/interpreter"
|
7
|
+
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
10
|
+
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
12
|
+
# require "pry"
|
13
|
+
# Pry.start
|
14
|
+
|
15
|
+
G = SearchQueryParser::Grammar
|
16
|
+
I = SearchQueryParser::Interpreter
|
17
|
+
T = SearchQueryParser.method(:to_ts_query)
|
18
|
+
|
19
|
+
puts "G: Grammar (new, &, |, etc.), I: Interpreter (#parse), T.(): #to_ts_query"
|
20
|
+
|
21
|
+
require "irb"
|
22
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
module SearchQueryParser
|
2
|
+
class Grammar
|
3
|
+
def self.prepare_text(text)
|
4
|
+
text.
|
5
|
+
gsub(/[&]+/, '&').
|
6
|
+
gsub(/[|]+/, '|').
|
7
|
+
gsub(/[^[:alnum:]\-()&|!]+/, ' ').
|
8
|
+
gsub(/([^[:alnum:]]|^)\-/, '\1').
|
9
|
+
gsub(/\-([^[:alnum:]]|$)/, '\1').
|
10
|
+
gsub(/ ?([|&!]) /, '\1').
|
11
|
+
gsub(/ ?([()]) ?/, '\1').
|
12
|
+
strip
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :expression
|
16
|
+
|
17
|
+
def initialize(e)
|
18
|
+
if e.is_a?(String)
|
19
|
+
@expression = [:term, e]
|
20
|
+
elsif e.is_a?(Array)
|
21
|
+
@expression = e
|
22
|
+
else
|
23
|
+
raise RuntimeError.new("Unknown expression #{e.inspect} of #{e.class.name}")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def join(other)
|
28
|
+
self.class.new([:join, self, other])
|
29
|
+
end
|
30
|
+
def >>(other)
|
31
|
+
join(other)
|
32
|
+
end
|
33
|
+
|
34
|
+
def and(other)
|
35
|
+
self.class.new([:and, self, other])
|
36
|
+
end
|
37
|
+
def &(other)
|
38
|
+
self.and(other)
|
39
|
+
end
|
40
|
+
|
41
|
+
def or(other)
|
42
|
+
self.class.new([:or, self, other])
|
43
|
+
end
|
44
|
+
def |(other)
|
45
|
+
self.or(other)
|
46
|
+
end
|
47
|
+
|
48
|
+
def not
|
49
|
+
self.class.new([:not, self])
|
50
|
+
end
|
51
|
+
def !
|
52
|
+
self.not
|
53
|
+
end
|
54
|
+
|
55
|
+
# Block to reduce must be given and accept
|
56
|
+
# (op, x, y), (term, x), (not, x)
|
57
|
+
# and return the result of applying op to (x, y) or x (for 'term' and 'not')
|
58
|
+
def reduce(&block)
|
59
|
+
op, x, y = @expression
|
60
|
+
case op
|
61
|
+
when :term
|
62
|
+
yield(:term, x, nil)
|
63
|
+
when :not
|
64
|
+
yield(:not, x.reduce(&block), nil)
|
65
|
+
when :and
|
66
|
+
yield(:and, x.reduce(&block), y.reduce(&block))
|
67
|
+
when :or
|
68
|
+
yield(:or, x.reduce(&block), y.reduce(&block))
|
69
|
+
when :join
|
70
|
+
yield(:join, x.reduce(&block), y.reduce(&block))
|
71
|
+
else
|
72
|
+
raise RuntimeError.new("Unknown op #{op.inspect}")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_s
|
77
|
+
reduce do |op, x, y|
|
78
|
+
case op
|
79
|
+
when :term
|
80
|
+
x
|
81
|
+
when :and
|
82
|
+
"(#{x} & #{y})"
|
83
|
+
when :or
|
84
|
+
"(#{x} | #{y})"
|
85
|
+
when :join
|
86
|
+
"(#{x} <-> #{y})"
|
87
|
+
when :not
|
88
|
+
"(! #{x})"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,195 @@
|
|
1
|
+
#
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by Racc 1.4.14
|
4
|
+
# from Racc grammer file "".
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'racc/parser.rb'
|
8
|
+
|
9
|
+
|
10
|
+
require 'search_query_parser/grammar'
|
11
|
+
|
12
|
+
class SearchQueryParser::ParseError < ArgumentError; end
|
13
|
+
|
14
|
+
module SearchQueryParser
|
15
|
+
class Interpreter < Racc::Parser
|
16
|
+
|
17
|
+
module_eval(<<'...end interpreter.y/module_eval...', 'interpreter.y', 26)
|
18
|
+
|
19
|
+
def parse(str)
|
20
|
+
@q = []
|
21
|
+
str = SearchQueryParser::Grammar.prepare_text(str)
|
22
|
+
str.scan(/[[:alnum:]\-]+|[&|()!\s]/) do |token|
|
23
|
+
case token
|
24
|
+
when /[[:alnum:]\-]+/
|
25
|
+
@q.push [:TERM, SearchQueryParser::Grammar.new(token)]
|
26
|
+
when /\s/
|
27
|
+
@q.push ['<->', '<->']
|
28
|
+
else
|
29
|
+
@q.push [token, token]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
@q.push [false, '$end']
|
33
|
+
begin
|
34
|
+
do_parse
|
35
|
+
rescue Racc::ParseError => e
|
36
|
+
raise SearchQueryParser::ParseError.new(e)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def next_token
|
41
|
+
@q.shift
|
42
|
+
end
|
43
|
+
|
44
|
+
...end interpreter.y/module_eval...
|
45
|
+
##### State transition tables begin ###
|
46
|
+
|
47
|
+
racc_action_table = [
|
48
|
+
4, 4, 9, 7, 3, 3, 5, 5, 4, 4,
|
49
|
+
6, 9, 3, 3, 5, 5, 4, 4, 12, nil,
|
50
|
+
3, 3, 5, 5, 9, 7, 8, nil, 16, 9,
|
51
|
+
7, 8 ]
|
52
|
+
|
53
|
+
racc_action_check = [
|
54
|
+
0, 3, 14, 14, 0, 3, 0, 3, 4, 7,
|
55
|
+
1, 13, 4, 7, 4, 7, 8, 9, 6, nil,
|
56
|
+
8, 9, 8, 9, 10, 10, 10, nil, 10, 2,
|
57
|
+
2, 2 ]
|
58
|
+
|
59
|
+
racc_action_pointer = [
|
60
|
+
-2, 10, 26, -1, 6, nil, 18, 7, 14, 15,
|
61
|
+
21, nil, nil, 8, -1, nil, nil ]
|
62
|
+
|
63
|
+
racc_action_default = [
|
64
|
+
-8, -8, -1, -8, -8, -7, -8, -8, -8, -8,
|
65
|
+
-8, -6, 17, -2, -3, -4, -5 ]
|
66
|
+
|
67
|
+
racc_goto_table = [
|
68
|
+
2, 1, nil, 10, 11, nil, nil, 13, 14, 15 ]
|
69
|
+
|
70
|
+
racc_goto_check = [
|
71
|
+
2, 1, nil, 2, 2, nil, nil, 2, 2, 2 ]
|
72
|
+
|
73
|
+
racc_goto_pointer = [
|
74
|
+
nil, 1, 0 ]
|
75
|
+
|
76
|
+
racc_goto_default = [
|
77
|
+
nil, nil, nil ]
|
78
|
+
|
79
|
+
racc_reduce_table = [
|
80
|
+
0, 0, :racc_error,
|
81
|
+
1, 10, :_reduce_none,
|
82
|
+
3, 11, :_reduce_2,
|
83
|
+
3, 11, :_reduce_3,
|
84
|
+
3, 11, :_reduce_4,
|
85
|
+
3, 11, :_reduce_5,
|
86
|
+
2, 11, :_reduce_6,
|
87
|
+
1, 11, :_reduce_7 ]
|
88
|
+
|
89
|
+
racc_reduce_n = 8
|
90
|
+
|
91
|
+
racc_shift_n = 17
|
92
|
+
|
93
|
+
racc_token_table = {
|
94
|
+
false => 0,
|
95
|
+
:error => 1,
|
96
|
+
"!" => 2,
|
97
|
+
"<->" => 3,
|
98
|
+
"&" => 4,
|
99
|
+
"|" => 5,
|
100
|
+
"(" => 6,
|
101
|
+
")" => 7,
|
102
|
+
:TERM => 8 }
|
103
|
+
|
104
|
+
racc_nt_base = 9
|
105
|
+
|
106
|
+
racc_use_result_var = true
|
107
|
+
|
108
|
+
Racc_arg = [
|
109
|
+
racc_action_table,
|
110
|
+
racc_action_check,
|
111
|
+
racc_action_default,
|
112
|
+
racc_action_pointer,
|
113
|
+
racc_goto_table,
|
114
|
+
racc_goto_check,
|
115
|
+
racc_goto_default,
|
116
|
+
racc_goto_pointer,
|
117
|
+
racc_nt_base,
|
118
|
+
racc_reduce_table,
|
119
|
+
racc_token_table,
|
120
|
+
racc_shift_n,
|
121
|
+
racc_reduce_n,
|
122
|
+
racc_use_result_var ]
|
123
|
+
|
124
|
+
Racc_token_to_s_table = [
|
125
|
+
"$end",
|
126
|
+
"error",
|
127
|
+
"\"!\"",
|
128
|
+
"\"<->\"",
|
129
|
+
"\"&\"",
|
130
|
+
"\"|\"",
|
131
|
+
"\"(\"",
|
132
|
+
"\")\"",
|
133
|
+
"TERM",
|
134
|
+
"$start",
|
135
|
+
"target",
|
136
|
+
"exp" ]
|
137
|
+
|
138
|
+
Racc_debug_parser = false
|
139
|
+
|
140
|
+
##### State transition tables end #####
|
141
|
+
|
142
|
+
# reduce 0 omitted
|
143
|
+
|
144
|
+
# reduce 1 omitted
|
145
|
+
|
146
|
+
module_eval(<<'.,.,', 'interpreter.y', 10)
|
147
|
+
def _reduce_2(val, _values, result)
|
148
|
+
result &= val[2]
|
149
|
+
result
|
150
|
+
end
|
151
|
+
.,.,
|
152
|
+
|
153
|
+
module_eval(<<'.,.,', 'interpreter.y', 11)
|
154
|
+
def _reduce_3(val, _values, result)
|
155
|
+
result |= val[2]
|
156
|
+
result
|
157
|
+
end
|
158
|
+
.,.,
|
159
|
+
|
160
|
+
module_eval(<<'.,.,', 'interpreter.y', 12)
|
161
|
+
def _reduce_4(val, _values, result)
|
162
|
+
result >>= val[2]
|
163
|
+
result
|
164
|
+
end
|
165
|
+
.,.,
|
166
|
+
|
167
|
+
module_eval(<<'.,.,', 'interpreter.y', 13)
|
168
|
+
def _reduce_5(val, _values, result)
|
169
|
+
result = val[1]
|
170
|
+
result
|
171
|
+
end
|
172
|
+
.,.,
|
173
|
+
|
174
|
+
module_eval(<<'.,.,', 'interpreter.y', 14)
|
175
|
+
def _reduce_6(val, _values, result)
|
176
|
+
result = !val[1]
|
177
|
+
result
|
178
|
+
end
|
179
|
+
.,.,
|
180
|
+
|
181
|
+
module_eval(<<'.,.,', 'interpreter.y', 15)
|
182
|
+
def _reduce_7(val, _values, result)
|
183
|
+
result = val[0]
|
184
|
+
result
|
185
|
+
end
|
186
|
+
.,.,
|
187
|
+
|
188
|
+
def _reduce_none(val, _values, result)
|
189
|
+
val[0]
|
190
|
+
end
|
191
|
+
|
192
|
+
end # class Interpreter
|
193
|
+
end # module SearchQueryParser
|
194
|
+
|
195
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class SearchQueryParser::Interpreter
|
2
|
+
prechigh
|
3
|
+
nonassoc '!'
|
4
|
+
left '<->'
|
5
|
+
left '&'
|
6
|
+
left '|'
|
7
|
+
preclow
|
8
|
+
rule
|
9
|
+
target: exp
|
10
|
+
|
11
|
+
exp: exp '&' exp { result &= val[2] }
|
12
|
+
| exp '|' exp { result |= val[2] }
|
13
|
+
| exp '<->' exp { result >>= val[2] }
|
14
|
+
| '(' exp ')' { result = val[1] }
|
15
|
+
| '!' exp { result = !val[1] }
|
16
|
+
| TERM { result = val[0] }
|
17
|
+
end
|
18
|
+
|
19
|
+
---- header
|
20
|
+
|
21
|
+
require 'search_query_parser/grammar'
|
22
|
+
|
23
|
+
class SearchQueryParser::ParseError < ArgumentError; end
|
24
|
+
|
25
|
+
---- inner
|
26
|
+
|
27
|
+
def parse(str)
|
28
|
+
@q = []
|
29
|
+
str = SearchQueryParser::Grammar.prepare_text(str)
|
30
|
+
str.scan(/[[:alnum:]\-]+|[&|()!\s]/) do |token|
|
31
|
+
case token
|
32
|
+
when /[[:alnum:]\-]+/
|
33
|
+
@q.push [:TERM, SearchQueryParser::Grammar.new(token)]
|
34
|
+
when /\s/
|
35
|
+
@q.push ['<->', '<->']
|
36
|
+
else
|
37
|
+
@q.push [token, token]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
@q.push [false, '$end']
|
41
|
+
begin
|
42
|
+
do_parse
|
43
|
+
rescue Racc::ParseError => e
|
44
|
+
raise SearchQueryParser::ParseError.new(e)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def next_token
|
49
|
+
@q.shift
|
50
|
+
end
|
51
|
+
|
52
|
+
---- footer
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "search_query_parser/version"
|
2
|
+
require "search_query_parser/interpreter"
|
3
|
+
|
4
|
+
module SearchQueryParser
|
5
|
+
@interpreter = SearchQueryParser::Interpreter.new
|
6
|
+
|
7
|
+
def self.to_ts_query(str, language = 'english', prefix = true)
|
8
|
+
language = language.gsub(/[^a-zA-Z\-]/, '')
|
9
|
+
@interpreter.parse(str).reduce do |op, x, y|
|
10
|
+
case op
|
11
|
+
when :term
|
12
|
+
if prefix
|
13
|
+
%Q{to_tsquery('#{language}', '#{x}:*')}
|
14
|
+
else
|
15
|
+
%Q{to_tsquery('#{language}', '#{x}')}
|
16
|
+
end
|
17
|
+
when :not
|
18
|
+
%Q{(!! #{x})}
|
19
|
+
when :and
|
20
|
+
%Q{(#{x} && #{y})}
|
21
|
+
when :or
|
22
|
+
%Q{(#{x} || #{y})}
|
23
|
+
when :join
|
24
|
+
%Q{(#{x} <-> #{y})}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "search_query_parser/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "search_query_parser"
|
8
|
+
spec.version = SearchQueryParser::VERSION
|
9
|
+
spec.authors = ["Eugene Zolotarev"]
|
10
|
+
spec.email = ["eugzol@gmail.com"]
|
11
|
+
spec.homepage = "https://github.com/EugZol/search_query_spec"
|
12
|
+
|
13
|
+
spec.summary = %q{Parser for simple search query language and an interpreter to produce PostgreSQL tsearch directives}
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "racc", "~> 1.4"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: search_query_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eugene Zolotarev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: racc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.4'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- eugzol@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/search_query_parser.rb
|
88
|
+
- lib/search_query_parser/grammar.rb
|
89
|
+
- lib/search_query_parser/interpreter.rb
|
90
|
+
- lib/search_query_parser/interpreter.y
|
91
|
+
- lib/search_query_parser/version.rb
|
92
|
+
- search_query_parser.gemspec
|
93
|
+
homepage: https://github.com/EugZol/search_query_spec
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.6.11
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Parser for simple search query language and an interpreter to produce PostgreSQL
|
117
|
+
tsearch directives
|
118
|
+
test_files: []
|