API Methods

Elastics exposes the full elasticsearch API as own class methods. You will need to use them directly only for advanced usage, however Elastics uses them internally and creates them with just a few lines of yaml. Indeed they are just generated by Generic Templates (see Elastics 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 Elastics.doc (for the full API) or Elastics.doc :create_index, :get_index_mapping, ... (for specific methods). You can also get only the usage with Elastics.usage, and search for some pattern or string (part of any API method) with Elastics.find('get').

This document is updated to elasticsearch version 1.7.3

Document API

Index API
Elastics.store

Template

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

Usage

Elastics.store :id    => id,   # required
               :type  => nil,  
               :index => nil

You must pass the :data variable.

Aliases

put_store
Elastics.post_store

Template

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

Usage

Elastics.post_store :index => nil,  
                    :type  => nil

The id is assigned by elasticsearch; you must pass the :data variable.

Aliases

put_store
Get API
Elastics.get

Template

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

Usage

Elastics.get :id    => id,   # required
             :type  => nil,  
             :index => nil
Elastics.get_source

Template

get_source:
- GET
- "/<<index>>/<<type>>/<<id>>/_source"

Usage

Elastics.get_source :id    => id,   # required
                    :type  => nil,  
                    :index => nil
Delete API
Elastics.delete

Template

delete:
- DELETE
- "/<<index>>/<<type>>/<<id>>"

Usage

Elastics.delete :id    => id,   # required
                :type  => nil,  
                :index => nil

You must pass the :data variable.

Aliases

remove
Update API
Elastics.update

Template

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

Usage

Elastics.update :id    => id,   # required
                :type  => nil,  
                :index => nil

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

Multi Get API
Elastics.multi_get_ids

Template

multi_get_ids:
- GET
- "/<<index>>/<<type>>/_mget"
- ids: "<< ids >>"

Usage

Elastics.multi_get_ids :ids   => ids,  # required
                       :type  => nil,  
                       :index => nil

You must pass the :ids array.

Aliases

multi_get
Elastics.multi_get_docs

Template

multi_get_docs:
- GET
- "/<<index>>/<<type>>/_mget"
- docs: "<< docs >>"

Usage

Elastics.multi_get_docs :docs  => docs,  # required
                        :type  => nil,   
                        :index => nil

You must pass the :docs array.

Aliases

multi_get
Bulk API
Elastics.post_bulk_string

Template

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

Usage

Elastics.post_bulk_string :bulk_string => bulk_string  # required

You must pass the :docs array.

Aliases

bulk
Term Vectors
Elastics.termvector

Template

termvector:
- POST
- "/<<index>>/<<type>>/<<id>>/_termvector"

Usage

Elastics.termvector :id    => id,   # required
                    :type  => nil,  
                    :index => nil

You must pass the options as the :data variable.

Multi Termvectors API
Elastics.multi_termvectors

Template

multi_termvectors:
- POST
- "/<<index>>/<<type>>/_mtermvectors"
- docs: "<<docs= ~ >>"

Usage

Elastics.multi_termvectors :index => nil,  
                           :type  => nil,  
                           :docs  => nil

You must pass the docs array as the :docs variable or use the :data variable for the simplified request.

Indices API

Create Index
Elastics.put_index

Template

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

Usage

Elastics.put_index :index              => nil,  
                   :number_of_shards   => 5,    
                   :number_of_replicas => 1

You can also pass the complete data structure as :data variable, so passing settings, mappings, warmers, aliases, ...

Aliases

create_index
Elastics.post_index

Template

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

Usage

Elastics.post_index :index              => nil,  
                    :number_of_shards   => 5,    
                    :number_of_replicas => 1

You can also pass the complete data structure as :data variable, so passing settings, mappings, warmers, aliases, ...

Aliases

create_index
Delete Index
Elastics.delete_index

Template

delete_index:
- DELETE
- "/<<index>>"

Usage

Elastics.delete_index :index => nil
Get Index
Elastics.get_index

Template

get_index:
- GET
- "/<<index>>/<<features= ~ >>"

Usage

Elastics.get_index :index    => nil,  
                   :features => nil
Indices Exists
Elastics.indices_exists

Template

indices_exists:
- HEAD
- "/<<index>>"

Usage

Elastics.indices_exists :index => nil

Aliases

index_exists exist?
Open/Close Index
Elastics.close_index

Template

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

Usage

Elastics.close_index :index => nil
Elastics.open_index

Template

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

Usage

Elastics.open_index :index => nil
Put Mappings
Elastics.put_index_mapping

Template

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

Usage

Elastics.put_index_mapping :properties => properties,  # required
                           :type       => nil,         
                           :index      => nil

Aliases

put_mappings
Get Mappings
Elastics.get_index_mapping

Template

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

Usage

Elastics.get_index_mapping :index => nil,  
                           :type  => nil
Get Field Mappings
Elastics.get_field_mapping

Template

get_field_mapping:
- GET
- "/<<index>>/_mapping/<<type>>/field/<<field>>"

Usage

Elastics.get_field_mapping :field => field,  # required
                           :type  => nil,    
                           :index => nil
Types Exists
Elastics.types_exists

Template

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

Usage

Elastics.types_exists :index => nil,  
                      :type  => nil

Aliases

type_exists
Delete Mappings
Elastics.delete_index_mapping

Template

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

Usage

Elastics.delete_index_mapping :index => nil,  
                              :type  => nil
Index Aliases
Elastics.get_index_aliases

Template

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

Usage

Elastics.get_index_aliases :index => nil
Elastics.post_index_aliases

Template

post_index_aliases:
- POST
- "/_aliases"
- actions: "<<actions>>"

Usage

Elastics.post_index_aliases :actions => actions  # required

This is the generic interface allowing all the options. You can pass the :actions array or the aliases API structure as the :data variable.

Elastics.delete_index_alias

Template

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

Usage

Elastics.delete_index_alias :alias => alias,  # required
                            :index => nil
Elastics.get_index_alias

Template

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

Usage

Elastics.get_index_alias :index => nil,  
                         :alias => "*"
Elastics.put_index_alias

Template

put_index_alias:
- PUT
- "/<<index>>/_alias/<<alias>>"

Usage

Elastics.put_index_alias :alias => alias,  # required
                         :index => nil

This is the single index alias. You can pass other options as the :data variable.

Update Indices Settings
Elastics.update_index_settings

Template

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

Usage

Elastics.update_index_settings :index => nil

You can pass the settings structure as the :data variable.

Aliases

put_index_settings
Get Indices Settings
Elastics.get_index_settings

Template

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

Usage

Elastics.get_index_settings :index => nil

Aliases

get_settings
Analyze
Elastics.analyze_index

Template

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

Usage

Elastics.analyze_index :index => nil

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

Index Templates
Elastics.delete_index_template

Template

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

Usage

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

Template

get_index_template:
- GET
- "/_template/<<template= ~ >>"

Usage

Elastics.get_index_template :template => nil
Elastics.index_template_exists

Template

index_template_exists:
- HEAD
- "/_template/<<template>>"

Usage

Elastics.index_template_exists :template => template  # required
Elastics.put_index_template

Template

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

Usage

Elastics.put_index_template :template => template  # required

You must pass the template structure as the :data variable.

Warmers
Elastics.delete_index_warmer

Template

delete_index_warmer:
- DELETE
- "/<<index>>/_warmer/<<warmer>>"

Usage

Elastics.delete_index_warmer :warmer => warmer,  # required
                             :index  => nil
Elastics.put_index_warmer

Template

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

Usage

Elastics.put_index_warmer :warmer => warmer,  # required
                          :type   => nil,     
                          :index  => nil

You must pass the warmer structure as the :data variable.

Elastics.get_index_warmer

Template

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

Usage

Elastics.get_index_warmer :index  => nil,  
                          :warmer => nil
Status
Elastics.index_status

Template

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

Usage

Elastics.index_status :index => nil
Stats
Elastics.index_stats

Template

index_stats:
- GET
- "/<<index>>/_stats/<<stats= ~ >>"

Usage

Elastics.index_stats :index => nil,  
                     :stats => nil
Segments
Elastics.index_segments

Template

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

Usage

Elastics.index_segments :index => nil
Recovery
Elastics.index_recovery

Template

index_recovery:
- GET
- "/<<index>>/_recovery"

Usage

Elastics.index_recovery :index => nil
Elastics.index_clearcache

Template

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

Usage

Elastics.index_clearcache :index => nil
Flush
Elastics.flush_index

Template

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

Usage

Elastics.flush_index :index => nil

You can pass the parameters as the :params variable.

Synced Flush
Elastics.synced_flush_index

Template

synced_flush_index:
- POST
- "/<<index>>/_flush/synced"

Usage

Elastics.synced_flush_index :index => nil

You can pass the parameters as the :params variable.

Refresh
Elastics.refresh_index

Template

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

Usage

Elastics.refresh_index :index => nil
Optimize
Elastics.optimize_index

Template

optimize_index:
- POST
- "/<<index>>/_optimize"

Usage

Elastics.optimize_index :index => nil

You can pass the parameters as the :params variable.

Upgrade
Elastics.upgrade_index

Template

upgrade_index:
- POST
- "/<<index>>/_upgrade"

Usage

Elastics.upgrade_index :index => nil

You can pass the parameters as the :params variable.

Search API

Search Shards
Elastics.search_shards

Template

search_shards:
- GET
- "/<<index>>/<<type>>/_search_shards"

Usage

Elastics.search_shards :index => nil,  
                       :type  => nil

You can pass the parameters as the :params variable.

Elastics.count

Template

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

Usage

Elastics.count :index => nil,  
               :type  => nil

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

Search Exists
Elastics.search_exists

Template

search_exists:
- GET
- "/<<index>>/<<type>>/<<id= ~ >>/_search/exists"

Usage

Elastics.search_exists :index => nil,  
                       :type  => nil,  
                       :id    => nil

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

Validate
Elastics.validate

Template

validate:
- GET
- "/<<index>>/<<type>>/<<id= ~ >>/_validate/query"

Usage

Elastics.validate :index => nil,  
                  :type  => nil,  
                  :id    => nil

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

Elastics.explain

Template

explain:
- GET
- "/<<index>>/<<type>>/<<id= ~ >>/_explain"

Usage

Elastics.explain :index => nil,  
                 :type  => nil,  
                 :id    => nil

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

Percolator
Elastics.delete_percolator

Template

delete_percolator:
- DELETE
- "/<<index>>/.percolator/<<id>>"

Usage

Elastics.delete_percolator :id    => id,  # required
                           :index => nil
Elastics.percolate

Template

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

Usage

Elastics.percolate :index => nil,  
                   :type  => nil

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

Elastics.percolate_count

Template

percolate_count:
- GET
- "/<<index>>/<<type>>/_percolate/count"

Usage

Elastics.percolate_count :index => nil,  
                         :type  => nil

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

Elastics.put_percolator

Template

put_percolator:
- PUT
- "/<<index>>/.percolator/<<id>>"

Usage

Elastics.put_percolator :id    => id,  # required
                        :index => nil

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

More Like This
Elastics.more_like_this

Template

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

Usage

Elastics.more_like_this :id    => id,   # required
                        :type  => nil,  
                        :index => nil

You can pass the search API as :data variable.

Aliases

mlt

Cat API

Cat API
Elastics.cat

Template

cat:
- GET
- "/"

Usage

Elastics.cat

You must pass the :path variable or you can pass it as a simple string argument: Elastics.cat('/path')

Cluster API

Health
Elastics.cluster_health

Template

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

Usage

Elastics.cluster_health :index => nil

You can pass the params as the :params variable.

State
Elastics.cluster_state

Template

cluster_state:
- GET
- "/_cluster/state/<<metrics= _all >>/<<index>>"

Usage

Elastics.cluster_state :metrics => "_all",  
                       :index   => nil

You can pass the metrics as the :metrics variable.

Stats
Elastics.cluster_stats

Template

cluster_stats:
- GET
- "/_cluster/stats"

Usage

Elastics.cluster_stats

You can pass the params as the :params variable.

Pending Cluster Tasks
Elastics.cluster_pending_tasks

Template

cluster_pending_tasks:
- GET
- "/_cluster/pending_tasks"

Usage

Elastics.cluster_pending_tasks

You can pass the params as the :params variable.

Cluster Reroute
Elastics.cluster_reroute

Template

cluster_reroute:
- POST
- "/_cluster/reroute"

Usage

Elastics.cluster_reroute

You must pass the data structure as the :data variable.

Cluster Update Settings
Elastics.put_cluster_settings

Template

put_cluster_settings:
- PUT
- "/_cluster/settings"

Usage

Elastics.put_cluster_settings

You must pass the data structure as the :data variable.

Nodes Stats
Elastics.cluster_nodes_stats

Template

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

Usage

Elastics.cluster_nodes_stats :nodes => nil,  
                             :stats => nil

You can pass the params as the :params variable. You must pass the data structure as the :data variable.

Nodes Info
Elastics.cluster_nodes_info

Template

cluster_nodes_info:
- GET
- "/_nodes/<<nodes= ~ >>/<<info= ~ >>"

Usage

Elastics.cluster_nodes_info :nodes => nil,  
                            :info  => nil

This template uses the _nodes shortcut.

Nodes Hot Threads
Elastics.cluster_nodes_hot_threads

Template

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

Usage

Elastics.cluster_nodes_hot_threads :nodes => nil

Elastics Additions

Elastics Additions
Elastics.match_all

Not a real API method, but handy:

Template

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

Usage

Elastics.match_all :index => "elastics_test_index",
                   :type  => nil
Elastics.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

Elastics.search_by_id :id      => id,    # required
                      :type    => nil,
                      :index   => "elastics_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 Elastics.refresh_index, before calling it.

YourClass.elastics.multi_search

This method is documented here because it is an elasticsearch API method, however it is defined into your elastics proxy included by Elastics::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.elastics.multi_search({:my_search_a => {:a_var => 'foo'},
                                        :my_search_b => {:another_var => 'bar'},
                                       {:index => 'another_index'})

result.responses.each{|r| r.do_something }
Elastics.scan_search

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

Usage

Elastics.scan_search(:my_template, my_vars) do |result|
  result['hits']['hits'].each{|d|do_something_with(d)}
end
Elastics.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

Elastics.scan_all(my_vars) do |batch|
  batch.each{|d|do_something_with(d)}
end
Elastics.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

Elastics.dump_all(my_vars) do |batch|
  batch.each{|d|do_something_with(d)}
end
Elastics.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

Elastics.dump_one :id      => id,    # required
                  :type    => nil,
                  :index   => "elastics_test_index"

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)