API Methods

Flex exposes the full elasticsearch API as own class methods. You will need to use them directly only for advanced usage, however Flex uses them internally and creates them with just a few lines of yaml. Indeed they are just generated by Generic Templates (see Flex Generic Templates).

The naming of these methods tries to conform with the original elasticsearch documentation, and adds a few aliases for convenience.

As any template-generated method, they accept a list of variables (hashes) for interpolation (see Variables and Interpolation). This documentation reports the underlying templates and the usage examples complete with all the accepted variable, for completeness. Please, notice that you can omit variables that have defaults.

When an API has flags/parameters, you can pass them as the :params variable; when it has structured data you can pass it as the :data variable.

Notice: you can get the updated full reference and usage example of these methods also in the console at any time, by just writing Flex.doc (for the full API) or Flex.doc :create_index, :get_index_mapping, ... (for specific methods).

This document is updated to elasticsearch version 0.90.2

Core API

Index
Flex.store

Stores a document into the index using PUT.

Template

store:
- PUT
- /<<index>>/<<type>>/<<id>>

Usage

Flex.store :id    => id,               # required
           :type  => nil,
           :index => "flex_test_index"

You must pass the source as the :data variable.

Flex.put_store

Alias of Flex.store (defined for symmetry with Flex.post_store)

Flex.post_store

Stores a document into the index using POST: the :id is assigned by elasticsearch.

Template

post_store:
- POST
- /<<index>>/<<type>>

Usage

Flex.post_store :index => "flex_test_index",
                :type  => nil

You must pass the source as the :data variable.

Delete
Flex.remove

Removes a document from the index.

Template

remove:
- DELETE
- /<<index>>/<<type>>/<<id>>

Usage

Flex.remove :id    => id,               # required
            :type  => nil,
            :index => "flex_test_index"
Get
Flex.get

Retrieves a document from the index.

Template

get:
- GET
- /<<index>>/<<type>>/<<id>>

Usage

Flex.get :id    => id,               # required
         :type  => nil,
         :index => "flex_test_index"
Flex.get_source

Retrieves the source of a document from the index.

Template

get:
- GET
- /<<index>>/<<type>>/<<id>>/_source

Usage

Flex.get_source :id    => id,               # required
                :type  => nil,
                :index => "flex_test_index"
Multi Get
Flex.multi_get

Retrieves multiple documents

Template

multi_get:
- GET
- /<<index>>/<<type>>/_mget
- ids: << ids >>

Usage

Flex.multi_get :ids   => ids,              # required
               :type  => nil,
               :index => "flex_test_index"
Update
Flex.update

Updates a document based on a script provided.

Template

update:
- POST
- /<<index>>/<<type>>/<<id>>/_update

Usage

You must pass script or doc and the other options as the :data variable

Flex.update :id    => id,    # required
            :type  => nil,
            :index => "flex_test_index"
Search
YourClass.your_custom_search

The Search API is completely covered by the Flex Templating system and mostly covered by the flex-scopes gem.

(see Templating and flex-scopes)
Multi Search
YourClass.flex.multi_search

This method is documented here because it is an elasticsearch API method, however it is defined into your flex proxy included by Flex::Templates.

The method allows you to do multi-searching by using multiple templates defined by your class. It takes 2 arguments. The first is a required hash with the templates names as the keys and the variable hash as the values. You can also use an array of arrays. The second is an optional hash of variables that will be used to render the multi_search template. The array of responses is available as responses method.

Template

multi_search:
- GET
- /<<index>>/<<type>>/_msearch

Usage

