es_utils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f915708d508475ae6a6e1554cde67e5b3d56628c
4
+ data.tar.gz: 5866f75f14bbfc929a99b6c634d674929f87e9b7
5
+ SHA512:
6
+ metadata.gz: 9aac32dc765a3ad98e8b995f721aa6b447e3f928472f40a01817731c2238279b46142f93f51146ee1c720db483dfb2a7f870dbe91da823da07103679be37a4aa
7
+ data.tar.gz: 485c20eaecb405d70625968f61aa1c79d611a4974582a2ac4f56750830185e08e3c25fd23a8cc2ec4185d38b7a33e062803e417ce996db6d58713c8ad337ccb7
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in es_utils.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 sent-hil
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # es_utils
2
+
3
+ This library contains a bunch of improvements to `elasticsearch` ruby gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'es_utils'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install es_utils
18
+
19
+ ## Api
20
+ # this requires `elasticsearch`, so you don't have to again
21
+ require "es_utils"
22
+
23
+ ### kibana
24
+
25
+ # I find this time format `%FT%T%:z` or `2014-06-29T18:26:54-07:00`
26
+ # to be best for kibana. This library monkepatches `Time` to add
27
+ # `kibana` method.
28
+ Time.now.kibana
29
+
30
+ ### scroll_each
31
+
32
+ # `A scrolled search allows us to do an initial search and to keep
33
+ # pulling batches of results from Elasticsearch until there are no
34
+ # more results left. It’s a bit like a cursor in a traditional database.`
35
+ #
36
+ # http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/scan-scroll.html
37
+ #
38
+ # `scroll_each` abstracts away the bookkeeping logic. It takes `options` Hash
39
+ # and a block which is called on the results of each scroll.
40
+ client = Elasticsearch::Client.new
41
+ options = {
42
+ :index => <index_name>, # required
43
+ :scroll => "5m", # optional
44
+ :size => 10, # optional
45
+ :body => {:sort => "_id"} # optional
46
+ }
47
+
48
+ client.scroll_each options do |results|
49
+ results.each do |result|
50
+ # add your logic here
51
+ # example: puts result["_source"]
52
+ end
53
+ end
54
+
55
+ ### bulk_index
56
+
57
+ # `bulk_index` removes away need to pass index name and type as part
58
+ # of each document when doing a bulk indexing operation.
59
+ documents = [ {a: 1}, {a: 2}, {a: 3} ]
60
+ client.bulk_index(:index => "es_utils", :type => "doc", :refresh => true, :body => documents)
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/es_utils.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'es_utils/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "es_utils"
8
+ spec.version = EsUtils::VERSION
9
+ spec.authors = ["sent-hil"]
10
+ spec.email = ["me@sent-hil.com"]
11
+ spec.description = %q{Utils library for elasticsearch gem}
12
+ spec.summary = %q{This library contains a bunch of improvements and additions to elasticsearch ruby gem.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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 "elasticsearch"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,12 @@
1
+ require "time"
2
+
3
+ class Time
4
+ # Returns time in format `2014-06-29T18:26:54-07:00`
5
+ def kibana
6
+ self.strftime("%FT%T%:z")
7
+ end
8
+
9
+ def logstash
10
+ self.strftime("%Y.%m.%d")
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Actions
4
+ [:index, :update, :delete].each do |action|
5
+ define_method "bulk_#{action}" do |params={}|
6
+ body = params[:body].map do |raw|
7
+ {action => {
8
+ :_index => params[:index],
9
+ :_type => params[:type],
10
+ :data => raw
11
+ }}
12
+ end
13
+
14
+ bulk(params.merge(:body => body))
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Actions
4
+ def scroll_each(arguments={}, &blk)
5
+ valid_params = [
6
+ :index, :scroll, :size, :body
7
+ ]
8
+
9
+ params = Utils.__validate_and_extract_params arguments, valid_params
10
+
11
+ params[:body] ||= {}
12
+ params[:scroll] ||= "5m"
13
+ params[:size] ||= 100
14
+
15
+ s = search({
16
+ index: params[:index],
17
+ scroll: params[:scroll],
18
+ size: params[:size],
19
+ body: {sort: '_id'}.merge(params[:body]),
20
+ search_type: "scan",
21
+ })
22
+
23
+ loop do
24
+ s = scroll(:scroll_id => s["_scroll_id"], :scroll => params[:scroll])
25
+ results = s["hits"]["hits"]
26
+
27
+ break if results.empty?
28
+
29
+ blk.call(results)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module EsUtils
2
+ VERSION = "0.0.1"
3
+ end
data/lib/es_utils.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "elasticsearch"
2
+
3
+ require_relative "./es_utils/version"
4
+ require_relative "./es_utils/core_ext/time"
5
+ require_relative "./es_utils/ext/elasticsearch/scroll"
6
+ require_relative "./es_utils/ext/elasticsearch/bulk_index"
7
+
8
+ module EsUtils
9
+ end
@@ -0,0 +1,7 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe "Time#kibana" do
4
+ it "monkey patches `Time` to add `kibana` method" do
5
+ expect(Time.parse("2014.06.29").kibana).to eq("2014-06-29T00:00:00-07:00")
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require_relative "../../../spec_helper"
2
+
3
+ describe "Elasticsearach#bulk_index" do
4
+ after { client.indices.delete(:index => index_name) }
5
+
6
+ it "monkey patches `Elasticsearach` to add `bulk_index`" do
7
+ documents = [
8
+ {a: 1}, {a: 2}, {a: 3}
9
+ ]
10
+ client.bulk_index(:index => index_name, :type => "doc", :refresh => true, :body => documents)
11
+
12
+ raw = client.search(:index => index_name, :type => "doc")
13
+ result = raw["hits"]["hits"]
14
+
15
+ index = result.map {|x| x["_index"]}.uniq[0]
16
+ type = result.map {|x| x["_type"]}.uniq[0]
17
+
18
+ expect(index).to eq(index_name)
19
+ expect(type).to eq("doc")
20
+ expect(result.count).to eq(3)
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "../../../spec_helper"
2
+
3
+ describe "Elasticsearach#scroll" do
4
+ after { client.indices.delete(:index => index_name) }
5
+
6
+ it "monkey patches `Elasticsearach` to add `scroll` method" do
7
+ events = []
8
+ (1..100).to_a.each do |i|
9
+ events << {:index => {
10
+ :_index => "es_utils",
11
+ :_type => "document",
12
+ :data => {
13
+ :number => i
14
+ }
15
+ }}
16
+ end
17
+
18
+ client.bulk(:body => events, :refresh => true)
19
+
20
+ options = {
21
+ :index => index_name,
22
+ :scroll => "5m",
23
+ :size => 10,
24
+ :body => {:sort => "_id"}
25
+ }
26
+
27
+ output = []
28
+ client.scroll_each options do |results|
29
+ results.each do |result|
30
+ output << result["_source"]["number"]
31
+ end
32
+ end
33
+
34
+ expect((1..100).to_a).to eq(output.sort)
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "../lib/es_utils"
2
+
3
+ def client
4
+ @client ||= Elasticsearch::Client.new
5
+ end
6
+
7
+ def index_name
8
+ "es_utils"
9
+ end
10
+
11
+ require "pry"
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: es_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sent-hil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-08 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Utils library for elasticsearch gem
70
+ email:
71
+ - me@sent-hil.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - es_utils.gemspec
82
+ - lib/es_utils.rb
83
+ - lib/es_utils/core_ext/time.rb
84
+ - lib/es_utils/ext/elasticsearch/bulk_index.rb
85
+ - lib/es_utils/ext/elasticsearch/scroll.rb
86
+ - lib/es_utils/version.rb
87
+ - spec/es_utils/core_ext/time_spec.rb
88
+ - spec/es_utils/ext/elasticsearch/bulk_index_spec.rb
89
+ - spec/es_utils/ext/elasticsearch/scroll_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: This library contains a bunch of improvements and additions to elasticsearch
115
+ ruby gem.
116
+ test_files:
117
+ - spec/es_utils/core_ext/time_spec.rb
118
+ - spec/es_utils/ext/elasticsearch/bulk_index_spec.rb
119
+ - spec/es_utils/ext/elasticsearch/scroll_spec.rb
120
+ - spec/spec_helper.rb