elasticsearch-client 0.0.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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in elasticsearch-client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Jonathan Hoyt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # ElasticSearch ruby client
2
+
3
+ ElasticSearch ruby module.
4
+
5
+ ## Usage
6
+
7
+ Add to Gemfile
8
+
9
+ gem 'elasticsearch-client', :require => 'elasticsearch'
10
+
11
+ Create connection:
12
+
13
+ index = 'twitter'
14
+ url = 'http://localhost:9200'
15
+ es = ElasticSearch::Index.new(index, url)
16
+
17
+ Index a document:
18
+
19
+ doc = {:id => 'abcd', :foo => 'bar'}
20
+ es.add(index, doc[:id], doc)
21
+
22
+ Get a document:
23
+
24
+ id = '1234'
25
+ es.mget(id)
26
+
27
+ Search:
28
+
29
+ query = {
30
+ :query => {
31
+ :bool => {
32
+ :must => {
33
+ :query_string => {
34
+ :default_field => '_all',
35
+ :query => 'foobar!',
36
+ }
37
+ }
38
+ }
39
+ }
40
+ }
41
+ es.search(index, query)
42
+
43
+ Remove record:
44
+
45
+ es.remove_by_query(index, :term => {:id => id})
46
+
47
+ ## Note on Patches/Pull Requests
48
+
49
+ * Fork the project.
50
+ * Make your feature addition or bug fix.
51
+ * Add tests for it. This is important so we don't break it in a future version unintentionally.
52
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine, but bump version in a commit by itself so we can ignore when we pull)
53
+ * Send us a pull request. Bonus points for topic branches.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ desc "Run the specs"
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.rspec_opts = %w(-fs --color)
7
+ end
8
+
9
+ task :default => :spec
10
+ task :test => :spec
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "elasticsearch/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "elasticsearch-client"
7
+ s.version = ElasticSearch::VERSION
8
+ s.authors = ["Jonathan Hoyt"]
9
+ s.email = ["hoyt@github.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{ElasticSearch ruby client.}
12
+ s.description = %q{ElasticSearch ruby client.}
13
+
14
+ s.rubyforge_project = "elasticsearch-client"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'faraday', '~> 0.7.6'
22
+ s.add_dependency 'yajl-ruby', '~> 1.1.0'
23
+
24
+ s.add_development_dependency 'rake'
25
+ s.add_development_dependency 'rspec', '~> 2.8.0'
26
+ end
@@ -0,0 +1,188 @@
1
+ require "elasticsearch/version"
2
+ require 'faraday'
3
+ require 'yajl'
4
+ require 'time'
5
+
6
+ module ElasticSearch
7
+ class JSONResponse < Faraday::Response::Middleware
8
+ def parse(body)
9
+ Yajl.load body
10
+ end
11
+ end
12
+
13
+ class Error < StandardError
14
+ end
15
+
16
+ def self.get_connection(server)
17
+ return unless server
18
+
19
+ Faraday.new(:url => server) do |builder|
20
+ # TODO: add timeout middleware
21
+ builder.request :json
22
+ # builder.response :logger
23
+ builder.use JSONResponse
24
+ builder.adapter :excon
25
+ end
26
+ end
27
+
28
+ def self.available?
29
+ conn = get_connection
30
+ resp = conn.get '/'
31
+ resp.status == 200
32
+ end
33
+
34
+ # Object to represent an index in elasticsearch
35
+ class Index
36
+ def initialize(name, server)
37
+ @name = name
38
+ @conn = ElasticSearch.get_connection(server)
39
+ end
40
+
41
+ # Some helpers for making REST calls to elasticsearch
42
+ %w[ get post put delete ].each do |method|
43
+ class_eval <<-EOC, __FILE__, __LINE__
44
+ def #{method}(*args, &blk)
45
+ raise Error, "no connection" unless @conn
46
+ resp = @conn.#{method}(*args, &blk)
47
+ raise Error, "elasticsearch server is offline or not accepting requests" if resp.status == 0
48
+ raise Error, resp.body['error'] if resp.body['error']
49
+ @last_resp = resp
50
+ resp.body
51
+ end
52
+ EOC
53
+ end
54
+
55
+ # Force a refresh of this index
56
+ #
57
+ # This basically tells elasticsearch to flush it's buffers
58
+ # but not clear caches (unlike a commit in Solr)
59
+ # "Commits" happen automatically and are managed by elasticsearch
60
+ #
61
+ # Returns a hash, the parsed response body from elasticsearch
62
+ def refresh
63
+ post "/#{@name}/_refresh"
64
+ end
65
+
66
+ def bulk(data)
67
+ return if data.empty?
68
+ body = post "/#{@name}/_bulk", data
69
+ raise Error, "bulk import got HTTP #{@last_resp.status} response" if @last_resp.status != 200
70
+ end
71
+
72
+ # Grab a bunch of items from this index
73
+ #
74
+ # type - the type to pull from
75
+ # ids - an Array of ids to fetch
76
+ #
77
+ # Returns a hash, the parsed response body from elasticsearch
78
+ def mget(type, ids)
79
+ get do |req|
80
+ req.url "#{@name}/#{type}/_mget"
81
+ req.body = {'ids' => ids}
82
+ end
83
+ end
84
+
85
+ # Search this index using a post body
86
+ #
87
+ # types - the type or types (comma seperated) to search
88
+ # options - options hash for this search request
89
+ #
90
+ # Returns a hash, the parsed response body from elasticsearch
91
+ def search(types, options)
92
+ get do |req|
93
+ req.url "#{@name}/#{types}/_search"
94
+ req.body = options
95
+ end
96
+ end
97
+
98
+ # Search this index using a query string
99
+ #
100
+ # types - the type or types (comma seperated) to search
101
+ # query - the search query string
102
+ # options - options hash for this search request (optional)
103
+ #
104
+ # Returns a hash, the parsed response body from elasticsearch
105
+ def query(types, query, options=nil)
106
+ query = {'q' => query} if query.is_a?(String)
107
+ get do |req|
108
+ req.url "#{@name}/#{types}/_search", query
109
+ req.body = options if options
110
+ end
111
+ end
112
+
113
+ # Count results using a query string
114
+ #
115
+ # types - the type or types (comma seperated) to search
116
+ # query - the search query string
117
+ # options - options hash for this search request (optional)
118
+ #
119
+ # Returns a hash, the parsed response body from elasticsearch
120
+ def count(types, query, options=nil)
121
+ query = {'q' => query} if query.is_a?(String)
122
+ get do |req|
123
+ req.url "#{@name}/#{types}/_count", query
124
+ req.body = options if options
125
+ end
126
+ end
127
+
128
+ # Add a document to this index
129
+ #
130
+ # type - the type of this document
131
+ # id - the unique identifier for this document
132
+ # doc - the document to be indexed
133
+ #
134
+ # Returns a hash, the parsed response body from elasticsearch
135
+ def add(type, id, doc, params={})
136
+ doc.each do |key, val|
137
+ # make sure dates are in a consistent format for indexing
138
+ doc[key] = val.iso8601 if val.respond_to?(:iso8601)
139
+ end
140
+
141
+ put do |req|
142
+ req.url "/#{@name}/#{type}/#{id}", params
143
+ req.body = doc
144
+ end
145
+ end
146
+
147
+ # Remove a document from this index
148
+ #
149
+ # type - the type of document to be removed
150
+ # id - the unique identifier of the document to be removed
151
+ #
152
+ # Returns a hash, the parsed response body from elasticsearch
153
+ def remove(type, id)
154
+ delete do |req|
155
+ req.url "#{@name}/#{type}/#{id}"
156
+ end
157
+ end
158
+
159
+ # Remove a collection of documents matched by a query
160
+ #
161
+ # types - the type or types to query
162
+ # options - the search options hash
163
+ #
164
+ # Returns a hash, the parsed response body from elasticsearch
165
+ def remove_by_query(types, options)
166
+ delete do |req|
167
+ req.url "#{@name}/#{types}/_query"
168
+ req.body = options
169
+ end
170
+ end
171
+
172
+ # Create a new index in elasticsearch
173
+ #
174
+ # name - the name of the index to be created
175
+ # create_options - a hash of index creation options
176
+ #
177
+ # Returns a new ElasticSearch::Index instance
178
+ def self.create(name, create_options={})
179
+ conn = ElasticSearch.get_connection
180
+ conn.put do |req|
181
+ req.url "/#{name}"
182
+ req.body = create_options
183
+ end
184
+
185
+ new(name)
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,3 @@
1
+ module ElasticSearch
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'elasticsearch'
3
+
4
+ describe ElasticSearch::JSONResponse do
5
+ describe "#parse" do
6
+ subject { ElasticSearch::JSONResponse.new }
7
+
8
+ it "parses json" do
9
+ subject.parse('{"good":"bad"}').should == {"good" => "bad"}
10
+ end
11
+ end
12
+ end
13
+
14
+ describe ElasticSearch::Error do
15
+ subject { ElasticSearch::Error.new }
16
+
17
+ it "subclasses StandardError" do
18
+ subject.methods.should include("backtrace")
19
+ subject.methods.should include("exception")
20
+ subject.methods.should include("message")
21
+ subject.methods.should include("set_backtrace")
22
+ subject.methods.should include("to_str")
23
+ end
24
+ end
25
+
26
+ describe ElasticSearch do
27
+ let(:server) { '127.0.0.1' }
28
+
29
+ describe ".get_connection" do
30
+ let(:subject) { ElasticSearch.get_connection(server) }
31
+
32
+ it "returns immediately without server" do
33
+ Faraday.should_not_receive(:new)
34
+ ElasticSearch.get_connection(nil)
35
+ end
36
+
37
+ it "creates faraday instance with server" do
38
+ Faraday.should_receive(:new).with(:url => server)
39
+ subject
40
+ end
41
+
42
+ it "returns faraday instance" do
43
+ subject.should be_instance_of(Faraday::Connection)
44
+ end
45
+ end
46
+
47
+ describe ".available?" do
48
+ context "connection up" do
49
+ it "returns true" do
50
+ conn = stub(:get => stub(:status => 200))
51
+ ElasticSearch.stub(:get_connection => conn)
52
+ ElasticSearch.available?.should be_true
53
+ end
54
+ end
55
+
56
+ context "connection down" do
57
+ it "returns false" do
58
+ conn = stub(:get => stub(:status => 500))
59
+ ElasticSearch.stub(:get_connection => conn)
60
+ ElasticSearch.available?.should be_false
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler.require :default, :test
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticsearch-client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jonathan Hoyt
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-06 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: faraday
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 15
29
+ segments:
30
+ - 0
31
+ - 7
32
+ - 6
33
+ version: 0.7.6
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: yajl-ruby
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 19
45
+ segments:
46
+ - 1
47
+ - 1
48
+ - 0
49
+ version: 1.1.0
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: rake
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ type: :development
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: rspec
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 47
75
+ segments:
76
+ - 2
77
+ - 8
78
+ - 0
79
+ version: 2.8.0
80
+ type: :development
81
+ version_requirements: *id004
82
+ description: ElasticSearch ruby client.
83
+ email:
84
+ - hoyt@github.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files: []
90
+
91
+ files:
92
+ - .gitignore
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - elasticsearch-client.gemspec
98
+ - lib/elasticsearch.rb
99
+ - lib/elasticsearch/version.rb
100
+ - spec/elasticsearch_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: ""
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project: elasticsearch-client
131
+ rubygems_version: 1.8.10
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: ElasticSearch ruby client.
135
+ test_files:
136
+ - spec/elasticsearch_spec.rb
137
+ - spec/spec_helper.rb