result = MyClass.flex.multi_search({:my_search_a => {:a_var => 'foo'},
                                    :my_search_b => {:another_var => 'bar'},
                                   {:index => 'another_index'})

result.responses.each{|r| r.do_something }
Percolate
Flex.put_percolator

Registers a named percolator query against an index.

Template

put_percolator:
- PUT
- /_percolator/<<index>>/<<percolator>>

Usage

Flex.put_percolator :percolator => percolator,       # required
                    :index      => "flex_test_index"
Flex.delete_percolator

Unregisters a named percolator.

Template

delete_percolator:
- DELETE
- /_percolator/<<index>>/<<percolator>>

Usage

Flex.delete_percolator :percolator => percolator,       # required
                       :index      => "flex_test_index"
Flex.percolate

Percolates a document.

Template

percolate:
- GET
- /<<index>>/<<type>>/_percolate

Usage

Flex.percolate :index => "flex_test_index",
               :type  => nil

You must pass the document (and additional queries) as the :data variable)

Bulk
Flex.post_bulk_string

Performs many index/create/update/delete operations in a single API call.

Template

post_bulk_string:
- POST
- /_bulk
- << bulk_string >>

Usage

Flex.post_bulk_string :bulk_string => bulk_string   # required

It may return nil if you pass a blank :bulk_string


(see flex:import and Bulk Support)
Count
Flex.count

Gets the number of matches for a query.

Template

count:
- GET
- /<<index>>/<<type>>/_count

Usage

Flex.count :index => "flex_test_index",
           :type  => nil
Delete by Query
Flex.delete_by_query

Allows to delete documents from one or more indices and one or more types based on a query.

Template

delete_by_query:
- DELETE
- /<<index>>/<<type>>/_query

Usage

Flex.delete_by_query :index => "flex_test_index",
                     :type  => nil

You can pass the query as the :data variable

More Like This
Flex.more_like_this

Allows to get documents that are “like” a specified document.

Template

more_like_this:
- GET
- /<<index>>/<<type>>/<<id>>/_mlt

Usage

Flex.more_like_this :id    => id,          # required
                    :index => "flex_test_index",
                    :type  => nil

You can pass the search API (facets, from/to and so on) as the :data variable)

Flex.mlt

Alias for Flex.more_like_this

Validate
Flex.validate

Allows to validate a potentially expensive query without executing it.

Template

validate:
- GET
- /<<index>>/<<type>>/_validate/query

Usage

Flex.validate :index => "flex_test_index",
              :type  => nil

You must pass the query to validate as the :data variable You can pass the parameters as the :params variable

Explain
Flex.explain

Computes a score explanation for a query and a specific document.

Template

explain:
- GET
- /<<index>>/<<type>>/<<id>>/_explain

Usage

Flex.explain :id    => id,    # required
             :type  => nil,
             :index => "flex_test_index"

You must pass the query to explain as the :data variable You can pass the parameters as the :params variable

Flex Additions
Flex.match_all

Not a real API method, but handy:

Template

match_all:
- GET
- /<<index>>/<<type>>/_search
- query:
    match_all: {}

Usage

Flex.match_all :index => "flex_test_index",
               :type  => nil
Flex.search_by_id

Get a document without using the get API (which doesn’t support fields ’*’)

Template

search_by_id:
- GET
- /<<index>>/<<type>>/_search
- query:
    term:
      _id: <<id>>

Usage

Flex.search_by_id :id      => id,    # required
                  :type    => nil,
                  :index   => "flex_test_index"

This method uses the search API, which is not real-time like the get API. You may want to refresh the index with Flex.refresh_index, before calling it.

Flex.scan_search

Generic implementation of the elasticsearch search_type API of type scan. It passes the raw result to the block.

Usage

Flex.scan_search(:my_template, my_vars) do |result|
  result['hits']['hits'].each{|d|do_something_with(d)}
end
Flex.scan_all

Specific implementation of the elasticsearch search_type API of type scan, applied to the match_all template/query. It passes an array of documents to the block.

Usage

Flex.scan_all(my_vars) do |batch|
  batch.each{|d|do_something_with(d)}
end
Flex.dump_all

It flush_index and call scan_all, with added {:params => {:fields => '*,_source'}}. Used to include all the relevant fields from a document:

Usage

Flex.dump_all(my_vars) do |batch|
  batch.each{|d|do_something_with(d)}
