Utility Methods

Flex defines some utility class methods: a few are useful for bulk management, and a few are mostly useful for quick prototyping and debugging.

Curl-like Methods

You can use the methods HEAD, GET, PUT, POST, DELETE exactly as you would do with curl (also enforced by the unconventional upcase naming).

They accept 3 arguments:

For example:

# with JSON
Flex.GET '/_search', '{"query":{"match_all":{}}}'

# same thing with a ruby structure
Flex.GET '/_search', {:query => {:match_all => {}}}

# same thing with YAML
Flex.GET '/_search', <<-yaml
  query:
    match_all: {}
  yaml

# using also tags and variables (for the sake of it)
Flex.GET '/_search', '{"query":{"<<a_tag>>":{}}}', :a_tag => 'match_all'
Flex.GET '/_search',  {:query => {'<<a_tag>>' => {}}}, :a_tag => 'match_all'
Flex.GET '/_search', <<-yaml, :a_tag => 'match_all'
query:
  <<a_tag>>: {}
yaml

Bulk Support

This methods are mostly used internally by the rake tasks (see Rake Tasks), but you may want to use them directly.

Flex.post_bulk_collection

This method accepts a collection of objects as the first argument and a hash of options. It passes each object in the collection (and the options) to the Flex.build_bulk_string and collects the formatted bulk string and posts it.

Notice: If you have an already formatted bulk string you should use Flex.post_bulk_string (see Flex.post_bulk_string).

Flex.build_bulk_string

Accepts an object as the first argument and a hash of options. The object can be a Hash in the same format of the elasticsearch document results, or a record/document (flex-models instance).

You can pass an :action option that can be 'index' (default), 'create', 'update' or 'delete'. It will return the bulk string understood by the elasticsearch bulk API, that can be joined with other bulk strings in order to collect the complete bulk string to pass to the Flex.post_bulk_string method.

Other methods

Flex.reload!

This method is useful in the console, when you are editing some template source: it reloads all the sources, so you can try the changes without restarting the console.

Flex.search

This method allows you to define and use a Search Template (see Flex Search Tempaltes) on the fly. It is very useful in the console or for quick prototyping, but it is not as efficient as a regular Template loaded from source (which gets compiled), so don’t use it in your production code.

For example:

result = Flex.search <<-yaml, :index => 'a_non_default_index'
           query:
             match_all: {}
         yaml

As the curl-like methods, the data can be a JSON or YAML string or a ruby structure, and you can also pass the optional variable hash as usual.

Same as Flex.search but uses a Slim Search Template (see Flex Slim Search Templates).

Flex.json2yaml

Converts a JSON string to a YAML string

Flex.yaml2json

Converts a YAML string to a JSON string

Quasi API Methods

(see Flex Additions)