Module: Elasticsearch::API::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/elasticsearch/api/utils.rb

Overview

Generic utility methods

Instance Method Summary collapse

Instance Method Details

#__bulkify(payload) ⇒ Object

Convert an array of payloads into Elasticsearch ‘headerndata` format

Elasticsearch::API::Utils.__bulkify [
  { :index =>  { :_index => 'myindexA', :_type => 'mytype', :_id => '1', :data => { :title => 'Test' } } },
  { :update => { :_index => 'myindexB', :_type => 'mytype', :_id => '2', :data => { :doc => { :title => 'Update' } } } }
]

# => {"index":{"_index":"myindexA","_type":"mytype","_id":"1"}}
# => {"title":"Test"}
# => {"update":{"_index":"myindexB","_type":"mytype","_id":"2"}}
# => {"doc":{"title":"Update"}}


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/elasticsearch/api/utils.rb', line 65

def __bulkify(payload)
  case
  # Hashes with `:data`
  when payload.any? { |d| d.is_a?(Hash) && d.values.first.is_a?(Hash) && (d.values.first[:data] || d.values.first['data']) }
    payload = payload.
    inject([]) do |sum, item|
      operation, meta = item.to_a.first
      data            = meta.delete(:data) || meta.delete('data')

      sum << { operation => meta }
      sum << data if data
      sum
    end.
    map { |item| MultiJson.dump(item) }
    payload << "" unless payload.empty?

  # Array of strings
  when payload.all? { |d| d.is_a? String }
    payload << ''

  # Header/Data pairs
  else
    payload = payload.map { |item| MultiJson.dump(item) }
    payload << ''
  end

  payload = payload.join("\n")
end

#__escape(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

URL-escape a string

Examples:

__escape('bar^bam') # => 'bar%5Ebam'


14
15
16
# File 'lib/elasticsearch/api/utils.rb', line 14

def __escape(string)
  URI.encode(string.to_s)
end

#__listify(*list) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a “list” of values from arguments, ignoring nil values

Examples:

Create a list from array

__listify(['A','B']) # => 'A,B'

Create a list from arguments

__listify('A','B') # => 'A,B'


27
28
29
# File 'lib/elasticsearch/api/utils.rb', line 27

def __listify(*list)
  Array(list).flatten.compact.join(',')
end

#__pathify(*segments) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a path (URL part) from arguments, ignoring nil values and empty strings, and encoding special characters

# @example Encode special characters

__pathify(['foo', 'bar^bam']) # => 'foo/bar%5Ebam'

Examples:

Create a path from array

__pathify(['foo', '', nil, 'bar']) # => 'foo/bar'

Create a path from arguments

__pathify('foo', '', nil, 'bar') # => 'foo/bar'


44
45
46
47
48
49
50
51
# File 'lib/elasticsearch/api/utils.rb', line 44

def __pathify(*segments)
  Array(segments).flatten.
    compact.
    reject { |s| s.to_s =~ /^\s*$/ }.
    map    { |s| __escape(s) }.
    join('/').
    squeeze('/')
end