end
Flex.dump_one

It flush_index and call search_by_id, with added {:params => {:fields => '*,_source'}}. Used to include all the relevant fields from the document:

Usage

Flex.dump_one :id      => id,    # required
              :type    => nil,
              :index   => "flex_test_index"

Indices API

Aliases
Flex.post_index_aliases

The index aliases API allow to alias an index with a name, with all APIs automatically converting the alias name to the actual index name.

Template

post_index_aliases:
- POST
- /_aliases

Usage

Flex.post_index_aliases

You must pass the aliases API structure as the :data variable

Flex.get_index_aliases

Returns all indices with all aliases, or just for specific indices. (pre 0.90.1).

Template

get_index_aliases:
- GET
- /<<index>>/_aliases

Usage

Flex.get_index_aliases :index => "flex_test_index"
Flex.add_index_alias

Adds an index alias.

Template

add_index_alias:
- PUT
- /<<index>>/_alias/<<alias>>

Usage

Flex.add_index_alias :alias => alias,  # required
                     :index => "flex_test_index"

You can pass other options as the :data variable.

Flex.delete_index_alias

Deletes an index alias.

Template

delete_index_alias:
- DELETE
- /<<index>>/_alias/<<alias>>

Usage

Flex.delete_index_alias :alias => alias,  # required
                        :index => "flex_test_index"
Flex.get_index_alias

Allows to filter by alias name and index name.

Template

get_index_alias:
- GET
- /<<index>>/_alias/<<alias= '*' >>

Usage

Flex.get_index_alias :index => "flex_test_index",
                     :alias => "*"
Analyze
Flex.analyze_index

Performs the analysis process on a text and return the tokens breakdown of the text.

Template

analyze_index:
- GET
- /<<index>>/_analyze

Usage

Flex.analyze_index :index => "flex_test_index"

You can pass the text to analyze as the :data variable (or the param :text). You can pass the parameters as the :params variable.

Create Index
Flex.create_index

Allows to instantiate an index. (Uses PUT)

Template

create_index:
- PUT
- /<<index>>
- settings:
    number_of_shards: <<number_of_shards= 5 >>
    number_of_replicas: <<number_of_replicas= 1 >>

Usage

Flex.create_index :index              => "flex_test_index",
                  :number_of_shards   => 5,
                  :number_of_replicas => 1
Flex.put_index

Alias for Flex.create_index (defined for symmetry with Flex.post_index)

Flex.post_index

Allows to instantiate an index. (Uses POST)

Template

post_index:
- POST
- /<<index>>
- settings:
    number_of_shards: <<number_of_shards= 5 >>
    number_of_replicas: <<number_of_replicas= 1 >>

Usage

Flex.post_index :index              => "flex_test_index",
                :number_of_shards   => 5,
                :number_of_replicas => 1
Delete Index
Flex.delete_index

Allows to delete an existing index.

Template

delete_index:
- DELETE
- /<<index>>

Usage

Flex.delete_index :index => "flex_test_index"
Open/Close Index
Flex.close_index

Allows to close an index.

Template

close_index:
- POST
- /<<index>>/_close

Usage

Flex.close_index :index => "flex_test_index"
Flex.open_index

Allows to open an index.

Template

open_index:
- POST
- /<<index>>/_open

Usage

Flex.open_index :index => "flex_test_index"
Get Settings
Flex.get_index_settings

Allows to retrieve settings of index/indices.

Template

get_index_settings:
- GET
- /<<index>>/_settings

Usage

Flex.get_index_settings :index => "flex_test_index"
Flex.get_settings

Alias for Flex.get_index_settings. (Flex-backward compatible)

Get Mapping
Flex.get_index_mapping

Allows to retrieve the mapping of index/indices.

Template

get_index_mapping:
- GET
- /<<index>>/_mapping

Usage

Flex.get_index_mapping :index => "flex_test_index"
Flex.get_mapping

