elasticsearch-api 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +13 -0
  5. data/README.md +160 -0
  6. data/Rakefile +62 -0
  7. data/elasticsearch-api.gemspec +51 -0
  8. data/lib/elasticsearch-api +1 -0
  9. data/lib/elasticsearch/api.rb +23 -0
  10. data/lib/elasticsearch/api/actions/bulk.rb +71 -0
  11. data/lib/elasticsearch/api/actions/cluster/get_settings.rb +21 -0
  12. data/lib/elasticsearch/api/actions/cluster/health.rb +50 -0
  13. data/lib/elasticsearch/api/actions/cluster/node_hot_threads.rb +46 -0
  14. data/lib/elasticsearch/api/actions/cluster/node_info.rb +59 -0
  15. data/lib/elasticsearch/api/actions/cluster/node_shutdown.rb +36 -0
  16. data/lib/elasticsearch/api/actions/cluster/node_stats.rb +77 -0
  17. data/lib/elasticsearch/api/actions/cluster/put_settings.rb +28 -0
  18. data/lib/elasticsearch/api/actions/cluster/reroute.rb +44 -0
  19. data/lib/elasticsearch/api/actions/cluster/state.rb +47 -0
  20. data/lib/elasticsearch/api/actions/count.rb +46 -0
  21. data/lib/elasticsearch/api/actions/create.rb +34 -0
  22. data/lib/elasticsearch/api/actions/delete.rb +61 -0
  23. data/lib/elasticsearch/api/actions/delete_by_query.rb +62 -0
  24. data/lib/elasticsearch/api/actions/exists.rb +51 -0
  25. data/lib/elasticsearch/api/actions/explain.rb +71 -0
  26. data/lib/elasticsearch/api/actions/get.rb +59 -0
  27. data/lib/elasticsearch/api/actions/get_source.rb +59 -0
  28. data/lib/elasticsearch/api/actions/index.rb +81 -0
  29. data/lib/elasticsearch/api/actions/indices/analyze.rb +63 -0
  30. data/lib/elasticsearch/api/actions/indices/clear_cache.rb +69 -0
  31. data/lib/elasticsearch/api/actions/indices/close.rb +35 -0
  32. data/lib/elasticsearch/api/actions/indices/create.rb +83 -0
  33. data/lib/elasticsearch/api/actions/indices/delete.rb +48 -0
  34. data/lib/elasticsearch/api/actions/indices/delete_alias.rb +37 -0
  35. data/lib/elasticsearch/api/actions/indices/delete_mapping.rb +26 -0
  36. data/lib/elasticsearch/api/actions/indices/delete_template.rb +33 -0
  37. data/lib/elasticsearch/api/actions/indices/delete_warmer.rb +32 -0
  38. data/lib/elasticsearch/api/actions/indices/exists.rb +35 -0
  39. data/lib/elasticsearch/api/actions/indices/exists_alias.rb +41 -0
  40. data/lib/elasticsearch/api/actions/indices/exists_type.rb +39 -0
  41. data/lib/elasticsearch/api/actions/indices/flush.rb +40 -0
  42. data/lib/elasticsearch/api/actions/indices/get_alias.rb +41 -0
  43. data/lib/elasticsearch/api/actions/indices/get_aliases.rb +32 -0
  44. data/lib/elasticsearch/api/actions/indices/get_mapping.rb +36 -0
  45. data/lib/elasticsearch/api/actions/indices/get_settings.rb +32 -0
  46. data/lib/elasticsearch/api/actions/indices/get_template.rb +30 -0
  47. data/lib/elasticsearch/api/actions/indices/get_warmer.rb +44 -0
  48. data/lib/elasticsearch/api/actions/indices/open.rb +33 -0
  49. data/lib/elasticsearch/api/actions/indices/optimize.rb +57 -0
  50. data/lib/elasticsearch/api/actions/indices/put_alias.rb +43 -0
  51. data/lib/elasticsearch/api/actions/indices/put_mapping.rb +49 -0
  52. data/lib/elasticsearch/api/actions/indices/put_settings.rb +45 -0
  53. data/lib/elasticsearch/api/actions/indices/put_template.rb +40 -0
  54. data/lib/elasticsearch/api/actions/indices/put_warmer.rb +48 -0
  55. data/lib/elasticsearch/api/actions/indices/refresh.rb +43 -0
  56. data/lib/elasticsearch/api/actions/indices/segments.rb +33 -0
  57. data/lib/elasticsearch/api/actions/indices/snapshot_index.rb +32 -0
  58. data/lib/elasticsearch/api/actions/indices/stats.rb +96 -0
  59. data/lib/elasticsearch/api/actions/indices/status.rb +46 -0
  60. data/lib/elasticsearch/api/actions/indices/update_aliases.rb +49 -0
  61. data/lib/elasticsearch/api/actions/indices/validate_query.rb +68 -0
  62. data/lib/elasticsearch/api/actions/info.rb +19 -0
  63. data/lib/elasticsearch/api/actions/mget.rb +64 -0
  64. data/lib/elasticsearch/api/actions/mlt.rb +86 -0
  65. data/lib/elasticsearch/api/actions/msearch.rb +75 -0
  66. data/lib/elasticsearch/api/actions/percolate.rb +57 -0
  67. data/lib/elasticsearch/api/actions/ping.rb +29 -0
  68. data/lib/elasticsearch/api/actions/scroll.rb +44 -0
  69. data/lib/elasticsearch/api/actions/search.rb +145 -0
  70. data/lib/elasticsearch/api/actions/suggest.rb +46 -0
  71. data/lib/elasticsearch/api/actions/update.rb +103 -0
  72. data/lib/elasticsearch/api/namespace/cluster.rb +20 -0
  73. data/lib/elasticsearch/api/namespace/common.rb +27 -0
  74. data/lib/elasticsearch/api/namespace/indices.rb +20 -0
  75. data/lib/elasticsearch/api/utils.rb +97 -0
  76. data/lib/elasticsearch/api/version.rb +5 -0
  77. data/test/integration/yaml_test_runner.rb +330 -0
  78. data/test/test_helper.rb +52 -0
  79. data/test/unit/bulk_test.rb +85 -0
  80. data/test/unit/client_test.rb +31 -0
  81. data/test/unit/cluster/get_settings_test.rb +26 -0
  82. data/test/unit/cluster/health_test.rb +38 -0
  83. data/test/unit/cluster/node_hot_threads_test.rb +35 -0
  84. data/test/unit/cluster/node_info_test.rb +45 -0
  85. data/test/unit/cluster/node_shutdown_test.rb +45 -0
  86. data/test/unit/cluster/node_stats_test.rb +65 -0
  87. data/test/unit/cluster/put_settings_test.rb +26 -0
  88. data/test/unit/cluster/reroute_test.rb +38 -0
  89. data/test/unit/cluster/state_test.rb +37 -0
  90. data/test/unit/count_test.rb +46 -0
  91. data/test/unit/create_document_test.rb +38 -0
  92. data/test/unit/delete_by_query_test.rb +42 -0
  93. data/test/unit/delete_document_test.rb +62 -0
  94. data/test/unit/exists_document_test.rb +76 -0
  95. data/test/unit/explain_document_test.rb +64 -0
  96. data/test/unit/get_document_source_test.rb +62 -0
  97. data/test/unit/get_document_test.rb +62 -0
  98. data/test/unit/hashie_test.rb +78 -0
  99. data/test/unit/index_document_test.rb +77 -0
  100. data/test/unit/indices/analyze_test.rb +67 -0
  101. data/test/unit/indices/clear_cache_test.rb +45 -0
  102. data/test/unit/indices/close_test.rb +42 -0
  103. data/test/unit/indices/create_test.rb +42 -0
  104. data/test/unit/indices/delete_alias_test.rb +38 -0
  105. data/test/unit/indices/delete_mapping_test.rb +47 -0
  106. data/test/unit/indices/delete_template_test.rb +26 -0
  107. data/test/unit/indices/delete_test.rb +45 -0
  108. data/test/unit/indices/delete_warmer_test.rb +59 -0
  109. data/test/unit/indices/exists_alias_test.rb +65 -0
  110. data/test/unit/indices/exists_test.rb +57 -0
  111. data/test/unit/indices/exists_type_test.rb +59 -0
  112. data/test/unit/indices/flush_test.rb +45 -0
  113. data/test/unit/indices/get_alias_test.rb +41 -0
  114. data/test/unit/indices/get_aliases_test.rb +35 -0
  115. data/test/unit/indices/get_mapping_test.rb +53 -0
  116. data/test/unit/indices/get_settings_test.rb +35 -0
  117. data/test/unit/indices/get_template_test.rb +32 -0
  118. data/test/unit/indices/get_warmer_test.rb +41 -0
  119. data/test/unit/indices/open_test.rb +42 -0
  120. data/test/unit/indices/optimize_test.rb +45 -0
  121. data/test/unit/indices/put_alias_test.rb +47 -0
  122. data/test/unit/indices/put_mapping_test.rb +57 -0
  123. data/test/unit/indices/put_settings_test.rb +50 -0
  124. data/test/unit/indices/put_template_test.rb +48 -0
  125. data/test/unit/indices/put_warmer_test.rb +62 -0
  126. data/test/unit/indices/refresh_test.rb +55 -0
  127. data/test/unit/indices/segments_test.rb +55 -0
  128. data/test/unit/indices/snapshot_index_test.rb +55 -0
  129. data/test/unit/indices/stats_test.rb +76 -0
  130. data/test/unit/indices/status_test.rb +55 -0
  131. data/test/unit/indices/update_aliases_test.rb +42 -0
  132. data/test/unit/indices/validate_query_test.rb +75 -0
  133. data/test/unit/info_test.rb +26 -0
  134. data/test/unit/json_builders_test.rb +64 -0
  135. data/test/unit/mget_test.rb +70 -0
  136. data/test/unit/mlt_test.rb +80 -0
  137. data/test/unit/msearch_test.rb +120 -0
  138. data/test/unit/percolate_test.rb +49 -0
  139. data/test/unit/ping_test.rb +48 -0
  140. data/test/unit/scroll_test.rb +26 -0
  141. data/test/unit/search_test.rb +93 -0
  142. data/test/unit/suggest_test.rb +55 -0
  143. data/test/unit/update_document_test.rb +62 -0
  144. data/test/unit/utils_test.rb +118 -0
  145. metadata +498 -0
