elastic-rails 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 +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/elastic.gemspec +27 -0
- data/lib/elastic-rails.rb +1 -0
- data/lib/elastic.rb +44 -0
- data/lib/elastic/capabilities/aggregation_builder.rb +64 -0
- data/lib/elastic/capabilities/bool_query_builder.rb +74 -0
- data/lib/elastic/capabilities/context_handler.rb +31 -0
- data/lib/elastic/histogram.rb +49 -0
- data/lib/elastic/index.rb +113 -0
- data/lib/elastic/indexable.rb +25 -0
- data/lib/elastic/indexable_record.rb +45 -0
- data/lib/elastic/query.rb +39 -0
- data/lib/elastic/type.rb +121 -0
- data/lib/elastic/value_transform.rb +15 -0
- data/lib/elastic/version.rb +3 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4b7fcb53e08a79f313f4d3a4209f600ca621efd4
|
4
|
+
data.tar.gz: 082420e99a70e5d5ddd8b037e0f27eeec77b1363
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e2f2356d768d8ebf6b97aca4d6cc42929bc2c57716df72a03ddc416444aec1ec46a4a7bc60522d08a7ae581f906f4a402c1a5129d66f61472f2d8732df8db67f
|
7
|
+
data.tar.gz: 25a21fe7f5d976aae28e26b0e98b173ed38add01102b38793ff1c31b7b2786b5ecd7fde2ab57e7d6d471e42a47bf0ec9c0ea1caf0ced1c48d7a5c67753e6a7cc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at iobaixas@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Ignacio Baixas
|
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,41 @@
|
|
1
|
+
# Elastic
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/elastic`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'elastic'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install elastic
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/elastic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "elastic"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/elastic.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'elastic/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "elastic-rails"
|
8
|
+
spec.version = Elastic::VERSION
|
9
|
+
spec.authors = ["Ignacio Baixas"]
|
10
|
+
spec.email = ["ignacio@platan.us"]
|
11
|
+
|
12
|
+
spec.summary = %q{Elasticsearch integration for Ruby on Rails by Platanus}
|
13
|
+
spec.description = %q{Elasticsearch integration for Ruby on Rails by Platanus}
|
14
|
+
spec.homepage = "https://github.com/surbtc/elastic-rails"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "elasticsearch", "~> 1.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'elastic'
|
data/lib/elastic.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "elasticsearch"
|
2
|
+
require "elastic/version"
|
3
|
+
|
4
|
+
require "elastic/capabilities/aggregation_builder"
|
5
|
+
require "elastic/capabilities/bool_query_builder"
|
6
|
+
require "elastic/capabilities/context_handler"
|
7
|
+
|
8
|
+
require "elastic/index"
|
9
|
+
require "elastic/type"
|
10
|
+
require "elastic/query"
|
11
|
+
require "elastic/histogram"
|
12
|
+
require "elastic/value_transform"
|
13
|
+
require "elastic/indexable"
|
14
|
+
require "elastic/indexable_record"
|
15
|
+
|
16
|
+
module Elastic
|
17
|
+
extend self
|
18
|
+
|
19
|
+
def connect(_index = nil)
|
20
|
+
Elastic::Index.new api_client, (_index || default_index).to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def config
|
26
|
+
Rails.application.config_for(:elastic)
|
27
|
+
end
|
28
|
+
|
29
|
+
def default_index
|
30
|
+
config['index']
|
31
|
+
end
|
32
|
+
|
33
|
+
def api_client
|
34
|
+
@api_client ||= load_api_client
|
35
|
+
end
|
36
|
+
|
37
|
+
def load_api_client
|
38
|
+
uri = config['url'] ? URI(config['url']) : nil
|
39
|
+
Elasticsearch::Client.new(
|
40
|
+
host: uri ? uri.host : config['host'],
|
41
|
+
port: uri ? uri.port : config['port']
|
42
|
+
)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Elastic::Capabilities
|
2
|
+
module AggregationBuilder
|
3
|
+
def aggregate_in(_name)
|
4
|
+
set_context(:aggregate, _name)
|
5
|
+
end
|
6
|
+
|
7
|
+
def aggregate
|
8
|
+
set_context(:aggregate, nil)
|
9
|
+
end
|
10
|
+
|
11
|
+
def sum_of(_field)
|
12
|
+
with_context(:aggregate, :sum_of) do |name|
|
13
|
+
register_aggregation(name || _field, { "sum" => { "field" => _field } })
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def minimum_for(_field)
|
18
|
+
with_context(:aggregate, :minimum_for) do |name|
|
19
|
+
register_aggregation(name || _field, { "min" => { "field" => _field } })
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def maximum_for(_field)
|
24
|
+
with_context(:aggregate, :maximum_for) do |name|
|
25
|
+
register_aggregation(name || _field, { "max" => { "field" => _field } })
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def average_for(_field)
|
30
|
+
with_context(:aggregate, :average_for) do |name|
|
31
|
+
register_aggregation(name || _field, { "avg" => { "field" => _field } })
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def date_histogram_for(_field, resolution: '1d', &_block)
|
36
|
+
with_context(:aggregate, :date_histogram_for) do |name|
|
37
|
+
histogram = Elastic::Histogram.new _field, resolution
|
38
|
+
_block.call(histogram) if _block
|
39
|
+
register_aggregation(name || _field, histogram)
|
40
|
+
return histogram unless _block
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def aggregations
|
47
|
+
@aggregations ||= []
|
48
|
+
end
|
49
|
+
|
50
|
+
def register_aggregation(_name, _definition)
|
51
|
+
aggregations << [_name, _definition]
|
52
|
+
end
|
53
|
+
|
54
|
+
def render_aggregations_to(_to)
|
55
|
+
return if aggregations.length == 0
|
56
|
+
|
57
|
+
_to['aggs'] = aggs = {}
|
58
|
+
|
59
|
+
aggregations.each do |name, definition|
|
60
|
+
aggs[name] = definition.respond_to?(:render) ? definition.render : definition
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Elastic::Capabilities
|
2
|
+
module BoolQueryBuilder
|
3
|
+
def minimum_should_match
|
4
|
+
@minimum_should_match || 1
|
5
|
+
end
|
6
|
+
|
7
|
+
def minimum_should_match=(_value)
|
8
|
+
@minimum_should_match = _value
|
9
|
+
end
|
10
|
+
|
11
|
+
def should_have(_field)
|
12
|
+
set_context(:should_have, [should_parts, _field])
|
13
|
+
end
|
14
|
+
|
15
|
+
def must_have(_field)
|
16
|
+
set_context(:must_have, [must_parts, _field])
|
17
|
+
end
|
18
|
+
|
19
|
+
def in(*_terms)
|
20
|
+
with_context([:should_have, :must_have], :in) do |context, field|
|
21
|
+
raise ArgumentError, 'must provide at least one term' if _terms.length == 0
|
22
|
+
|
23
|
+
_terms = _terms.map { |t| transform_input(field, t) }
|
24
|
+
|
25
|
+
context << (_terms.length > 1 ?
|
26
|
+
{ "terms" => { field.to_s => _terms } } :
|
27
|
+
{ "term" => { field.to_s => _terms.first } }
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
alias :equal_to :in
|
33
|
+
|
34
|
+
def in_range(_range)
|
35
|
+
with_context([:should_have, :must_have], :in_range) do |context, field|
|
36
|
+
options = { }
|
37
|
+
|
38
|
+
case _range
|
39
|
+
when Range
|
40
|
+
options['gte'] = _range.begin
|
41
|
+
options['lt'] = _range.end if _range.exclude_end?
|
42
|
+
options['lte'] = _range.end if !_range.exclude_end?
|
43
|
+
when Hash
|
44
|
+
[:gte, :gt, :lte, :lt].each do |opt|
|
45
|
+
options[opt.to_s] = _range[opt] if _range.key? opt
|
46
|
+
end
|
47
|
+
else
|
48
|
+
raise ArgumentError, 'must provide a range or a set of options'
|
49
|
+
end
|
50
|
+
|
51
|
+
context << { "range" => { field.to_s => options } }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def must_parts
|
58
|
+
@must_parts ||= []
|
59
|
+
end
|
60
|
+
|
61
|
+
def should_parts
|
62
|
+
@should_parts ||= []
|
63
|
+
end
|
64
|
+
|
65
|
+
def render_bool_query_to(_to)
|
66
|
+
return if should_parts.length == 0 && must_parts.length == 0
|
67
|
+
|
68
|
+
bool = _to['bool'] = {}
|
69
|
+
bool['must'] = must_parts if must_parts.length > 0
|
70
|
+
bool['should'] = should_parts if should_parts.length > 0
|
71
|
+
bool['minimum_should_match'] = minimum_should_match if should_parts.length > 0
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Elastic::Capabilities
|
2
|
+
module ContextHandler
|
3
|
+
private
|
4
|
+
|
5
|
+
def set_context(_name, _data)
|
6
|
+
unless @context_name.nil?
|
7
|
+
raise ArgumentError, "#{_name} should not be called right after #{@context_name}"
|
8
|
+
end
|
9
|
+
|
10
|
+
@context_name = _name.to_sym
|
11
|
+
@context_data = _data
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def with_context(_names, _caller)
|
16
|
+
_names = [_names] unless _names.is_a? Array
|
17
|
+
_names = _names.map(&:to_sym)
|
18
|
+
|
19
|
+
if @context_name.nil?
|
20
|
+
raise ArgumentError, "#{_caller} should be called after #{_names.join(' or ')}"
|
21
|
+
elsif !_names.include? @context_name
|
22
|
+
raise ArgumentError, "#{_caller} should not be called after #{@context_name}"
|
23
|
+
end
|
24
|
+
|
25
|
+
yield @context_data
|
26
|
+
return self
|
27
|
+
ensure
|
28
|
+
@context_name = @context_data = nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Elastic
|
2
|
+
class Histogram
|
3
|
+
include Capabilities::ContextHandler
|
4
|
+
include Capabilities::AggregationBuilder
|
5
|
+
|
6
|
+
attr_accessor :resolution
|
7
|
+
|
8
|
+
def initialize(_time_field, _resolution)
|
9
|
+
@time_field = _time_field
|
10
|
+
@resolution = _resolution
|
11
|
+
end
|
12
|
+
|
13
|
+
def open_for(_field)
|
14
|
+
with_context(:aggregate, :open_for) do |name|
|
15
|
+
register_aggregation(name, {
|
16
|
+
top_hits: {
|
17
|
+
sort: [{ @time_field => { order: "asc" } }],
|
18
|
+
_source: { include: [ _field ] },
|
19
|
+
size: 1
|
20
|
+
}
|
21
|
+
})
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def close_for(_field)
|
26
|
+
with_context(:aggregate, :close_for) do |name|
|
27
|
+
register_aggregation(name, {
|
28
|
+
top_hits: {
|
29
|
+
sort: [{ @time_field => { order: "desc" } }],
|
30
|
+
_source: { include: [ _field ] },
|
31
|
+
size: 1
|
32
|
+
}
|
33
|
+
})
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def render
|
38
|
+
json = {
|
39
|
+
date_histogram: {
|
40
|
+
field: @time_field,
|
41
|
+
interval: @resolution,
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
render_aggregations_to json
|
46
|
+
json
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module Elastic
|
2
|
+
class Index
|
3
|
+
attr_reader :api_client, :index_name
|
4
|
+
|
5
|
+
def initialize(_api_client, _index_name)
|
6
|
+
@api_client = _api_client
|
7
|
+
@index_name = _index_name
|
8
|
+
# TODO: multiple index mode (one per type)
|
9
|
+
end
|
10
|
+
|
11
|
+
def drop
|
12
|
+
api_client.indices.delete index: index_name # TODO: add +'*' in multi index mode
|
13
|
+
clear_index_cache
|
14
|
+
end
|
15
|
+
|
16
|
+
def refresh
|
17
|
+
ensure_index index_name
|
18
|
+
api_client.indices.refresh index: index_name # TODO: add +'*' in multi index mode
|
19
|
+
end
|
20
|
+
|
21
|
+
def index(_type, _data, mapping: nil)
|
22
|
+
ensure_type(_type)
|
23
|
+
ensure_mapping(_type, mapping) unless mapping.nil?
|
24
|
+
|
25
|
+
options = {
|
26
|
+
index: index_name,
|
27
|
+
type: _type,
|
28
|
+
body: _data
|
29
|
+
}
|
30
|
+
|
31
|
+
options[:id] = _data[:id] if _data.key? :id
|
32
|
+
|
33
|
+
api_client.index(options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def exists?(_type)
|
37
|
+
api_client.indices.exists_type build_options(_type)
|
38
|
+
end
|
39
|
+
|
40
|
+
def clear(_type, _query = nil)
|
41
|
+
if _query.nil?
|
42
|
+
return unless exists? _type
|
43
|
+
api_client.indices.delete_mapping build_options(_type)
|
44
|
+
clear_type_cache(_type)
|
45
|
+
else
|
46
|
+
ensure_type(_type)
|
47
|
+
api_client.delete_by_query build_options(_type, q: _query)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def query(_type, _query)
|
52
|
+
ensure_type(_type)
|
53
|
+
api_client.search build_options(_type, body: _query)
|
54
|
+
end
|
55
|
+
|
56
|
+
def count(_type, _query = nil)
|
57
|
+
ensure_type(_type)
|
58
|
+
r = api_client.count build_options(_type, body: _query)
|
59
|
+
r["count"]
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def self.clear_cache
|
65
|
+
@@index_cache = nil
|
66
|
+
@@mapping_cache = nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def index_cache
|
70
|
+
@@index_cache ||= {}
|
71
|
+
end
|
72
|
+
|
73
|
+
def mapping_cache
|
74
|
+
@@mapping_cache ||= {}
|
75
|
+
end
|
76
|
+
|
77
|
+
def build_options(_type, _options = {})
|
78
|
+
# TODO: in multiple index mode use { index: index_name + _type }
|
79
|
+
{ index: index_name, type: _type }.merge! _options
|
80
|
+
end
|
81
|
+
|
82
|
+
def clear_index_cache
|
83
|
+
clear_cache
|
84
|
+
end
|
85
|
+
|
86
|
+
def clear_type_cache(_type)
|
87
|
+
mapping_cache[type_key(_type)]
|
88
|
+
end
|
89
|
+
|
90
|
+
def ensure_index(_name)
|
91
|
+
return if index_cache[_name]
|
92
|
+
return if api_client.indices.exists? index: _name
|
93
|
+
api_client.indices.create index: _name
|
94
|
+
api_client.cluster.health wait_for_status: 'yellow'
|
95
|
+
end
|
96
|
+
|
97
|
+
def ensure_type(_type)
|
98
|
+
ensure_index(index_name)
|
99
|
+
end
|
100
|
+
|
101
|
+
def ensure_mapping(_type, _mapping)
|
102
|
+
return if mapping_cache[type_key(_type)] == _mapping
|
103
|
+
api_client.indices.put_mapping index: index_name, type: _type, body: _mapping
|
104
|
+
mapping_cache[type_key(_type)] = _mapping
|
105
|
+
end
|
106
|
+
|
107
|
+
def type_key(_type)
|
108
|
+
index_name + '/' + _type
|
109
|
+
end
|
110
|
+
|
111
|
+
clear_cache
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Elastic
|
2
|
+
module Indexable
|
3
|
+
def self.included(_base)
|
4
|
+
_base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def index_class
|
9
|
+
@index_class = (to_s + 'Index').constantize
|
10
|
+
end
|
11
|
+
|
12
|
+
def query
|
13
|
+
Elastic::Query.new index_class
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def index_now
|
18
|
+
self.class.index_class.store self
|
19
|
+
end
|
20
|
+
|
21
|
+
def index_later
|
22
|
+
index_now
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Elastic
|
2
|
+
module IndexableRecord
|
3
|
+
def self.included(_base)
|
4
|
+
_base.include Indexable
|
5
|
+
_base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def index(_options)
|
10
|
+
index_depends(_options.delete(:depends))
|
11
|
+
on = _options.delete(:on)
|
12
|
+
|
13
|
+
if on == :create
|
14
|
+
index_on_create _options
|
15
|
+
elsif on == :save
|
16
|
+
index_on_save _options
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def index_on_create(_options = {})
|
21
|
+
after_create(_options) { index_later }
|
22
|
+
end
|
23
|
+
|
24
|
+
def index_on_save(_options = {})
|
25
|
+
after_save(_options) { index_later }
|
26
|
+
end
|
27
|
+
|
28
|
+
def index_depends(_depends)
|
29
|
+
@index_depends = _depends
|
30
|
+
end
|
31
|
+
|
32
|
+
def reindex
|
33
|
+
index_class.clear
|
34
|
+
|
35
|
+
scope = self
|
36
|
+
scope = self.includes(@index_depends) if @index_depends
|
37
|
+
scope.find_each { |r| index_class.store(r) } # TODO: index_many
|
38
|
+
end
|
39
|
+
|
40
|
+
def search
|
41
|
+
# TODO: index_class.query.decorate blabla
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Elastic
|
2
|
+
class Query
|
3
|
+
include Capabilities::ContextHandler
|
4
|
+
include Capabilities::BoolQueryBuilder
|
5
|
+
include Capabilities::AggregationBuilder
|
6
|
+
|
7
|
+
attr_accessor :type, :size, :input_transform
|
8
|
+
|
9
|
+
def initialize(_type, minimum_should_match: 1, size: nil)
|
10
|
+
@type = _type
|
11
|
+
@size = size
|
12
|
+
self.minimum_should_match = minimum_should_match
|
13
|
+
end
|
14
|
+
|
15
|
+
def render
|
16
|
+
search = {}
|
17
|
+
search['size'] = @size unless @size.nil?
|
18
|
+
render_query_to search
|
19
|
+
render_aggregations_to search
|
20
|
+
search
|
21
|
+
end
|
22
|
+
|
23
|
+
def run
|
24
|
+
type.query(render)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def render_query_to(_search)
|
30
|
+
query = {}
|
31
|
+
render_bool_query_to query
|
32
|
+
_search['query'] = query if query.length > 0
|
33
|
+
end
|
34
|
+
|
35
|
+
def transform_input(_name, _value)
|
36
|
+
type.prepare_field_for_query _name, _value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/elastic/type.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
module Elastic
|
2
|
+
class Type
|
3
|
+
MAPPING_OPTIONS = [ :type, :analyzer, :boost, :coerce, :copy_to, :doc_values, :dynamic,
|
4
|
+
:enabled, :fielddata, :geohash, :geohash_precision, :geohash_prefix, :format, :ignore_above,
|
5
|
+
:ignore_malformed, :include_in_all, :index_options, :lat_lon, :index, :fields, :norms,
|
6
|
+
:null_value, :position_increment_gap, :properties, :search_analyzer, :similarity, :store,
|
7
|
+
:term_vector ]
|
8
|
+
|
9
|
+
def self.connection
|
10
|
+
@connection ||= Elastic.connect index
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.index=(_value)
|
14
|
+
@index = _value
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.index
|
18
|
+
@index
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.type_name=(_value)
|
22
|
+
@type_name = _value
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.type_name
|
26
|
+
@type_name ||= to_s.underscore
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.fields(*_fields)
|
30
|
+
raise ArgumentError, 'must provide at least a field name' if _fields.length == 0
|
31
|
+
|
32
|
+
options = {}
|
33
|
+
options = _fields.pop if _fields.last.is_a? Hash
|
34
|
+
|
35
|
+
_fields.each do |name|
|
36
|
+
register_field(name, options)
|
37
|
+
register_mapping(name, options)
|
38
|
+
register_transform(name, options[:transform]) if options.key? :transform
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.field(_field, _options)
|
43
|
+
fields(_field, _options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.store(_data, _options = {})
|
47
|
+
connection.index(type_name, new(_data).render, mapping: { type_name => type_mapping })
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.store_bulk(_collection, _options = {})
|
51
|
+
# TODO
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.query(_query)
|
55
|
+
connection.query type_name, _query
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.clear(_options = {})
|
59
|
+
connection.clear type_name
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.prepare_field_for_query(_field, _value)
|
63
|
+
transform = transforms[_field.to_sym]
|
64
|
+
transform.nil? ? _value : transform.apply(_value)
|
65
|
+
end
|
66
|
+
|
67
|
+
attr_reader :object
|
68
|
+
|
69
|
+
def initialize(_object)
|
70
|
+
@object = _object
|
71
|
+
end
|
72
|
+
|
73
|
+
def render
|
74
|
+
document = {}
|
75
|
+
document[:id] = fetch_object_property(:id) if object_has_property?(:id)
|
76
|
+
|
77
|
+
self.class.type_fields.each do |name, options|
|
78
|
+
document[name] = fetch_object_property(name)
|
79
|
+
end
|
80
|
+
|
81
|
+
document
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def self.type_fields
|
87
|
+
@type_fields ||= []
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.type_mapping
|
91
|
+
@type_mapping ||= { "properties" => { } }
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.transforms
|
95
|
+
@transforms ||= { }
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.register_field(_name, _options)
|
99
|
+
type_fields << [_name.to_sym, _options]
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.register_mapping(_name, _options)
|
103
|
+
field = _options.slice(*MAPPING_OPTIONS)
|
104
|
+
field.merge! type: 'string', index: 'not_analyzed' if _options[:type].try(:to_sym) == :term
|
105
|
+
type_mapping["properties"][_name.to_s] = field if field.length > 0
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.register_transform(_name, _transform)
|
109
|
+
transforms[_name.to_sym] = ValueTransform.new self, _transform
|
110
|
+
end
|
111
|
+
|
112
|
+
def object_has_property?(_name)
|
113
|
+
self.respond_to?(_name) || object.respond_to?(_name)
|
114
|
+
end
|
115
|
+
|
116
|
+
def fetch_object_property(_name)
|
117
|
+
value = self.respond_to?(_name) ? public_send(_name) : object.public_send(_name)
|
118
|
+
self.class.prepare_field_for_query _name, value
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Elastic
|
2
|
+
class ValueTransform
|
3
|
+
attr_reader :context, :transform
|
4
|
+
|
5
|
+
def initialize(_context, _transform)
|
6
|
+
@context = _context
|
7
|
+
@transform = _transform
|
8
|
+
end
|
9
|
+
|
10
|
+
def apply(_value)
|
11
|
+
return _value.public_send @transform if @transform.is_a? String
|
12
|
+
_value.instance_exec(&@transform)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elastic-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ignacio Baixas
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: elasticsearch
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Elasticsearch integration for Ruby on Rails by Platanus
|
70
|
+
email:
|
71
|
+
- ignacio@platan.us
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- elastic.gemspec
|
87
|
+
- lib/elastic-rails.rb
|
88
|
+
- lib/elastic.rb
|
89
|
+
- lib/elastic/capabilities/aggregation_builder.rb
|
90
|
+
- lib/elastic/capabilities/bool_query_builder.rb
|
91
|
+
- lib/elastic/capabilities/context_handler.rb
|
92
|
+
- lib/elastic/histogram.rb
|
93
|
+
- lib/elastic/index.rb
|
94
|
+
- lib/elastic/indexable.rb
|
95
|
+
- lib/elastic/indexable_record.rb
|
96
|
+
- lib/elastic/query.rb
|
97
|
+
- lib/elastic/type.rb
|
98
|
+
- lib/elastic/value_transform.rb
|
99
|
+
- lib/elastic/version.rb
|
100
|
+
homepage: https://github.com/surbtc/elastic-rails
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.6.4
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Elasticsearch integration for Ruby on Rails by Platanus
|
124
|
+
test_files: []
|
125
|
+
has_rdoc:
|