Alias for Flex.get_index_mapping. (Flex-backward compatible)

Put Mapping
Flex.put_index_mapping

Allows to register specific mapping definition for a specific type.

Template

put_index_mapping: &put_index_mapping
- PUT
- /<<index>>/<<type>>/_mapping
- <<type>>:
    properties: <<properties>>

Usage

Flex.put_index_mapping :properties => properties,  # required
                       :type       => nil,
                       :index      => "flex_test_index"
Flex.put_mapping

Alias for Flex.put_index_mapping. (Flex-backward compatible)

Delete Mapping
Flex.delete_index_mapping

Allows to delete the mapping of an index/type.

Template

delete_index_mapping:
- DELETE
- /<<index>>/<<type>>

Usage

Flex.delete_index_mapping :index => "flex_test_index",
                          :type  => nil
Flex.delete_mapping

Alias for Flex.delete_index_mapping. (Flex-backward compatible)

Refresh
Flex.refresh_index

Allows to explicitly refresh one or more index.

Template

refresh_index:
- POST
- /<<index>>/_refresh

Usage

Flex.refresh_index :index => "flex_test_index"
Optimize
Flex.optimize_index

Allows to optimize one or more index.

Template

refresh_index:
- POST
- /<<index>>/_optimize

Usage

Flex.optimize_index :index => "flex_test_index"

You can pass the parameters as the :params variable.

Flush
Flex.flush_index

Allows to flush one or more index.

Template

flush_index:
- POST
- /<<index>>/_flush

Usage

Flex.flush_index :index => "flex_test_index"

You can pass the parameters as the :params variable.

Snapshot
Flex.gateway_snapshot

Allows to explicitly perform a snapshot through the gateway of one or more indices (backup them).

Template

gateway_snapshot:
 - POST
 - /<<index>>/_gateway/snapshot

Usage

Flex.gateway_snapshot :index => "flex_test_index"
Update
Flex.update_index_settings

Change specific index level settings in real time.

Template

update_index_settings:
- PUT
- /<<index>>/_settings

Usage

Flex.update_index_settings :index => "flex_test_index"

You can pass the settings structure as the :data variable

Templates
Flex.put_index_template

Creates a template that will automatically be applied to new indices created.

Template

put_index_template:
- PUT
- /_template/<<template>>

Usage

Flex.put_index_template :template => template  # required

You can pass the data structure as the :data variable

Flex.delete_index_template

Deletes an existing template.

Template

delete_index_template:
- DELETE
- /_template/<<template>>

Usage

Flex.delete_index_template :template => template  # required
Flex.get_index_template

Retrieves an existing template.

Template

get_index_template:
- GET
- /_template/<<template>>

Usage

Flex.get_index_template :template => template  # required
Warmers
Flex.put_index_warmer

Creates a warmer to warm up the index before it is available for search.

Template

put_index_warmer:
- PUT
- /<<index>>/<<type>>/_warmer/<<warmer>>

Usage

Flex.put_warmer :warmer => warmer,  # required
                :type   => nil,
                :index  => "flex_test_index"

You can pass the data structure as the :data variable

Flex.delete_index_warmer

Deletes an existing warmer.

Template

delete_index_warmer:
- DELETE
- /<<index>>/_warmer/<<warmer= ~ >>

Usage

Flex.delete_warmer :index  => "flex_test_index",
                   :warmer => nil
      
Flex.get_index_warmer

Retrieves an existing warmer.

Template

get_index_warmer:
- GET
- /<<index>>/_warmer/<<warmer= ~ >>

Usage

Flex.get_warmer :index  => "flex_test_index",
                :warmer => nil
Stats
Flex.index_stats

Provides statistics on different operations happening on an index.

Template

index_stats:
- GET
- /<<index>>/_stats/<<endpoint= ~ >>/<<type= ~ >>

Usage

Flex.index_stats :index    => "flex_test_index",
                 :endpoint => nil,
                 :type     => nil
Flex.stats