@@ -0,0 +1,145 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Actions
4
+
5
+ # Return documents matching a query, as well as aggregations (facets), highlighted snippets, suggestions, etc.
6
+ #
7
+ # The search API is used to query one or more indices either using simple
8
+ # [query string queries](http://www.elasticsearch.org/guide/reference/api/search/uri-request/)
9
+ # as the `:q` argument , or by passing the
10
+ # [full request definition](http://www.elasticsearch.org/guide/reference/api/search/request-body/)
11
+ # in the [Query DSL](http://www.elasticsearch.org/guide/reference/query-dsl/) as the `:body` argument.
12
+ #
13
+ # @example Search with a simple query string query
14
+ #
15
+ # client.search index: 'myindex', q: 'title:test'
16
+ #
17
+ # @example Passing a full request definition in the Elasticsearch's Query DSL as a `Hash`
18
+ #
19
+ # client.search index: 'myindex',
20
+ # body: {
21
+ # query: { match: { title: 'test' } },
22
+ # facets: { tags: { terms: { field: 'tags' } } }
23
+ # }
24
+ #
25
+ # @example Passing the search definition as a `String`, built with a JSON builder
26
+ #
27
+ # require 'jbuilder'
28
+ #
29
+ # json = Jbuilder.encode do |json|
30
+ # json.query do
31
+ # json.match do
32
+ # json.title do
33
+ # json.query 'test 1'
34
+ # json.operator 'and'
35
+ # end
36
+ # end
37
+ # end
38
+ # end
39
+ #
40
+ # client.search index: 'myindex', body: json
41
+ #
42
+ # @example Wrapping the result in [`Hashie::Mash`](https://github.com/intridea/hashie) for easier access
43
+ #
44
+ # response = client.search index: 'myindex',
45
+ # body: {
46
+ # query: { match: { title: 'test' } },
47
+ # facets: { tags: { terms: { field: 'tags' } } }
48
+ # }
49
+ #
50
+ # response = Hashie::Mash.new response
51
+ #
52
+ # response.hits.hits.first._source.title
53
+ #
54
+ # response.facets.tags.terms.to_a.map { |f| "#{f.term} [#{f.count}]" }.join(', ')
55
+ #
56
+ # @option arguments [List] :index A comma-separated list of index names to search; use `_all`
57
+ # or empty string to perform the operation on all indices
58
+ # @option arguments [List] :type A comma-separated list of document types to search;
59
+ # leave empty to perform the operation on all types
60
+ # @option arguments [Hash] :body The search definition using the Query DSL
61
+ # @option arguments [String] :analyzer The analyzer to use for the query string
62
+ # @option arguments [Boolean] :analyze_wildcard Specify whether wildcard and prefix queries should be analyzed
63
+ # (default: false)
64
+ # @option arguments [String] :default_operator The default operator for query string query (AND or OR)
65
+ # (options: AND, OR)
66
+ # @option arguments [String] :df The field to use as default where no field prefix is given in the query string
67
+ # @option arguments [Boolean] :explain Specify whether to return detailed information about score computation
68
+ # as part of a hit
69
+ # @option arguments [List] :fields A comma-separated list of fields to return as part of a hit
70
+ # @option arguments [Number] :from Starting offset (default: 0)
71
+ # @option arguments [String] :ignore_indices When performed on multiple indices, allows to ignore `missing` ones
72
+ # (options: none, missing)
73
+ # @option arguments [List] :indices_boost Comma-separated list of index boosts
74
+ # @option arguments [Boolean] :lenient Specify whether format-based query failures
75
+ # (such as providing text to a numeric field) should be ignored
76
+ # @option arguments [Boolean] :lowercase_expanded_terms Specify whether query terms should be lowercased
77
+ # @option arguments [String] :preference Specify the node or shard the operation should be performed on
78
+ # (default: random)
79
+ # @option arguments [String] :q Query in the Lucene query string syntax
80
+ # @option arguments [List] :routing A comma-separated list of specific routing values
81
+ # @option arguments [Duration] :scroll Specify how long a consistent view of the index should be maintained
82
+ # for scrolled search
83
+ # @option arguments [String] :search_type Search operation type (options: query_then_fetch, query_and_fetch,
84
+ # dfs_query_then_fetch, dfs_query_and_fetch, count, scan)
85
+ # @option arguments [Number] :size Number of hits to return (default: 10)
86
+ # @option arguments [List] :sort A comma-separated list of <field>:<direction> pairs
87
+ # @option arguments [String] :source The URL-encoded request definition using the Query DSL
88
+ # (instead of using request body)
89
+ # @option arguments [List] :stats Specific 'tag' of the request for logging and statistical purposes
90
+ # @option arguments [String] :suggest_field Specify which field to use for suggestions
91
+ # @option arguments [String] :suggest_mode Specify suggest mode (options: missing, popular, always)
92
+ # @option arguments [Number] :suggest_size How many suggestions to return in response
93
+ # @option arguments [Text] :suggest_text The source text for which the suggestions should be returned
94
+ # @option arguments [Time] :timeout Explicit operation timeout
95
+ # @option arguments [Boolean] :version Specify whether to return document version as part of a hit
96
+ #
97
+ # @return [Hash]
98
+ #
99
+ # @see http://www.elasticsearch.org/guide/reference/api/search/
100
+ # @see http://www.elasticsearch.org/guide/reference/api/search/request-body/
101
+ #
102
+ def search(arguments={})
103
+ arguments[:index] = '_all' if ! arguments[:index] && arguments[:type]
104
+
105
+ method = 'GET'
106
+ path = Utils.__pathify( Utils.__listify(arguments[:index]), Utils.__listify(arguments[:type]), '_search' )
107
+ params = arguments.select do |k,v|
108
+ [ :analyzer,
109
+ :analyze_wildcard,
110
+ :default_operator,
111
+ :df,
112
+ :explain,
113
+ :fields,
114
+ :from,
115
+ :ignore_indices,
116
+ :indices_boost,
117
+ :lenient,
118
+ :lowercase_expanded_terms,
119
+ :preference,
120
+ :q,
121
+ :routing,
122
+ :scroll,
123
+ :search_type,
124
+ :size,
125
+ :sort,
126
+ :source,
127
+ :stats,
128
+ :suggest_field,
129
+ :suggest_mode,
130
+ :suggest_size,
131
+ :suggest_text,
132
+ :timeout,
133
+ :version ].include?(k)
134
+ end
135
+ # Normalize Ruby 1.8 and Ruby 1.9 Hash#select behaviour
136
+ params = Hash[params] unless params.is_a?(Hash)
137
+ body = arguments[:body]
138
+
139
+ params[:fields] = Utils.__listify(params[:fields]) if params[:fields]
140
+
141
+ perform_request(method, path, params, body).body
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,46 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Actions
4
+
5
+ # Return query terms suggestions based on provided text and configuration.
6
+ #
7
+ # Pass the request definition in the `:body` argument.
8
+ #
9
+ # @example Return query terms suggestions ("auto-correction")
10
+ #
11
+ # client.suggest index: 'myindex',
12
+ # body: { my_suggest: { text: 'tset', term: { field: 'title' } } }
13
+ # # => { ... "my_suggest"=>[ {"text"=>"tset", ... "options"=>[{"text"=>"test", "score"=>0.75, "freq"=>5}] }]}
14
+ #
15
+ # @option arguments [List] :index A comma-separated list of index names to restrict the operation;
16
+ # use `_all` or empty string to perform the operation on all indices
17
+ # @option arguments [Hash] :body The request definition
18
+ # @option arguments [String] :ignore_indices When performed on multiple indices, allows to ignore `missing` ones
19
+ # (options: none, missing)
20
+ # @option arguments [String] :preference Specify the node or shard the operation should be performed on
21
+ # (default: random)
22
+ # @option arguments [String] :routing Specific routing value
23
+ # @option arguments [String] :source The URL-encoded request definition (instead of using request body)
24
+ #
25
+ # @since 0.90
26
+ #
27
+ # @see http://elasticsearch.org/guide/reference/api/search/suggest/
28
+ #
29
+ def suggest(arguments={})
30
+ method = 'POST'
31
+ path = Utils.__pathify( Utils.__listify(arguments[:index]), '_suggest' )
32
+ params = arguments.select do |k,v|
33
+ [ :ignore_indices,
34
+ :preference,
35
+ :routing,
36
+ :source ].include?(k)
37
+ end
38
+ # Normalize Ruby 1.8 and Ruby 1.9 Hash#select behaviour
39
+ params = Hash[params] unless params.is_a?(Hash)
40
+ body = arguments[:body]
41
+
42
+ perform_request(method, path, params, body).body
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,103 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Actions
4
+
5
+ # Update a document without sending the whole document in the request ("partial update").
6
+ #
7
+ # Send either a partial document (`doc` ) which will be deeply merged into an existing document,
8
+ # or a `script`, which will update the document content, in the `:body` argument.
9
+ #
10
+ # The partial update operation allows you to limit the amount of data you send over the wire and
11
+ # reduces the chance of failed updates due to conflict.
12
+ #
13
+ # Specify the `:version` and `:retry_on_conflict` arguments to balance convenience and consistency.
14
+ #
15
+ # @example Update document _title_ using partial `doc`-ument
16
+ #
17
+ # client.update index: 'myindex', type: 'mytype', id: '1',
18
+ # body: { doc: { title: 'Updated' } }
19
+ #
20
+ # @example Add a tag to document `tags` property using a `script`
21
+ #
22
+ # client.update index: 'myindex', type: 'mytype', id: '1',
23
+ # body: { script: 'ctx._source.tags += tag', params: { tag: 'x' } }
24
+ #
25
+ # @example Increment a document counter by 1 _or_ initialize it, when the document does not exist
26
+ #
27
+ # client.update index: 'myindex', type: 'mytype', id: '666',
28
+ # body: { script: 'ctx._source.counter += 1', upsert: { counter: 1 } }
29
+ #
30
+ # @example Delete a document if it's tagged "to-delete"
31
+ #
32
+ # client.update index: 'myindex', type: 'mytype', id: '1',
33
+ # body: { script: 'ctx._source.tags.contains(tag) ? ctx.op = "delete" : ctx.op = "none"',
34
+ # params: { tag: 'to-delete' } }
35
+ #
36
+ # @option arguments [String] :id Document ID (*Required*)
37
+ # @option arguments [Number,List] :ignore The list of HTTP errors to ignore; only `404` supported at the moment
38
+ # @option arguments [String] :index The name of the index (*Required*)
39
+ # @option arguments [String] :type The type of the document (*Required*)
40
+ # @option arguments [Hash] :body The request definition using either `script` or partial `doc` (*Required*)
41
+ # @option arguments [String] :consistency Explicit write consistency setting for the operation
42
+ # (options: one, quorum, all)
43
+ # @option arguments [List] :fields A comma-separated list of fields to return in the response
44
+ # @option arguments [String] :lang The script language (default: mvel)
45
+ # @option arguments [String] :parent ID of the parent document
46
+ # @option arguments [String] :percolate Perform percolation during the operation;
47
+ # use specific registered query name, attribute, or wildcard
48
+ # @option arguments [Boolean] :refresh Refresh the index after performing the operation
49
+ # @option arguments [String] :replication Specific replication type (options: sync, async)
50
+ # @option arguments [Number] :retry_on_conflict Specify how many times should the operation be retried
51
+ # when a conflict occurs (default: 0)
52
+ # @option arguments [String] :routing Specific routing value
53
+ # @option arguments [String] :script The URL-encoded script definition (instead of using request body)
54
+ # @option arguments [Time] :timeout Explicit operation timeout
55
+ # @option arguments [Time] :timestamp Explicit timestamp for the document
56
+ # @option arguments [Duration] :ttl Expiration time for the document
57
+ # @option arguments [Number] :version Explicit version number for concurrency control
58
+ # @option arguments [Number] :version_type Explicit version number for concurrency control
59
+ #
60
+ # @since 0.20
61
+ #
62
+ # @see http://elasticsearch.org/guide/reference/api/update/
63
+ #
64
+ def update(arguments={})
65
+ raise ArgumentError, "Required argument 'index' missing" unless arguments[:index]
66
+ raise ArgumentError, "Required argument 'type' missing" unless arguments[:type]
67
+ raise ArgumentError, "Required argument 'id' missing" unless arguments[:id]
68
+ method = 'POST'
69
+ path = Utils.__pathify( arguments[:index], arguments[:type], arguments[:id], '_update' )
70
+ params = arguments.select do |k,v|
71
+ [ :consistency,
72
+ :fields,
73
+ :lang,
74
+ :parent,
75
+ :percolate,
76
+ :refresh,
77
+ :replication,
78
+ :retry_on_conflict,
79
+ :routing,
80
+ :script,
81
+ :timeout,
82
+ :timestamp,
83
+ :ttl,
84
+ :version,
85
+ :version_type ].include?(k)
86
+ end
87
+ # Normalize Ruby 1.8 and Ruby 1.9 Hash#select behaviour
88
+ params = Hash[params] unless params.is_a?(Hash)
89
+ body = arguments[:body]
90
+
91
+ params[:fields] = Utils.__listify(params[:fields]) if params[:fields]
92
+
93
+ perform_request(method, path, params, body).body
94
+
95
+ rescue Exception => e
96
+ # NOTE: Use exception name, not full class in Elasticsearch::Client to allow client plugability
97
+ if arguments[:ignore] == 404 && e.class.to_s =~ /NotFound/; false
98
+ else raise(e)
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,20 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Cluster
4
+ module Actions; end
5
+
6
+ # Client for the "cluster" namespace (includes the {Cluster::Actions} methods)
7
+ #
8
+ class ClusterClient
9
+ include Common::Client, Common::Client::Base, Cluster::Actions
10
+ end
11
+
12
+ # Proxy method for {ClusterClient}, available in the receiving object
13
+ #
14
+ def cluster
15
+ @cluster ||= ClusterClient.new(self)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Common
4
+ module Actions; end
5
+
6
+ module Client
7
+
8
+ # Base client wrapper
9
+ #
10
+ module Base
11
+ attr_reader :client
12
+
13
+ def initialize(client)
14
+ @client = client
15
+ end
16
+ end
17
+
18
+ # Delegates the `perform_request` method to the wrapped client
19
+ #
20
+ def perform_request(method, path, params={}, body=nil)
21
+ client.perform_request method, path, params, body
22
+ end
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ module Elasticsearch
2
+ module API
3
+ module Indices
4
+ module Actions; end
5
+
6
+ # Client for the "indices" namespace (includes the {Indices::Actions} methods)
7
+ #
8
+ class IndicesClient
9
+ include Common::Client, Common::Client::Base, Indices::Actions
10
+ end
11
+
12
+ # Proxy method for {IndicesClient}, available in the receiving object
13
+ #
14
+ def indices
15
+ @indices ||= IndicesClient.new(self)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,97 @@
1
+ module Elasticsearch
2
+ module API
3
+
4
+ # Generic utility methods
5
+ #
6
+ module Utils
7
+
8
+ # URL-escape a string
9
+ #
10
+ # @example
11
+ # __escape('bar^bam') # => 'bar%5Ebam'
12
+ #
13
+ # @api private
14
+ def __escape(string)
15
+ URI.encode(string.to_s)
16
+ end
17
+
18
+ # Create a "list" of values from arguments, ignoring nil values
19
+ #
20
+ # @example Create a list from array
21
+ # __listify(['A','B']) # => 'A,B'
22
+ #
23
+ # @example Create a list from arguments
24
+ # __listify('A','B') # => 'A,B'
25
+ #
26
+ # @api private
27
+ def __listify(*list)
28
+ Array(list).flatten.compact.join(',')
29
+ end
30
+
31
+ # Create a path (URL part) from arguments, ignoring nil values and empty strings,
32
+ # and encoding special characters
33
+ #
34
+ # @example Create a path from array
35
+ # __pathify(['foo', '', nil, 'bar']) # => 'foo/bar'
36
+ #
37
+ # @example Create a path from arguments
38
+ # __pathify('foo', '', nil, 'bar') # => 'foo/bar'
39
+ #
40
+ # # @example Encode special characters
41
+ # __pathify(['foo', 'bar^bam']) # => 'foo/bar%5Ebam'
42
+ #
43
+ # @api private
44
+ def __pathify(*segments)
45
+ Array(segments).flatten.
46
+ compact.
47
+ reject { |s| s.to_s =~ /^\s*$/ }.
48
+ map { |s| __escape(s) }.
49
+ join('/').
50
+ squeeze('/')
51
+ end
52
+
53
+ # Convert an array of payloads into Elasticsearch `header\ndata` format
54
+ #
55
+ # Elasticsearch::API::Utils.__bulkify [
56
+ # { :index => { :_index => 'myindexA', :_type => 'mytype', :_id => '1', :data => { :title => 'Test' } } },
57
+ # { :update => { :_index => 'myindexB', :_type => 'mytype', :_id => '2', :data => { :doc => { :title => 'Update' } } } }
58
+ # ]
59
+ #
60
+ # # => {"index":{"_index":"myindexA","_type":"mytype","_id":"1"}}
61
+ # # => {"title":"Test"}
62
+ # # => {"update":{"_index":"myindexB","_type":"mytype","_id":"2"}}
63
+ # # => {"doc":{"title":"Update"}}
64
+ #
65
+ def __bulkify(payload)
66
+ case
67
+ # Hashes with `:data`
68
+ when payload.any? { |d| d.is_a?(Hash) && d.values.first.is_a?(Hash) && (d.values.first[:data] || d.values.first['data']) }
69
+ payload = payload.
70
+ inject([]) do |sum, item|
71
+ operation, meta = item.to_a.first
72
+ data = meta.delete(:data) || meta.delete('data')
73
+
74
+ sum << { operation => meta }
75
+ sum << data if data
76
+ sum
77
+ end.
78
+ map { |item| MultiJson.dump(item) }
79
+ payload << "" unless payload.empty?
80
+
81
+ # Array of strings
82
+ when payload.all? { |d| d.is_a? String }
83
+ payload << ''
84
+
85
+ # Header/Data pairs
86
+ else
87
+ payload = payload.map { |item| MultiJson.dump(item) }
88
+ payload << ''
89
+ end
90
+
91
+ payload = payload.join("\n")
92
+ end
93
+
94
+ extend self
95
+ end
96
+ end
97
+ end