es_searchable 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 +7 -0
- data/.gitignore +8 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +145 -0
- data/MIT-LICENSE +20 -0
- data/README.md +281 -0
- data/README.rdoc +3 -0
- data/Rakefile +34 -0
- data/es_searchable.gemspec +29 -0
- data/lib/es_searchable/configurable.rb +43 -0
- data/lib/es_searchable/search_collection.rb +191 -0
- data/lib/es_searchable/version.rb +3 -0
- data/lib/es_searchable.rb +52 -0
- data/lib/tasks/es_searchable_tasks.rake +4 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/models/user.rb +14 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config/application.rb +26 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/migrate/20160304063627_create_users.rb +10 -0
- data/test/dummy/db/migrate/20160304100221_add_is_admin_to_users.rb +5 -0
- data/test/dummy/db/schema.rb +24 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/fixtures/users.yml +9 -0
- data/test/dummy/test/models/user_test.rb +7 -0
- data/test/es_searchable/search_collection_test.rb +474 -0
- data/test/es_searchable_test.rb +58 -0
- data/test/test_helper.rb +38 -0
- metadata +270 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb43d18c820180b2f727faa7bd5438b0c700ed47
|
4
|
+
data.tar.gz: b26088eac28c5da9feebe4eeb191a3640720ab44
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 21192fc7a0f1fe59aea51f36d8de27abe14e7c0293105dcfae5933f0398fbbdf2fa676f1d6d53b62a777c7ae4d5131f1fadc3a6ec7d3996b482d636c69d8c646
|
7
|
+
data.tar.gz: 51007ee5bb2508902500d6acc6d938c310920c1ebfe070494098b8ca9297498b3a5b611e5897173a356d95c292f5c0466ec7c48090564ff607e6169d6149b11a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in es_searchable.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
gem 'ffaker'
|
8
|
+
gem 'pry'
|
9
|
+
|
10
|
+
# Declare any dependencies that are still in development here instead of in
|
11
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
12
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
13
|
+
# your gem to rubygems.org.
|
14
|
+
|
15
|
+
# To use a debugger
|
16
|
+
# gem 'byebug', group: [:development, :test]
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
es_searchable (0.1.0)
|
5
|
+
bundler (~> 1.6)
|
6
|
+
elasticsearch
|
7
|
+
elasticsearch-model (~> 0.1.8)
|
8
|
+
elasticsearch-rails (~> 0.1.7)
|
9
|
+
rake
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
actionmailer (4.2.5.2)
|
15
|
+
actionpack (= 4.2.5.2)
|
16
|
+
actionview (= 4.2.5.2)
|
17
|
+
activejob (= 4.2.5.2)
|
18
|
+
mail (~> 2.5, >= 2.5.4)
|
19
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
20
|
+
actionpack (4.2.5.2)
|
21
|
+
actionview (= 4.2.5.2)
|
22
|
+
activesupport (= 4.2.5.2)
|
23
|
+
rack (~> 1.6)
|
24
|
+
rack-test (~> 0.6.2)
|
25
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
+
actionview (4.2.5.2)
|
28
|
+
activesupport (= 4.2.5.2)
|
29
|
+
builder (~> 3.1)
|
30
|
+
erubis (~> 2.7.0)
|
31
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
33
|
+
activejob (4.2.5.2)
|
34
|
+
activesupport (= 4.2.5.2)
|
35
|
+
globalid (>= 0.3.0)
|
36
|
+
activemodel (4.2.5.2)
|
37
|
+
activesupport (= 4.2.5.2)
|
38
|
+
builder (~> 3.1)
|
39
|
+
activerecord (4.2.5.2)
|
40
|
+
activemodel (= 4.2.5.2)
|
41
|
+
activesupport (= 4.2.5.2)
|
42
|
+
arel (~> 6.0)
|
43
|
+
activesupport (4.2.5.2)
|
44
|
+
i18n (~> 0.7)
|
45
|
+
json (~> 1.7, >= 1.7.7)
|
46
|
+
minitest (~> 5.1)
|
47
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
48
|
+
tzinfo (~> 1.1)
|
49
|
+
arel (6.0.3)
|
50
|
+
builder (3.2.2)
|
51
|
+
coderay (1.1.0)
|
52
|
+
concurrent-ruby (1.0.1)
|
53
|
+
elasticsearch (1.0.14)
|
54
|
+
elasticsearch-api (= 1.0.14)
|
55
|
+
elasticsearch-transport (= 1.0.14)
|
56
|
+
elasticsearch-api (1.0.14)
|
57
|
+
multi_json
|
58
|
+
elasticsearch-model (0.1.8)
|
59
|
+
activesupport (> 3)
|
60
|
+
elasticsearch (> 0.4)
|
61
|
+
hashie
|
62
|
+
elasticsearch-rails (0.1.8)
|
63
|
+
elasticsearch-transport (1.0.14)
|
64
|
+
faraday
|
65
|
+
multi_json
|
66
|
+
erubis (2.7.0)
|
67
|
+
faraday (0.9.2)
|
68
|
+
multipart-post (>= 1.2, < 3)
|
69
|
+
ffaker (2.2.0)
|
70
|
+
globalid (0.3.6)
|
71
|
+
activesupport (>= 4.1.0)
|
72
|
+
hashie (3.4.3)
|
73
|
+
i18n (0.7.0)
|
74
|
+
json (1.8.3)
|
75
|
+
loofah (2.0.3)
|
76
|
+
nokogiri (>= 1.5.9)
|
77
|
+
mail (2.6.3)
|
78
|
+
mime-types (>= 1.16, < 3)
|
79
|
+
metaclass (0.0.4)
|
80
|
+
method_source (0.8.2)
|
81
|
+
mime-types (2.99.1)
|
82
|
+
mini_portile2 (2.0.0)
|
83
|
+
minitest (5.8.4)
|
84
|
+
mocha (1.1.0)
|
85
|
+
metaclass (~> 0.0.1)
|
86
|
+
multi_json (1.11.2)
|
87
|
+
multipart-post (2.0.0)
|
88
|
+
nokogiri (1.6.7.2)
|
89
|
+
mini_portile2 (~> 2.0.0.rc2)
|
90
|
+
pry (0.10.3)
|
91
|
+
coderay (~> 1.1.0)
|
92
|
+
method_source (~> 0.8.1)
|
93
|
+
slop (~> 3.4)
|
94
|
+
rack (1.6.4)
|
95
|
+
rack-test (0.6.3)
|
96
|
+
rack (>= 1.0)
|
97
|
+
rails (4.2.5.2)
|
98
|
+
actionmailer (= 4.2.5.2)
|
99
|
+
actionpack (= 4.2.5.2)
|
100
|
+
actionview (= 4.2.5.2)
|
101
|
+
activejob (= 4.2.5.2)
|
102
|
+
activemodel (= 4.2.5.2)
|
103
|
+
activerecord (= 4.2.5.2)
|
104
|
+
activesupport (= 4.2.5.2)
|
105
|
+
bundler (>= 1.3.0, < 2.0)
|
106
|
+
railties (= 4.2.5.2)
|
107
|
+
sprockets-rails
|
108
|
+
rails-deprecated_sanitizer (1.0.3)
|
109
|
+
activesupport (>= 4.2.0.alpha)
|
110
|
+
rails-dom-testing (1.0.7)
|
111
|
+
activesupport (>= 4.2.0.beta, < 5.0)
|
112
|
+
nokogiri (~> 1.6.0)
|
113
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
114
|
+
rails-html-sanitizer (1.0.3)
|
115
|
+
loofah (~> 2.0)
|
116
|
+
railties (4.2.5.2)
|
117
|
+
actionpack (= 4.2.5.2)
|
118
|
+
activesupport (= 4.2.5.2)
|
119
|
+
rake (>= 0.8.7)
|
120
|
+
thor (>= 0.18.1, < 2.0)
|
121
|
+
rake (10.5.0)
|
122
|
+
slop (3.6.0)
|
123
|
+
sprockets (3.5.2)
|
124
|
+
concurrent-ruby (~> 1.0)
|
125
|
+
rack (> 1, < 3)
|
126
|
+
sprockets-rails (3.0.3)
|
127
|
+
actionpack (>= 4.0)
|
128
|
+
activesupport (>= 4.0)
|
129
|
+
sprockets (>= 3.0.0)
|
130
|
+
sqlite3 (1.3.11)
|
131
|
+
thor (0.19.1)
|
132
|
+
thread_safe (0.3.5)
|
133
|
+
tzinfo (1.2.2)
|
134
|
+
thread_safe (~> 0.1)
|
135
|
+
|
136
|
+
PLATFORMS
|
137
|
+
ruby
|
138
|
+
|
139
|
+
DEPENDENCIES
|
140
|
+
es_searchable!
|
141
|
+
ffaker
|
142
|
+
mocha
|
143
|
+
pry
|
144
|
+
rails
|
145
|
+
sqlite3
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 zuozuo
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,281 @@
|
|
1
|
+
# es_searchable
|
2
|
+
es_searchable implemented an ActiveRecord like search api for Elasticsearch.
|
3
|
+
|
4
|
+
## Install
|
5
|
+
|
6
|
+
`gem 'es_searchable', git: 'git@github.com:rongchain/es_searchable.git'`
|
7
|
+
|
8
|
+
or
|
9
|
+
|
10
|
+
`gem install es_searchable`
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
`bundle`
|
15
|
+
|
16
|
+
## Configure
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
|
20
|
+
# config/initializers/es_searchable.rb
|
21
|
+
|
22
|
+
EsSearchable.configure do |config|
|
23
|
+
config.logger = Logger.new("log/es_searchable.log")
|
24
|
+
config.hosts = ["localhost:9200"]
|
25
|
+
end
|
26
|
+
|
27
|
+
```
|
28
|
+
|
29
|
+
## Use es_searchable
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class User
|
33
|
+
include EsSearchable
|
34
|
+
end
|
35
|
+
|
36
|
+
search_collection = User.es_where(id: 1)
|
37
|
+
pp search_collection
|
38
|
+
=> #<EsSearchable::SearchCollection:0x007fa64361be08
|
39
|
+
@conditions=
|
40
|
+
{:query=>{:filtered=>{:filter=>{:bool=>{:must=>[{:term=>{:id=>1}}]}}}}},
|
41
|
+
@klass=User>
|
42
|
+
|
43
|
+
search_collection.class
|
44
|
+
=> EsSearchable::SearchCollection
|
45
|
+
|
46
|
+
search_params = search_collection.search_params
|
47
|
+
=> {:query=>{:filtered=>{:filter=>{:bool=>{:must=>[{:term=>{:id=>1}}]}}}}}
|
48
|
+
```
|
49
|
+
|
50
|
+
call `load` on `search_collection` to perform the search
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
search_response = search_collection.load #perform search
|
54
|
+
pp search_response
|
55
|
+
=> #<EsSearchable::SearchCollection:0x007fa64288bf18
|
56
|
+
@collections=
|
57
|
+
[{"id"=>1,
|
58
|
+
"name"=>"Arnaldo Powlowski PhD",
|
59
|
+
"email"=>"charley_walsh@weber.co.uk",
|
60
|
+
"created_at"=>"2016-03-04T06:47:58.999Z",
|
61
|
+
"updated_at"=>"2016-03-04T06:47:58.999Z",
|
62
|
+
"is_admin"=>true}],
|
63
|
+
@conditions=
|
64
|
+
{:query=>{:filtered=>{:filter=>{:bool=>{:must=>[{:term=>{:id=>1}}]}}}},
|
65
|
+
:fields=>{}},
|
66
|
+
@count=1,
|
67
|
+
@klass=User,
|
68
|
+
@response=
|
69
|
+
{"took"=>1,
|
70
|
+
"timed_out"=>false,
|
71
|
+
"_shards"=>{"total"=>1, "successful"=>1, "failed"=>0},
|
72
|
+
"hits"=>
|
73
|
+
{"total"=>1,
|
74
|
+
"max_score"=>1.0,
|
75
|
+
"hits"=>
|
76
|
+
[{"_index"=>"users",
|
77
|
+
"_type"=>"user",
|
78
|
+
"_id"=>"1",
|
79
|
+
"_score"=>1.0,
|
80
|
+
"_source"=>
|
81
|
+
{"id"=>1,
|
82
|
+
"name"=>"Arnaldo Powlowski PhD",
|
83
|
+
"email"=>"charley_walsh@weber.co.uk",
|
84
|
+
"created_at"=>"2016-03-04T06:47:58.999Z",
|
85
|
+
"updated_at"=>"2016-03-04T06:47:58.999Z",
|
86
|
+
"is_admin"=>true}}]}},
|
87
|
+
@time=1>
|
88
|
+
|
89
|
+
pp search_response.collections #return search results as an Array of hash
|
90
|
+
=> [{"id"=>1,
|
91
|
+
"name"=>"Arnaldo Powlowski PhD",
|
92
|
+
"email"=>"charley_walsh@weber.co.uk",
|
93
|
+
"created_at"=>"2016-03-04T06:47:58.999Z",
|
94
|
+
"updated_at"=>"2016-03-04T06:47:58.999Z",
|
95
|
+
"is_admin"=>true}}]
|
96
|
+
|
97
|
+
search_response.time #time consumed by the search
|
98
|
+
=> 1
|
99
|
+
|
100
|
+
search_response.count #count of search results
|
101
|
+
=> 1
|
102
|
+
|
103
|
+
pp search_response.response #response hash from elasticsearch
|
104
|
+
=> {"took"=>1,
|
105
|
+
"timed_out"=>false,
|
106
|
+
"_shards"=>{"total"=>1, "successful"=>1, "failed"=>0},
|
107
|
+
"hits"=>
|
108
|
+
{"total"=>1,
|
109
|
+
"max_score"=>1.0,
|
110
|
+
"hits"=>
|
111
|
+
[{"_index"=>"users",
|
112
|
+
"_type"=>"user",
|
113
|
+
"_id"=>"1",
|
114
|
+
"_score"=>1.0,
|
115
|
+
"_source"=>
|
116
|
+
{"id"=>1,
|
117
|
+
"name"=>"Arnaldo Powlowski PhD",
|
118
|
+
"email"=>"charley_walsh@weber.co.uk",
|
119
|
+
"created_at"=>"2016-03-04T06:47:58.999Z",
|
120
|
+
"updated_at"=>"2016-03-04T06:47:58.999Z",
|
121
|
+
"is_admin"=>true}}}]}}
|
122
|
+
```
|
123
|
+
|
124
|
+
### #es_where
|
125
|
+
```ruby
|
126
|
+
# Like ActiveRecord, es_searchable use es_where for exact matching,
|
127
|
+
pp User.es_where(id: 1).load.collections
|
128
|
+
=> [{"id"=>1,
|
129
|
+
"name"=>"Arnaldo Powlowski PhD",
|
130
|
+
"email"=>"charley_walsh@weber.co.uk",
|
131
|
+
"created_at"=>"2016-03-04T06:47:58.999Z",
|
132
|
+
"updated_at"=>"2016-03-04T06:47:58.999Z",
|
133
|
+
"is_admin"=>true}}]
|
134
|
+
|
135
|
+
pp User.es_where(id: [1,2]).load.collections
|
136
|
+
=> [{"id"=>1,
|
137
|
+
"name"=>"Arnaldo Powlowski PhD",
|
138
|
+
"email"=>"charley_walsh@weber.co.uk",
|
139
|
+
"created_at"=>"2016-03-04T06:47:58.999Z",
|
140
|
+
"updated_at"=>"2016-03-04T06:47:58.999Z",
|
141
|
+
"is_admin"=>true}},
|
142
|
+
{"id"=>2,
|
143
|
+
"name"=>"Chanel Robel",
|
144
|
+
"email"=>"wilma@rice.com",
|
145
|
+
"created_at"=>"2016-03-04T06:47:59.008Z",
|
146
|
+
"updated_at"=>"2016-03-04T06:47:59.008Z",
|
147
|
+
"is_admin"=>false}}]
|
148
|
+
|
149
|
+
# #es_where with multiple search conditions
|
150
|
+
User.es_where(id: 1, is_admin: false).load.collections
|
151
|
+
=> []
|
152
|
+
|
153
|
+
pp User.es_where(id: 1, is_admin: true).load.collections
|
154
|
+
=> [{"id"=>1,
|
155
|
+
"name"=>"Arnaldo Powlowski PhD",
|
156
|
+
"email"=>"charley_walsh@weber.co.uk",
|
157
|
+
"created_at"=>"2016-03-04T06:47:58.999Z",
|
158
|
+
"updated_at"=>"2016-03-04T10:03:39.102Z",
|
159
|
+
"is_admin"=>true}]
|
160
|
+
```
|
161
|
+
|
162
|
+
### #es_select
|
163
|
+
```ruby
|
164
|
+
# use es_select to select the attribtues you want
|
165
|
+
pp User.es_where(id: 1).es_select(:id,:name).load.collections
|
166
|
+
=> [{"id"=>1, "name"=>"Arnaldo Powlowski PhD"},
|
167
|
+
{"id"=>2, "name"=>"Chanel Robel"}]
|
168
|
+
```
|
169
|
+
|
170
|
+
### #es_like
|
171
|
+
```ruby
|
172
|
+
# use es_like to perform keyword matching
|
173
|
+
pp User.es_like(name: 'Mr. Su').select(:name).load.collections
|
174
|
+
=> [{"name"=>"Mr. Marquise Goodwin"},
|
175
|
+
{"name"=>"Mr. Stephanie Carter"},
|
176
|
+
{"name"=>"Mr. Susana Jerde"},
|
177
|
+
{"name"=>"Mrs. Earline Thompson"},
|
178
|
+
{"name"=>"Mrs. Eunice Bergnaum"}]
|
179
|
+
pp User.es_like(name: 'Mr. Su').select(:name).search_params
|
180
|
+
=> {:query=>
|
181
|
+
{:filtered=>{:query=>{:bool=>{:must=>[{:match=>{:name=>"Mr. Su"}}]}}}},
|
182
|
+
:fields=>[:name]}
|
183
|
+
|
184
|
+
pp User.es_like(name: {or: 'Mr. Su'}).select(:name).load.collections
|
185
|
+
=> [{"name"=>"Mr. Marquise Goodwin"},
|
186
|
+
{"name"=>"Mr. Stephanie Carter"},
|
187
|
+
{"name"=>"Mr. Susana Jerde"},
|
188
|
+
{"name"=>"Mrs. Earline Thompson"},
|
189
|
+
{"name"=>"Mrs. Eunice Bergnaum"}]
|
190
|
+
pp User.es_like(name: {or: 'Mr. Su'}).select(:name).search_params
|
191
|
+
=> {:query=>
|
192
|
+
{:filtered=>
|
193
|
+
{:query=>
|
194
|
+
{:bool=>
|
195
|
+
{:must=>[{:match=>{:name=>{:operator=>:or, :query=>"Mr. Su"}}}]}}}},
|
196
|
+
:fields=>[:name]}
|
197
|
+
|
198
|
+
pp User.es_like(name: {and: 'Mr. Su'}).select(:name).load.collections
|
199
|
+
=> []
|
200
|
+
pp User.es_like(name: {and: 'Mr. Su'}).select(:name).search_params
|
201
|
+
=> {:query=>
|
202
|
+
{:filtered=>
|
203
|
+
{:query=>
|
204
|
+
{:bool=>
|
205
|
+
{:must=>[{:match=>{:name=>{:operator=>:and, :query=>"Mr. Su"}}}]}}}},
|
206
|
+
:fields=>[:name]}
|
207
|
+
```
|
208
|
+
### #use :like in #es_where
|
209
|
+
```ruby
|
210
|
+
# the following search are equal
|
211
|
+
User.es_like(name: "test").es_where(id:1)
|
212
|
+
User.es_where(id:1, name: {like: "test"})
|
213
|
+
|
214
|
+
pp User.es_where(id: 1, email: {like: 'charley_walsh'}).select(:id,:email).load.collections
|
215
|
+
=> [{"id"=>1, "email"=>"charley_walsh@weber.co.uk"}]
|
216
|
+
pp User.es_where(id: 1).es_like(email: 'charley_walsh').select(:id,:email).load.collections
|
217
|
+
=> [{"id"=>1, "email"=>"charley_walsh@weber.co.uk"}]
|
218
|
+
|
219
|
+
```
|
220
|
+
### #es_or, #es_and
|
221
|
+
```
|
222
|
+
# use es_or to perform OR condition search
|
223
|
+
User.es_or(id:2, is_admin: true).select(:id,:is_admin).load.collections
|
224
|
+
=> [{"id"=>1, "is_admin"=>true}, {"id"=>2, "is_admin"=>false}]
|
225
|
+
|
226
|
+
# use es_and to perform AND condition search
|
227
|
+
User.es_and(id:2, is_admin: true).select(:id,:is_admin).load.collections
|
228
|
+
=> []
|
229
|
+
User.es_and(id:1, is_admin: true).select(:id,:is_admin).load.collections
|
230
|
+
=> [{"id"=>1, "is_admin"=>true}]
|
231
|
+
|
232
|
+
# es_and is alias of es_where
|
233
|
+
```
|
234
|
+
|
235
|
+
### #es_or and #es_where for nested condition search
|
236
|
+
```ruby
|
237
|
+
User.es_and(id: 1, or: {email: {like: 'brennon'}, is_admin: true})
|
238
|
+
# the condictions of the search above is
|
239
|
+
tel=18510705036 and ( email like 'edaixi.com' or is_admin=true)
|
240
|
+
```
|
241
|
+
|
242
|
+
### #es_not
|
243
|
+
```ruby
|
244
|
+
#use es_not to perform not equal condition search
|
245
|
+
User.es_where({}).load.count
|
246
|
+
=> 100
|
247
|
+
User.es_not({id: 1}).load.count
|
248
|
+
=> 99
|
249
|
+
```
|
250
|
+
|
251
|
+
### #range search
|
252
|
+
```ruby
|
253
|
+
# use the following syntax to perform range search
|
254
|
+
User.es_where(id: {lt: 3, gt:1 }).select(:id).load.collections
|
255
|
+
=> [{"id"=>2}]
|
256
|
+
|
257
|
+
User.es_where(id: {lte: 2, gte: 1 }).select(:id).load.collections
|
258
|
+
=> [{"id"=>1}, {"id"=>2}]
|
259
|
+
|
260
|
+
# lt => less than <
|
261
|
+
# gt => greater than >
|
262
|
+
# lte => less than equal <=
|
263
|
+
# gte => greater than equal >=
|
264
|
+
|
265
|
+
### #es_limit and #es_offset
|
266
|
+
|
267
|
+
# es_limit and es_offset work the same as limit and offset methods of ActiveRecor
|
268
|
+
|
269
|
+
User.es_where({}).select(:id).limit(3).load.collections
|
270
|
+
=> [{"id"=>1}, {"id"=>2}, {"id"=>3}]
|
271
|
+
|
272
|
+
User.es_where({}).select(:id).limit(3).offset(2).load.collections
|
273
|
+
=> [{"id"=>3}, {"id"=>4}, {"id"=>5}]
|
274
|
+
```
|
275
|
+
|
276
|
+
### *_like, *_between, *_gt, *_lt, *_gte, *_lte
|
277
|
+
```ruby
|
278
|
+
User.name_like('test') <=> User.like(name: 'test')
|
279
|
+
User.id_lte(10) <=> User.lte(id: 10)
|
280
|
+
User.id_between(1,3) <=> User.where(id: {gte: 1, lte: 2})
|
281
|
+
```
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'EsSearchable'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'es_searchable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "es_searchable"
|
8
|
+
spec.version = EsSearchable::VERSION
|
9
|
+
spec.authors = ["zuozuo"]
|
10
|
+
spec.email = ["c_yzuo@groupon.com"]
|
11
|
+
spec.summary = %q{A small wrapper of elasticsearch search api}
|
12
|
+
spec.description = %q{A small wrapper of elasticsearch search api}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_dependency "rake"
|
23
|
+
spec.add_dependency "elasticsearch"
|
24
|
+
spec.add_dependency "elasticsearch-rails", "~> 0.1.7"
|
25
|
+
spec.add_dependency "elasticsearch-model", "~> 0.1.8"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
spec.add_development_dependency "rails"
|
28
|
+
spec.add_development_dependency "mocha"
|
29
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module EsSearchable
|
2
|
+
module Configurable
|
3
|
+
def self.extended(base)
|
4
|
+
base.const_set :SearchMethods,
|
5
|
+
[:where, :like, :limit, :offset, :or, :not, :select, :and]
|
6
|
+
|
7
|
+
base.const_set :DEFAULTS, {
|
8
|
+
log: true,
|
9
|
+
retry_on_failure: 5,
|
10
|
+
async_callback: true,
|
11
|
+
queue: 'elasticsearch',
|
12
|
+
reload_on_failure: true,
|
13
|
+
hosts: ['localhost:9200'],
|
14
|
+
logger: defined?(Rails) ? Logger.new($stdout) : nil,
|
15
|
+
}
|
16
|
+
|
17
|
+
base.class_eval do
|
18
|
+
DEFAULTS.each do |k, v|
|
19
|
+
self.define_singleton_method "#{k}=" do |value|
|
20
|
+
self.options.merge!(k => value)
|
21
|
+
end
|
22
|
+
|
23
|
+
self.define_singleton_method k do
|
24
|
+
self.options[k]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def configure
|
31
|
+
yield self
|
32
|
+
end
|
33
|
+
|
34
|
+
def options
|
35
|
+
@options ||= DEFAULTS.dup
|
36
|
+
end
|
37
|
+
|
38
|
+
def options=(opts)
|
39
|
+
@options = opts
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|