elasticsearch-api 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -3
- data/Rakefile +4 -6
- data/elasticsearch-api.gemspec +2 -1
- data/lib/elasticsearch/api.rb +1 -0
- data/lib/elasticsearch/api/actions/bulk.rb +9 -6
- data/lib/elasticsearch/api/actions/count.rb +4 -0
- data/lib/elasticsearch/api/actions/delete_by_query.rb +3 -1
- data/lib/elasticsearch/api/actions/indices/analyze.rb +1 -1
- data/lib/elasticsearch/api/actions/indices/delete_template.rb +6 -0
- data/lib/elasticsearch/api/actions/indices/get_field_mapping.rb +46 -0
- data/lib/elasticsearch/api/version.rb +1 -1
- data/test/integration/yaml_test_runner.rb +21 -4
- data/test/test_helper.rb +4 -0
- data/test/unit/count_test.rb +2 -2
- data/test/unit/delete_by_query_test.rb +9 -0
- data/test/unit/indices/delete_template_test.rb +8 -0
- data/test/unit/indices/get_field_mapping_test.rb +44 -0
- data/test/unit/utils_test.rb +1 -1
- metadata +21 -2
data/README.md
CHANGED
@@ -49,7 +49,7 @@ client = Elasticsearch::Client.new log: true
|
|
49
49
|
client.index index: 'myindex', type: 'mytype', id: 1, body: { title: 'Test' }
|
50
50
|
# => {"ok"=>true, "_index"=>"myindex", ...}
|
51
51
|
|
52
|
-
client.search body: { query: { match: { title: 'test' } } }
|
52
|
+
client.search index: 'myindex', body: { query: { match: { title: 'test' } } }
|
53
53
|
# => {"took"=>2, ..., "hits"=>{"total":5, ...}}
|
54
54
|
```
|
55
55
|
|
@@ -105,7 +105,7 @@ taking advantage of JSON builders such as [JBuilder](https://github.com/rails/jb
|
|
105
105
|
```ruby
|
106
106
|
require 'jbuilder'
|
107
107
|
|
108
|
-
|
108
|
+
query = Jbuilder.encode do |json|
|
109
109
|
json.query do
|
110
110
|
json.match do
|
111
111
|
json.title do
|
@@ -116,7 +116,7 @@ json = Jbuilder.encode do |json|
|
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
-
client.search index: 'myindex', body:
|
119
|
+
client.search index: 'myindex', body: query
|
120
120
|
|
121
121
|
# 2013-06-25 09:56:05 +0200: GET http://localhost:9200/myindex/_search [status:200, request:0.015s, query:0.011s]
|
122
122
|
# 2013-06-25 09:56:05 +0200: > {"query":{"match":{"title":{"query":"test 1","operator":"and"}}}}
|
data/Rakefile
CHANGED
@@ -43,17 +43,15 @@ namespace :test do
|
|
43
43
|
desc "Start Elasticsearch nodes for tests"
|
44
44
|
task :start do
|
45
45
|
$LOAD_PATH << File.expand_path('../../elasticsearch-transport/lib', __FILE__) << File.expand_path('../test', __FILE__)
|
46
|
-
require 'elasticsearch/
|
47
|
-
|
48
|
-
Elasticsearch::TestCluster.start
|
46
|
+
require 'elasticsearch/extensions/test/cluster'
|
47
|
+
Elasticsearch::Extensions::Test::Cluster.start
|
49
48
|
end
|
50
49
|
|
51
50
|
desc "Stop Elasticsearch nodes for tests"
|
52
51
|
task :stop do
|
53
52
|
$LOAD_PATH << File.expand_path('../../elasticsearch-transport/lib', __FILE__) << File.expand_path('../test', __FILE__)
|
54
|
-
require 'elasticsearch/
|
55
|
-
|
56
|
-
Elasticsearch::TestCluster.stop
|
53
|
+
require 'elasticsearch/extensions/test/cluster'
|
54
|
+
Elasticsearch::Extensions::Test::Cluster.stop
|
57
55
|
end
|
58
56
|
end
|
59
57
|
end
|
data/elasticsearch-api.gemspec
CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
|
28
28
|
s.add_development_dependency "elasticsearch"
|
29
29
|
s.add_development_dependency "elasticsearch-transport"
|
30
|
+
s.add_development_dependency "elasticsearch-extensions"
|
30
31
|
|
31
32
|
s.add_development_dependency "ansi"
|
32
33
|
s.add_development_dependency "shoulda-context"
|
@@ -42,7 +43,6 @@ Gem::Specification.new do |s|
|
|
42
43
|
s.add_development_dependency "jbuilder"
|
43
44
|
s.add_development_dependency "jsonify"
|
44
45
|
s.add_development_dependency "hashie"
|
45
|
-
s.add_development_dependency "escape_utils"
|
46
46
|
|
47
47
|
# Prevent unit test failures on Ruby 1.8
|
48
48
|
if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
|
@@ -51,6 +51,7 @@ Gem::Specification.new do |s|
|
|
51
51
|
end
|
52
52
|
|
53
53
|
if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
|
54
|
+
s.add_development_dependency "escape_utils"
|
54
55
|
s.add_development_dependency "simplecov"
|
55
56
|
s.add_development_dependency "cane"
|
56
57
|
s.add_development_dependency "require-prof"
|
data/lib/elasticsearch/api.rb
CHANGED
@@ -33,13 +33,15 @@ module Elasticsearch
|
|
33
33
|
#
|
34
34
|
# ]
|
35
35
|
#
|
36
|
-
# @option arguments [String]
|
37
|
-
# @option arguments [String]
|
36
|
+
# @option arguments [String] :index Default index for items which don't provide one
|
37
|
+
# @option arguments [String] :type Default document type for items which don't provide one
|
38
38
|
# @option arguments [Array<Hash>] :body An array of operations to perform, each operation is a Hash
|
39
|
-
# @option arguments [String]
|
39
|
+
# @option arguments [String] :consistency Explicit write consistency setting for the operation
|
40
|
+
# (options: one, quorum, all)
|
40
41
|
# @option arguments [Boolean] :refresh Refresh the index after performing the operation
|
41
|
-
# @option arguments [String]
|
42
|
-
# @option arguments [String]
|
42
|
+
# @option arguments [String] :replication Explicitely set the replication type (options: sync, async)
|
43
|
+
# @option arguments [String] :type Default document type for items which don't provide one
|
44
|
+
# @option arguments [Time] :timeout Explicit operation timeout
|
43
45
|
#
|
44
46
|
# @return [Hash] Deserialized Elasticsearch response
|
45
47
|
#
|
@@ -50,7 +52,8 @@ module Elasticsearch
|
|
50
52
|
:consistency,
|
51
53
|
:refresh,
|
52
54
|
:replication,
|
53
|
-
:type
|
55
|
+
:type,
|
56
|
+
:timeout ]
|
54
57
|
|
55
58
|
method = 'POST'
|
56
59
|
path = Utils.__pathify Utils.__escape(arguments[:index]), Utils.__escape(arguments[:type]), '_bulk'
|
@@ -12,6 +12,10 @@ module Elasticsearch
|
|
12
12
|
#
|
13
13
|
# client.count index: 'myindex'
|
14
14
|
#
|
15
|
+
# @example Get the number of documents matching a specific query
|
16
|
+
#
|
17
|
+
# index: 'my_index', body: { filtered: { filter: { terms: { foo: ['bar'] } } } }
|
18
|
+
#
|
15
19
|
# @option arguments [List] :index A comma-separated list of indices to restrict the results
|
16
20
|
# @option arguments [List] :type A comma-separated list of types to restrict the results
|
17
21
|
# @option arguments [Hash] :body A query to restrict the results (optional)
|
@@ -51,7 +51,9 @@ module Elasticsearch
|
|
51
51
|
:timeout ]
|
52
52
|
|
53
53
|
method = 'DELETE'
|
54
|
-
path = Utils.__pathify
|
54
|
+
path = Utils.__pathify Utils.__listify(arguments[:index]),
|
55
|
+
Utils.__listify(arguments[:type]),
|
56
|
+
'/_query'
|
55
57
|
|
56
58
|
params = Utils.__validate_and_extract_params arguments, valid_params
|
57
59
|
body = arguments[:body]
|
@@ -13,7 +13,7 @@ module Elasticsearch
|
|
13
13
|
#
|
14
14
|
# client.indices.analyze text: 'The Quick Brown Jumping Fox', analyzer: 'snowball'
|
15
15
|
#
|
16
|
-
# @example Analyze text "Quick Brown Jumping Fox" with
|
16
|
+
# @example Analyze text "Quick Brown Jumping Fox" with a custom tokenizer and filter chain
|
17
17
|
#
|
18
18
|
# client.indices.analyze text: 'The Quick Brown Jumping Fox',
|
19
19
|
# tokenizer: 'whitespace',
|
@@ -25,6 +25,12 @@ module Elasticsearch
|
|
25
25
|
body = nil
|
26
26
|
|
27
27
|
perform_request(method, path, params, body).body
|
28
|
+
|
29
|
+
rescue Exception => e
|
30
|
+
# NOTE: Use exception name, not full class in Elasticsearch::Client to allow client plugability
|
31
|
+
if arguments[:ignore] == 404 && e.class.to_s =~ /NotFound/; false
|
32
|
+
else raise(e)
|
33
|
+
end
|
28
34
|
end
|
29
35
|
end
|
30
36
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Elasticsearch
|
2
|
+
module API
|
3
|
+
module Indices
|
4
|
+
module Actions
|
5
|
+
|
6
|
+
# Return the mapping definition for specific field (or fields)
|
7
|
+
#
|
8
|
+
# @example Get mapping for a specific field across all indices
|
9
|
+
#
|
10
|
+
# client.indices.get_field_mapping field: 'foo'
|
11
|
+
#
|
12
|
+
# @example Get mapping for a specific field in an index
|
13
|
+
#
|
14
|
+
# client.indices.get_field_mapping index: 'foo', field: 'bar'
|
15
|
+
#
|
16
|
+
# @example Get mappings for multiple fields in an index
|
17
|
+
#
|
18
|
+
# client.indices.get_field_mapping index: 'foo', field: ['bar', 'bam']
|
19
|
+
#
|
20
|
+
# @option arguments [List] :index A comma-separated list of index names
|
21
|
+
# @option arguments [List] :type A comma-separated list of document types
|
22
|
+
# @option arguments [List] :field A comma-separated list of fields (*Required*)
|
23
|
+
# @option arguments [Boolean] :include_defaults Whether default mapping values should be returned as well
|
24
|
+
#
|
25
|
+
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
|
26
|
+
#
|
27
|
+
def get_field_mapping(arguments={})
|
28
|
+
raise ArgumentError, "Required argument 'field' missing" unless arguments[:field]
|
29
|
+
valid_params = [ :include_defaults ]
|
30
|
+
|
31
|
+
method = 'GET'
|
32
|
+
path = Utils.__pathify(
|
33
|
+
Utils.__listify(arguments[:index]),
|
34
|
+
Utils.__listify(arguments[:type]),
|
35
|
+
'_mapping', 'field',
|
36
|
+
Utils.__listify(arguments[:field])
|
37
|
+
)
|
38
|
+
params = Utils.__validate_and_extract_params arguments, valid_params
|
39
|
+
body = nil
|
40
|
+
|
41
|
+
perform_request(method, path, params, body).body
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -4,16 +4,18 @@ require 'yaml'
|
|
4
4
|
require 'pry'
|
5
5
|
|
6
6
|
require 'elasticsearch'
|
7
|
-
require 'elasticsearch/
|
7
|
+
require 'elasticsearch/extensions/test/cluster'
|
8
|
+
require 'elasticsearch/extensions/test/startup_shutdown'
|
9
|
+
require 'elasticsearch/extensions/test/profiling'
|
8
10
|
|
9
11
|
# Launch test cluster
|
10
12
|
#
|
11
|
-
Elasticsearch::
|
13
|
+
Elasticsearch::Extensions::Test::Cluster.start if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running?
|
12
14
|
|
13
15
|
# Register `at_exit` handler for server shutdown.
|
14
16
|
# MUST be called before requiring `test/unit`.
|
15
17
|
#
|
16
|
-
at_exit { Elasticsearch::
|
18
|
+
at_exit { Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] }
|
17
19
|
|
18
20
|
require 'logger'
|
19
21
|
require 'ansi'
|
@@ -29,7 +31,17 @@ logger.formatter = proc do |severity, datetime, progname, msg|
|
|
29
31
|
ANSI.ansi(severity[0] + ' ', color, :faint) + ANSI.ansi(msg, :white, :faint) + "\n"
|
30
32
|
end
|
31
33
|
|
32
|
-
|
34
|
+
# Set up the client for the test
|
35
|
+
#
|
36
|
+
# To set up your own client, just set the `$client` variable in a file, and then require it:
|
37
|
+
#
|
38
|
+
# ruby -I lib:test -r ./tmp/my_special_client.rb test/integration/yaml_test_runner.rb
|
39
|
+
#
|
40
|
+
$client ||= Elasticsearch::Client.new host: "localhost:#{ENV['TEST_CLUSTER_PORT'] || 9250}",
|
41
|
+
logger: (ENV['QUIET'] ? nil : logger)
|
42
|
+
|
43
|
+
# Store Elasticsearch version
|
44
|
+
#
|
33
45
|
$es_version = $client.info['version']['number']
|
34
46
|
|
35
47
|
puts '-'*80, "Elasticsearch #{ANSI.ansi($es_version, :bold)}", '-'*80
|
@@ -211,6 +223,10 @@ suites.each do |suite|
|
|
211
223
|
# Extract setup actions
|
212
224
|
setup_actions = tests.select { |t| t['setup'] }.first['setup'] rescue []
|
213
225
|
|
226
|
+
# Skip all the tests when `skip` is part fo the `setup`
|
227
|
+
# TODO: Implement top-level `setup` expression
|
228
|
+
next if Runner.skip? setup_actions
|
229
|
+
|
214
230
|
# Remove setup actions from tests
|
215
231
|
tests = tests.reject { |t| t['setup'] }
|
216
232
|
|
@@ -230,6 +246,7 @@ suites.each do |suite|
|
|
230
246
|
# --- Register test setup -------------------------------------------
|
231
247
|
setup do
|
232
248
|
actions.select { |a| a['setup'] }.first['setup'].each do |action|
|
249
|
+
next unless action['do']
|
233
250
|
api, arguments = action['do'].to_a.first
|
234
251
|
arguments = Utils.symbolize_keys(arguments)
|
235
252
|
Runner.perform_api_call((test.to_s + '___setup'), api, arguments)
|
data/test/test_helper.rb
CHANGED
@@ -24,6 +24,10 @@ require 'require-prof' if ENV["REQUIRE_PROF"]
|
|
24
24
|
require 'elasticsearch/api'
|
25
25
|
RequireProf.print_timing_infos if ENV["REQUIRE_PROF"]
|
26
26
|
|
27
|
+
require 'elasticsearch/extensions/test/cluster'
|
28
|
+
require 'elasticsearch/extensions/test/startup_shutdown'
|
29
|
+
require 'elasticsearch/extensions/test/profiling'
|
30
|
+
|
27
31
|
module Elasticsearch
|
28
32
|
module Test
|
29
33
|
class FakeClient
|
data/test/unit/count_test.rb
CHANGED
@@ -32,11 +32,11 @@ module Elasticsearch
|
|
32
32
|
should "take the query" do
|
33
33
|
subject.expects(:perform_request).with do |method, url, params, body|
|
34
34
|
assert_equal 'GET', method
|
35
|
-
assert_equal( {:
|
35
|
+
assert_equal( {:match => {:foo => 'bar'}}, body)
|
36
36
|
true
|
37
37
|
end.returns(FakeResponse.new)
|
38
38
|
|
39
|
-
subject.count :body => { :
|
39
|
+
subject.count :body => { :match => { :foo => 'bar' } }
|
40
40
|
end
|
41
41
|
|
42
42
|
end
|
@@ -25,6 +25,15 @@ module Elasticsearch
|
|
25
25
|
subject.delete_by_query :index => 'foo', :body => { :term => {} }
|
26
26
|
end
|
27
27
|
|
28
|
+
should "optionally take the :type argument" do
|
29
|
+
subject.expects(:perform_request).with do |method, url, params, body|
|
30
|
+
assert_equal 'foo/tweet,post/_query', url
|
31
|
+
true
|
32
|
+
end.returns(FakeResponse.new)
|
33
|
+
|
34
|
+
subject.delete_by_query :index => 'foo', :type => ['tweet', 'post'], :body => { :term => {} }
|
35
|
+
end
|
36
|
+
|
28
37
|
should "pass the query in URL parameters" do
|
29
38
|
subject.expects(:perform_request).with do |method, url, params, body|
|
30
39
|
assert_equal 'foo/_query', url
|
@@ -28,6 +28,14 @@ module Elasticsearch
|
|
28
28
|
subject.indices.delete_template :name => 'foo^bar'
|
29
29
|
end
|
30
30
|
|
31
|
+
should "ignore 404s" do
|
32
|
+
subject.expects(:perform_request).raises(NotFound)
|
33
|
+
|
34
|
+
assert_nothing_raised do
|
35
|
+
assert ! subject.indices.delete_template(:name => 'foo^bar', :ignore => 404)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
31
39
|
end
|
32
40
|
|
33
41
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Elasticsearch
|
4
|
+
module Test
|
5
|
+
class IndicesGetFieldMappingTest < ::Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "Indices: Get field mapping" do
|
8
|
+
subject { FakeClient.new }
|
9
|
+
|
10
|
+
should "perform correct request" do
|
11
|
+
subject.expects(:perform_request).with do |method, url, params, body|
|
12
|
+
assert_equal 'GET', method
|
13
|
+
assert_equal '_mapping/field/foo', url
|
14
|
+
assert_equal Hash.new, params
|
15
|
+
assert_nil body
|
16
|
+
true
|
17
|
+
end.returns(FakeResponse.new)
|
18
|
+
|
19
|
+
subject.indices.get_field_mapping :field => 'foo'
|
20
|
+
end
|
21
|
+
|
22
|
+
should "perform request against an index" do
|
23
|
+
subject.expects(:perform_request).with do |method, url, params, body|
|
24
|
+
assert_equal 'foo/_mapping/field/bam', url
|
25
|
+
true
|
26
|
+
end.returns(FakeResponse.new)
|
27
|
+
|
28
|
+
subject.indices.get_field_mapping :index => 'foo', :field => 'bam'
|
29
|
+
end
|
30
|
+
|
31
|
+
should "perform request against an index and type" do
|
32
|
+
subject.expects(:perform_request).with do |method, url, params, body|
|
33
|
+
assert_equal 'foo/bar/_mapping/field/bam', url
|
34
|
+
true
|
35
|
+
end.returns(FakeResponse.new)
|
36
|
+
|
37
|
+
subject.indices.get_field_mapping :index => 'foo', :type => 'bar', :field => 'bam'
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/test/unit/utils_test.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticsearch-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-12-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: multi_json
|
@@ -91,6 +91,22 @@ dependencies:
|
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: elasticsearch-extensions
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
94
110
|
- !ruby/object:Gem::Dependency
|
95
111
|
name: ansi
|
96
112
|
requirement: !ruby/object:Gem::Requirement
|
@@ -416,6 +432,7 @@ files:
|
|
416
432
|
- lib/elasticsearch/api/actions/indices/flush.rb
|
417
433
|
- lib/elasticsearch/api/actions/indices/get_alias.rb
|
418
434
|
- lib/elasticsearch/api/actions/indices/get_aliases.rb
|
435
|
+
- lib/elasticsearch/api/actions/indices/get_field_mapping.rb
|
419
436
|
- lib/elasticsearch/api/actions/indices/get_mapping.rb
|
420
437
|
- lib/elasticsearch/api/actions/indices/get_settings.rb
|
421
438
|
- lib/elasticsearch/api/actions/indices/get_template.rb
|
@@ -487,6 +504,7 @@ files:
|
|
487
504
|
- test/unit/indices/flush_test.rb
|
488
505
|
- test/unit/indices/get_alias_test.rb
|
489
506
|
- test/unit/indices/get_aliases_test.rb
|
507
|
+
- test/unit/indices/get_field_mapping_test.rb
|
490
508
|
- test/unit/indices/get_mapping_test.rb
|
491
509
|
- test/unit/indices/get_settings_test.rb
|
492
510
|
- test/unit/indices/get_template_test.rb
|
@@ -582,6 +600,7 @@ test_files:
|
|
582
600
|
- test/unit/indices/flush_test.rb
|
583
601
|
- test/unit/indices/get_alias_test.rb
|
584
602
|
- test/unit/indices/get_aliases_test.rb
|
603
|
+
- test/unit/indices/get_field_mapping_test.rb
|
585
604
|
- test/unit/indices/get_mapping_test.rb
|
586
605
|
- test/unit/indices/get_settings_test.rb
|
587
606
|
- test/unit/indices/get_template_test.rb
|