chewy 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.travis.yml +3 -2
- data/CHANGELOG.md +28 -2
- data/README.md +156 -7
- data/filters +6 -4
- data/lib/chewy/index.rb +114 -18
- data/lib/chewy/index/actions.rb +117 -14
- data/lib/chewy/index/aliases.rb +21 -0
- data/lib/chewy/query.rb +5 -5
- data/lib/chewy/query/compose.rb +59 -0
- data/lib/chewy/query/criteria.rb +8 -55
- data/lib/chewy/query/{context.rb → filters.rb} +60 -7
- data/lib/chewy/query/loading.rb +1 -1
- data/lib/chewy/query/nodes/has_child.rb +14 -0
- data/lib/chewy/query/nodes/has_parent.rb +14 -0
- data/lib/chewy/query/nodes/has_relation.rb +61 -0
- data/lib/chewy/query/nodes/match_all.rb +11 -0
- data/lib/chewy/railtie.rb +2 -2
- data/lib/chewy/rspec/update_index.rb +1 -0
- data/lib/chewy/type.rb +1 -1
- data/lib/chewy/type/adapter/active_record.rb +28 -12
- data/lib/chewy/type/adapter/object.rb +12 -2
- data/lib/chewy/type/import.rb +60 -20
- data/lib/chewy/type/wrapper.rb +1 -1
- data/lib/chewy/version.rb +1 -1
- data/lib/tasks/chewy.rake +41 -8
- data/spec/chewy/index/actions_spec.rb +282 -2
- data/spec/chewy/index/aliases_spec.rb +50 -0
- data/spec/chewy/index_spec.rb +64 -0
- data/spec/chewy/query/criteria_spec.rb +11 -11
- data/spec/chewy/query/{context_spec.rb → filters_spec.rb} +2 -2
- data/spec/chewy/query/loading_spec.rb +5 -3
- data/spec/chewy/query/nodes/and_spec.rb +1 -1
- data/spec/chewy/query/nodes/bool_spec.rb +1 -1
- data/spec/chewy/query/nodes/equal_spec.rb +1 -1
- data/spec/chewy/query/nodes/exists_spec.rb +1 -1
- data/spec/chewy/query/nodes/has_child_spec.rb +40 -0
- data/spec/chewy/query/nodes/has_parent_spec.rb +40 -0
- data/spec/chewy/query/nodes/match_all_spec.rb +11 -0
- data/spec/chewy/query/nodes/missing_spec.rb +1 -1
- data/spec/chewy/query/nodes/not_spec.rb +1 -1
- data/spec/chewy/query/nodes/or_spec.rb +1 -1
- data/spec/chewy/query/nodes/prefix_spec.rb +1 -1
- data/spec/chewy/query/nodes/query_spec.rb +1 -1
- data/spec/chewy/query/nodes/range_spec.rb +1 -1
- data/spec/chewy/query/nodes/raw_spec.rb +1 -1
- data/spec/chewy/query/nodes/regexp_spec.rb +1 -1
- data/spec/chewy/query/nodes/script_spec.rb +1 -1
- data/spec/chewy/query/pagination_spec.rb +7 -7
- data/spec/chewy/query_spec.rb +60 -0
- data/spec/chewy/type/adapter/active_record_spec.rb +68 -9
- data/spec/chewy/type/adapter/object_spec.rb +18 -0
- data/spec/chewy/type/import_spec.rb +87 -2
- data/spec/spec_helper.rb +1 -0
- metadata +47 -33
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
MWIwNDA3MzMyNzRlZGM2ODllODA4ZjAxMzI0NzM5OTc3MGU4NjViMA==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 37ef7870c9b4ea62e334143eddf486f55e21627e
|
4
|
+
data.tar.gz: 57cd914528a78448c58f89d687f84a666b8a2ffc
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
OTgzMTgyNDI2M2RlMjJhMWQzMjk1MzZjOGFkODQ3NzFjZmEwOTQ2M2UzNDMy
|
11
|
-
MDJjY2Y0ZmFiZjU5NjY3MTE5MzQ3NDk0ZDg4MzdjZDJkOTZhNGI=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NWEyODY1M2JhZjEwMDI1MTUyNGQ1NzM4ZTU0NTgyYWRkOWM3NWI0ZmQ2ZmY1
|
14
|
-
ODkyMjc5OTQwNThlODA2YjY3MGM5MTQyZDA5NzMzZDBjOWYyYmI2NmMyNGQx
|
15
|
-
OTU3ZGM3OTEyOGJjMWIyMmEwYWY2MjkzOGJmM2Q3MzQ2MzgzMGE=
|
6
|
+
metadata.gz: 9e923803456e4d75abe810db4c2cceb6bd4cdd758de480f937131570830dcb7e5e1053a4db06ec1aec62f74cf0abe43173f02d461a56e83e9705f2f819d0b7df
|
7
|
+
data.tar.gz: 53e95e0795081fda77fbb8858207081eded90ab8d5ce5b01b4ae2f140641f71e1db11c3af470f321c4ece7f881916045286a4e22b0ab81694ca5bdb01b271655
|
data/.travis.yml
CHANGED
@@ -2,8 +2,9 @@ language: ruby
|
|
2
2
|
rvm:
|
3
3
|
- 1.9.3
|
4
4
|
- 2.0.0
|
5
|
+
- 2.1.0
|
5
6
|
# - rbx
|
6
7
|
before_install:
|
7
|
-
- curl -# https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.
|
8
|
+
- curl -# https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.10.tar.gz | tar xz -C /tmp
|
8
9
|
before_script:
|
9
|
-
- TEST_CLUSTER_COMMAND="/tmp/elasticsearch-0.90.
|
10
|
+
- TEST_CLUSTER_COMMAND="/tmp/elasticsearch-0.90.10/bin/elasticsearch" rake elasticsearch:start
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,36 @@
|
|
1
1
|
# master
|
2
2
|
|
3
|
+
* Reworked import error handling. Now all the import errors from ElasticSearch are handled properly, also import method returns true of false depending on the import process success.
|
4
|
+
|
5
|
+
* `Chewy::Index.import` now takes types hash as argument within options hash:
|
6
|
+
|
7
|
+
`PlacesIndex.import city: City.enabled, country: Country.enabled, refresh: false`
|
8
|
+
|
9
|
+
* Old indexes cleanup after reset.
|
10
|
+
|
11
|
+
* Added index prefixes.
|
12
|
+
|
13
|
+
* `define_type` now takes options for adapter.
|
14
|
+
|
15
|
+
* `chewy:reset` and `chewy:reset:all` rake tasks are now trying to reset index with zero downtime if it is possible.
|
16
|
+
|
17
|
+
* Added `chewy:update:all` rake task.
|
18
|
+
|
19
|
+
* Methods `.create`, `.create!`, `.delete`, `.delete`, `reset!` are now supports index name suffix passing as the first argument. See [actions.rb](lib/chewy/index/actions.rb) for more details.
|
20
|
+
|
21
|
+
* Method `reset` renamed to `reset!`.
|
22
|
+
|
23
|
+
* Added common loading scope for AR adapter. Also removed scope proc argument, now it executes just in main load scope context.
|
24
|
+
|
25
|
+
`CitiesIndex.all.load(scope: {city: City.include(:country)})`
|
26
|
+
`CitiesIndex.all.load(scope: {city: -> { include(:country) }})`
|
27
|
+
`CitiesIndex.all.load(scope: ->{ include(:country) })`
|
28
|
+
|
3
29
|
# Version 0.1.0
|
4
30
|
|
5
|
-
* Added filters simplified DSL. See [
|
31
|
+
* Added filters simplified DSL. See [filters.rb](lib/chewy/query/filters.rb) for more details.
|
6
32
|
|
7
|
-
* Queries and filters join system reworked. See [query.rb](lib/chewy/query.rb) for more
|
33
|
+
* Queries and filters join system reworked. See [query.rb](lib/chewy/query.rb) for more details.
|
8
34
|
|
9
35
|
* Added query `merge` method
|
10
36
|
|
data/README.md
CHANGED
@@ -5,6 +5,24 @@
|
|
5
5
|
|
6
6
|
Chewy is ODM and wrapper for official elasticsearch client (https://github.com/elasticsearch/elasticsearch-ruby)
|
7
7
|
|
8
|
+
## Why chewy?
|
9
|
+
|
10
|
+
* Index classes are independant from ORM/ODM models.
|
11
|
+
|
12
|
+
Now implementing, e.g. cross-model autocomplete is much easier. You can just define index and work with it in object-oriented style. You can define several types for index - one per indexed model.
|
13
|
+
|
14
|
+
* Every index is observable by all the related models.
|
15
|
+
|
16
|
+
Most of the indexed models a related to other and somtimes it is nessesary to denormalize this related data and put at the same object. Like you need to index array of tags with article together. Chewy allows you to specify updatable index for every model separately. So, corresponding articles will be reindexed on the any tag update.
|
17
|
+
|
18
|
+
* Bulk import everywhere.
|
19
|
+
|
20
|
+
Chewy utilizes bulk ES API for full reindexing or index updates. Also it uses atomic updates concept. All the changed objects are collected inside the atomic block and index is updated once at the end of it with all the collected object. See `Chewy.atomic` for more details.
|
21
|
+
|
22
|
+
* Powerful querying DSL.
|
23
|
+
|
24
|
+
Chewy has AR-style query DSL. It is chainable, mergable and lazy. So you can produce queries in the most efficient way. Also it has object-oriented query and filter builders.
|
25
|
+
|
8
26
|
## Installation
|
9
27
|
|
10
28
|
Add this line to your application's Gemfile:
|
@@ -21,7 +39,39 @@ Or install it yourself as:
|
|
21
39
|
|
22
40
|
## Usage
|
23
41
|
|
24
|
-
###
|
42
|
+
### Client configuration
|
43
|
+
|
44
|
+
There are 2 ways to configure Chewy client: `Chewy.client_options` hash and `chewy.yml`
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# config/initializers/chewy.rb
|
48
|
+
Chewy.client_options = {host: 'localhost:9250'} # do not use environments
|
49
|
+
```
|
50
|
+
|
51
|
+
```yaml
|
52
|
+
# config/chewy.yml
|
53
|
+
# separate environment configs
|
54
|
+
test:
|
55
|
+
host: 'localhost:9250'
|
56
|
+
prefix: 'test'
|
57
|
+
development:
|
58
|
+
host: 'localhost:9250'
|
59
|
+
```
|
60
|
+
|
61
|
+
The result config merges both hashes. Client options are passed as is to Elasticsearch::Transport::Client except the `:prefix` - it is used internally by chewy to create prefixed index names:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
Chewy.client_options = {prefix: 'testing'}
|
65
|
+
UsersIndex.index_name # => 'testing_users'
|
66
|
+
```
|
67
|
+
|
68
|
+
Also logger might be set explicitly:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
Chewy.logger = Logger.new
|
72
|
+
```
|
73
|
+
|
74
|
+
### Index definition
|
25
75
|
|
26
76
|
1. Create `/app/chewy/users_index.rb`
|
27
77
|
|
@@ -162,9 +212,13 @@ UsersIndex::User.import User.where('rating > 100').to_a # or import specified us
|
|
162
212
|
UsersIndex::User.import [1, 2, 42] # pass even ids for import, it will be handled in the most effective way
|
163
213
|
|
164
214
|
UsersIndex.import # import every defined type
|
165
|
-
UsersIndex.
|
215
|
+
UsersIndex.import user: User.where('rating > 100') # import only active users to `user` type.
|
216
|
+
# Other index types, if exists, will be imported with default scope from the type definition.
|
217
|
+
UsersIndex.reset! # purges index and imports default data for all types
|
166
218
|
```
|
167
219
|
|
220
|
+
See [actions.rb](lib/chewy/index/actions.rb) for more details.
|
221
|
+
|
168
222
|
Also if passed user is #destroyed? or specified id is not existing in the database, import will perform `delete` index for this it
|
169
223
|
|
170
224
|
### Observing strategies
|
@@ -253,7 +307,7 @@ UsersIndex::User.filter{ name == 'Fred' }.filter{ age < 42 }.filter_mode(:should
|
|
253
307
|
UsersIndex::User.filter{ name == 'Fred' }.filter{ age < 42 }.filter_mode('75%') # will be wrapped with bool `should` filter with `minimum_should_match: '75%'`
|
254
308
|
```
|
255
309
|
|
256
|
-
See [query.rb](lib/chewy/query.rb) for more
|
310
|
+
See [query.rb](lib/chewy/query.rb) for more details.
|
257
311
|
|
258
312
|
### Filters query DSL.
|
259
313
|
|
@@ -500,7 +554,41 @@ Compliance cheatsheet for filters and DSL expressions:
|
|
500
554
|
UsersIndex.filter{ !(age < 42) }
|
501
555
|
```
|
502
556
|
|
503
|
-
|
557
|
+
* Match all filter
|
558
|
+
|
559
|
+
```json
|
560
|
+
{"match_all": {}}
|
561
|
+
```
|
562
|
+
|
563
|
+
```ruby
|
564
|
+
UsersIndex.filter{ match_all }
|
565
|
+
```
|
566
|
+
|
567
|
+
* Has child filter
|
568
|
+
|
569
|
+
```json
|
570
|
+
{"has_child": {"type": "blog_tag", "query": {"term": {"tag": "something"}}}
|
571
|
+
{"has_child": {"type": "comment", "term": {"term": {"user": "john"}}}
|
572
|
+
```
|
573
|
+
|
574
|
+
```ruby
|
575
|
+
UsersIndex.filter{ has_child(:blog_tag).query(term: {tag: 'something'}) }
|
576
|
+
UsersIndex.filter{ has_child(:comment).filter{ user == 'john' } }
|
577
|
+
```
|
578
|
+
|
579
|
+
* Has parent filter
|
580
|
+
|
581
|
+
```json
|
582
|
+
{"has_parent": {"type": "blog", "query": {"term": {"tag": "something"}}}}
|
583
|
+
{"has_parent": {"type": "blog", "filter": {"term": {"text": "bonsai three"}}}}
|
584
|
+
```
|
585
|
+
|
586
|
+
```ruby
|
587
|
+
UsersIndex.filter{ has_parent(:blog).query(term: {tag: 'something'}) }
|
588
|
+
UsersIndex.filter{ has_parent(:blog).filter{ text == 'bonsai three' } }
|
589
|
+
```
|
590
|
+
|
591
|
+
See [filters.rb](lib/chewy/query/filters.rb) for more details.
|
504
592
|
|
505
593
|
### Objects loading
|
506
594
|
|
@@ -510,12 +598,73 @@ It is possible to load source objects from database for every search result:
|
|
510
598
|
scope = UsersIndex.filter(range: {rating: {gte: 100}})
|
511
599
|
|
512
600
|
scope.load # => will return User instances array (not a scope because )
|
513
|
-
scope.load(user: { scope: ->
|
514
|
-
|
515
|
-
scope.load(user: { scope: User.includes(:country) }) # the second scope passing way
|
601
|
+
scope.load(user: { scope: ->{ includes(:country) }}) # you can also pass loading scopes for each
|
602
|
+
# possibly returned type
|
603
|
+
scope.load(user: { scope: User.includes(:country) }) # the second scope passing way.
|
604
|
+
scope.load(scope: ->{ includes(:country) }) # and more common scope applied to every loaded object type.
|
605
|
+
|
516
606
|
scope.only(:id).load # it is optimal to request ids only if you are not planning to use type objects
|
517
607
|
```
|
518
608
|
|
609
|
+
### `ActiveSupport::Notifications` support
|
610
|
+
|
611
|
+
Chewy has notifing the following events:
|
612
|
+
|
613
|
+
#### `search_query.chewy` payload
|
614
|
+
|
615
|
+
* `payload[:index]`: requested index class
|
616
|
+
* `payload[:request]`: request hash
|
617
|
+
|
618
|
+
#### `import_objects.chewy` payload
|
619
|
+
|
620
|
+
* `payload[:type]`: currently imported type
|
621
|
+
* `payload[:import]`: imports stast, total imported and deleted objects count:
|
622
|
+
|
623
|
+
```ruby
|
624
|
+
{index: 30, delete: 5}
|
625
|
+
```
|
626
|
+
|
627
|
+
* `payload[:erorrs]`: might not exists. Contains grouped errors with objects ids list:
|
628
|
+
|
629
|
+
```ruby
|
630
|
+
{index: {
|
631
|
+
'error 1 text' => ['1', '2', '3'],
|
632
|
+
'error 2 text' => ['4']
|
633
|
+
}, delete: {
|
634
|
+
'delete error text' => ['10', '12']
|
635
|
+
}}
|
636
|
+
```
|
637
|
+
|
638
|
+
#### NewRelic integration
|
639
|
+
|
640
|
+
To integrate with NewRelic you may use the following example source (config/initializers/chewy.rb):
|
641
|
+
|
642
|
+
```ruby
|
643
|
+
ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
|
644
|
+
metrics = "Database/ElasticSearch/import"
|
645
|
+
duration = (finish - start).to_f
|
646
|
+
logged = "#{payload[:type]} #{payload[:import].to_a.map{ |i| i.join(':') }.join(', ')}"
|
647
|
+
|
648
|
+
self.class.trace_execution_scoped([metrics]) do
|
649
|
+
NewRelic::Agent.instance.transaction_sampler.notice_sql(logged, nil, duration)
|
650
|
+
NewRelic::Agent.instance.sql_sampler.notice_sql(logged, metrics, nil, duration)
|
651
|
+
NewRelic::Agent.instance.stats_engine.record_metrics(metrics, duration)
|
652
|
+
end
|
653
|
+
end
|
654
|
+
|
655
|
+
ActiveSupport::Notifications.subscribe('search_query.chewy') do |name, start, finish, id, payload|
|
656
|
+
metrics = "Database/ElasticSearch/search"
|
657
|
+
duration = (finish - start).to_f
|
658
|
+
logged = "#{payload[:index]} #{payload[:request]}"
|
659
|
+
|
660
|
+
self.class.trace_execution_scoped([metrics]) do
|
661
|
+
NewRelic::Agent.instance.transaction_sampler.notice_sql(logged, nil, duration)
|
662
|
+
NewRelic::Agent.instance.sql_sampler.notice_sql(logged, metrics, nil, duration)
|
663
|
+
NewRelic::Agent.instance.stats_engine.record_metrics(metrics, duration)
|
664
|
+
end
|
665
|
+
end
|
666
|
+
```
|
667
|
+
|
519
668
|
### Rake tasks
|
520
669
|
|
521
670
|
Inside Rails application some index mantaining rake tasks are defined.
|
data/filters
CHANGED
@@ -49,7 +49,8 @@ not
|
|
49
49
|
email != 'email'
|
50
50
|
|
51
51
|
script s()
|
52
|
-
|
52
|
+
match all
|
53
|
+
match_all
|
53
54
|
|
54
55
|
has child
|
55
56
|
has_child('type').query()
|
@@ -59,8 +60,10 @@ has parent
|
|
59
60
|
has_parent('type').filter()
|
60
61
|
nested
|
61
62
|
name.nested()
|
62
|
-
|
63
|
-
|
63
|
+
|
64
|
+
query q()
|
65
|
+
type
|
66
|
+
.types()
|
64
67
|
|
65
68
|
geo bounding box
|
66
69
|
geo distance
|
@@ -70,7 +73,6 @@ geoshape
|
|
70
73
|
geohash cell
|
71
74
|
|
72
75
|
indices
|
73
|
-
type
|
74
76
|
ids
|
75
77
|
|
76
78
|
limit
|
data/lib/chewy/index.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'chewy/index/actions'
|
2
|
+
require 'chewy/index/aliases'
|
2
3
|
require 'chewy/index/search'
|
3
4
|
|
4
5
|
module Chewy
|
5
6
|
class Index
|
6
7
|
include Actions
|
8
|
+
include Aliases
|
7
9
|
include Search
|
8
10
|
|
9
11
|
singleton_class.delegate :client, to: 'Chewy'
|
@@ -14,8 +16,67 @@ module Chewy
|
|
14
16
|
class_attribute :_settings
|
15
17
|
self._settings = {}
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
# Setups or returns ElasticSearch index name
|
20
|
+
#
|
21
|
+
# class UsersIndex < Chewy::Index
|
22
|
+
# end
|
23
|
+
# UsersIndex.index_name # => 'users'
|
24
|
+
#
|
25
|
+
# class UsersIndex < Chewy::Index
|
26
|
+
# index_name 'dudes'
|
27
|
+
# end
|
28
|
+
# UsersIndex.index_name # => 'dudes'
|
29
|
+
#
|
30
|
+
def self.index_name(suggest = nil)
|
31
|
+
if suggest
|
32
|
+
@index_name = build_index_name(suggest, prefix: Chewy.client_options[:prefix])
|
33
|
+
else
|
34
|
+
@index_name ||= begin
|
35
|
+
build_index_name(
|
36
|
+
name.gsub(/Index\Z/, '').demodulize.underscore,
|
37
|
+
prefix: Chewy.client_options[:prefix]
|
38
|
+
) if name
|
39
|
+
end
|
40
|
+
end
|
41
|
+
@index_name or raise UndefinedIndex
|
42
|
+
end
|
43
|
+
|
44
|
+
# Defines type for the index. Arguments depends on adapter used. For
|
45
|
+
# ActiveRecord you can pass model or scope and options
|
46
|
+
#
|
47
|
+
# class CarsIndex < Chewy::Index
|
48
|
+
# define_type Car do
|
49
|
+
# ...
|
50
|
+
# end # defines VehiclesIndex::Car type
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# Type name might be passed in complicated cases:
|
54
|
+
#
|
55
|
+
# class VehiclesIndex < Chewy::Index
|
56
|
+
# define_type Vehicle.cars.includes(:manufacturer), name: 'cars' do
|
57
|
+
# ...
|
58
|
+
# end # defines VehiclesIndex::Cars type
|
59
|
+
#
|
60
|
+
# define_type Vehicle.motocycles.includes(:manufacturer), name: 'motocycles' do
|
61
|
+
# ...
|
62
|
+
# end # defines VehiclesIndex::Motocycles type
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# For plain objects:
|
66
|
+
#
|
67
|
+
# class PlanesIndex < Chewy::Index
|
68
|
+
# define_type :plane do
|
69
|
+
# ...
|
70
|
+
# end # defines PlanesIndex::Plane type
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# The main difference between using plain objects or ActiveRecord models for indexing
|
74
|
+
# is import. If you will call `CarsIndex::Car.import` - it will import all the cars
|
75
|
+
# automatically, while `PlanesIndex::Plane.import(my_planes)` requires import data to be
|
76
|
+
# passed.
|
77
|
+
#
|
78
|
+
def self.define_type(target, options = {}, &block)
|
79
|
+
type_class = Chewy::Type.new(self, target, options, &block)
|
19
80
|
self.type_hash = type_hash.merge(type_class.type_name => type_class)
|
20
81
|
|
21
82
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
@@ -25,6 +86,17 @@ module Chewy
|
|
25
86
|
RUBY
|
26
87
|
end
|
27
88
|
|
89
|
+
# Types method has double usage.
|
90
|
+
# If no arguments are passed - it returns array of defined types:
|
91
|
+
#
|
92
|
+
# UsersIndex.types # => [UsersIndex::Admin, UsersIndex::Manager, UsersIndex::User]
|
93
|
+
#
|
94
|
+
# If arguments are passed it treats like a part of chainable query dsl and
|
95
|
+
# adds types array for index to select.
|
96
|
+
#
|
97
|
+
# UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager)
|
98
|
+
# UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example
|
99
|
+
#
|
28
100
|
def self.types *args
|
29
101
|
if args.any?
|
30
102
|
all.types *args
|
@@ -33,21 +105,54 @@ module Chewy
|
|
33
105
|
end
|
34
106
|
end
|
35
107
|
|
108
|
+
# Returns defined types names:
|
109
|
+
#
|
110
|
+
# UsersIndex.type_names # => ['admin', 'manager', 'user']
|
111
|
+
#
|
36
112
|
def self.type_names
|
37
113
|
type_hash.keys
|
38
114
|
end
|
39
115
|
|
116
|
+
# Used as a part of index definition DSL. Defines settings:
|
117
|
+
#
|
118
|
+
# class UsersIndex < Chewy::Index
|
119
|
+
# settings analysis: {
|
120
|
+
# analyzer: {
|
121
|
+
# name: {
|
122
|
+
# tokenizer: 'standard',
|
123
|
+
# filter: ['lowercase', 'icu_folding', 'names_nysiis']
|
124
|
+
# }
|
125
|
+
# }
|
126
|
+
# }
|
127
|
+
# end
|
128
|
+
#
|
129
|
+
# See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
|
130
|
+
# for more details
|
131
|
+
#
|
40
132
|
def self.settings(params)
|
41
133
|
self._settings = params
|
42
134
|
end
|
43
135
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
136
|
+
# Perform import operation for every defined type
|
137
|
+
#
|
138
|
+
# UsersIndex.import
|
139
|
+
# UsersIndex.import refresh: false # to disable index refreshing after import
|
140
|
+
# UsersIndex.import suffix: Time.now.to_i # imports data to index with specified suffix if such is exists
|
141
|
+
# UsersIndex.import batch_size: 300 # import batch size
|
142
|
+
#
|
143
|
+
def self.import options = {}
|
144
|
+
objects = options.extract!(*type_names.map(&:to_sym))
|
145
|
+
types.map do |type|
|
146
|
+
args = [objects[type.type_name.to_sym], options.dup].reject(&:blank?)
|
147
|
+
type.import *args
|
148
|
+
end.all?
|
149
|
+
end
|
150
|
+
|
151
|
+
private
|
152
|
+
|
153
|
+
def self.build_index_name *args
|
154
|
+
options = args.extract_options!
|
155
|
+
[options[:prefix], args.first || index_name, options[:suffix]].reject(&:blank?).join(?_)
|
51
156
|
end
|
52
157
|
|
53
158
|
def self.settings_hash
|
@@ -70,14 +175,5 @@ module Chewy
|
|
70
175
|
def self.search_type
|
71
176
|
type_names
|
72
177
|
end
|
73
|
-
|
74
|
-
def self.import
|
75
|
-
types.all? { |t| t.import }
|
76
|
-
end
|
77
|
-
|
78
|
-
def self.reset
|
79
|
-
purge!
|
80
|
-
import
|
81
|
-
end
|
82
178
|
end
|
83
179
|
end
|