Alias for Flex.index_stats. (Flex-backward compatible)

Status
Flex.index_status

Provides a comprehensive status information of one or more indices.

Template

index_status:
- GET
- /<<index>>/_status

Usage

Flex.index_status :index => "flex_test_index"
Segments
Flex.index_segments

Provides low level segments information that a Lucene index (shard level) is built with.

Template

index_segments:
- GET
- /<<index>>/_segments

Usage

Flex.index_segments :index => "flex_test_index"
Clear Cache
Flex.index_clearcache

Clears either all caches or specific cached associated with one ore more indices.

Template

index_clearcache:
- POST
- /<<index>>/_cache/clear

Usage

Flex.index_clearcache :index => "flex_test_index"
Indices Exists
Flex.indices_exists

Checks if the index/indices exists.

Template

indices_exists:
- HEAD
- /<<index>>

Usage

Flex.indices_exists :index => "flex_test_index"

Notice: the result is a boolean

Flex.exist?

Alias for Flex.indices_exists. (Flex-backward compatible)

Types Exists
Flex.types_exists

Checks if the type/types exists in an index/indices.

Template

types_exists:
- HEAD
- /<<index>>/<<type>>

Usage

Flex.types_exists :index => "flex_test_index",
                  :type  => nil

Notice: the result is a boolean

Flex.exist?

Alias for Flex.indices_exists. (Flex-backward compatible)

Cluster API

Health
Flex.cluster_health

Gets a very simple status on the health of the cluster.

Template

cluster_health:
- GET
- /_cluster/health/<<index>>

Usage

Flex.cluster_health :index => "flex_test_index"

You can pass the params as the :params variable

State
Flex.cluster_state

Gets a comprehensive state information of the whole cluster.

Template

cluster_state:
- GET
- /_cluster/state

Usage

Flex.cluster_state

You can pass the params as the :params variable

Update Settings
Flex.put_cluster_settings

Updates cluster wide specific settings.

Template

put_cluster_settings:
- PUT
- /_cluster/settings

Usage

Flex.put_cluster_settings

You can pass the settings structure as the :data variable

Flex.get_cluster_settings

Retrieves cluster settings.

Template

get_cluster_settings:
- GET
- /_cluster/settings

Usage

Flex.get_cluster_settings
Nodes Info
Flex.cluster_nodes_info

Retrieves one or more (or all) of the cluster nodes information.

Template

cluster_nodes_info:
- GET
- /_nodes/<<nodes= ~ >>/<<endpoint= ~ >>

Usage

Flex.cluster_nodes_info :nodes    => nil,
                        :endpoint => nil
Nodes Stats
Flex.cluster_nodes_info

Retrieves one or more (or all) of the cluster nodes statistics.

Template

cluster_nodes_stats:
- GET
- /_nodes/<<nodes= ~ >>/stats/<<endpoint= ~ >>

Usage

Flex.cluster_nodes_stats :nodes    => nil,
                         :endpoint => nil
Nodes Shutdown
Flex.cluster_nodes_shutdown

Shutdown one or more (or all) nodes in the cluster.

Template

cluster_nodes_shutdown:
- POST
- /_cluster/nodes/<<nodes= ~ >>/_shutdown

Usage

Flex.cluster_nodes_shutdown :nodes => nil
Nodes Hot Threads
Flex.cluster_nodes_hot_threads

Gets the current hot threads on each node in the cluster.

Template

cluster_nodes_hot_threads:
- GET
- /_nodes/<<nodes= ~ >>/hot_threads

Usage

Flex.cluster_nodes_hot_threads :nodes => nil
Cluster Reroute
Flex.cluster_reroute

Explicitly execute a cluster reroute allocation command including specific commands.

Template

cluster_reroute:
- POST
- /_cluster/reroute

Usage

Flex.cluster_reroute

You can pass the data structure as the :data variable

If you find any elasticsearch API missing from this list, please, let me know and I will add it right away (it should be just a matter of a few minutes)