Module: ElasticSearch

Defined in:
lib/elastic_search.rb,
lib/elastic_search/bulk.rb,
lib/elastic_search/index.rb,
lib/elastic_search/model.rb,
lib/elastic_search/config.rb,
lib/elastic_search/result.rb,
lib/elastic_search/version.rb,
lib/elastic_search/relation.rb,
lib/elastic_search/response.rb,
lib/elastic_search/http_client.rb,
lib/elastic_search/filterable_relation.rb,
lib/elastic_search/aggregation_relation.rb,
lib/elastic_search/aggregatable_relation.rb,
lib/elastic_search/post_filterable_relation.rb

Defined Under Namespace

Modules: AggregatableRelation, FilterableRelation, Index, Model, PostFilterableRelation Classes: AggregationRelation, Bulk, ConnectionError, HTTPClient, NotSupportedError, Relation, Response, ResponseError, Result

Constant Summary collapse

Config =
{
  index_prefix: nil,
  base_url: "http://127.0.0.1:9200",
  bulk_limit: 1_000,
  auto_refresh: false
}
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.aliases(payload) ⇒ ElasticSearch::Response

Used to manipulate, ie add and remove index aliases. Raises an ElasticSearch::ResponseError in case any errors occur.

Examples:

ElasticSearch.post_aliases(actions: [
  { remove: { index: "test1", alias: "alias1" }},
  { add: { index: "test2", alias: "alias1" }}
])

76
77
78
# File 'lib/elastic_search.rb', line 76

def self.aliases(payload)
  ElasticSearch::HTTPClient.headers(accept: "application/json").post("#{ElasticSearch::Config[:base_url]}/_aliases", body: JSON.generate(payload))
end

.msearch(relations) ⇒ Array<ElasticSearch::Response>

Uses the ElasticSearch Multi Search API to execute multiple search requests within a single request. Raises ElasticSearch::ResponseError in case any errors occur.

Examples:

ElasticSearch.msearch [ProductIndex.match_all, CommentIndex.match_all]

50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/elastic_search.rb', line 50

def self.msearch(relations)
  payload = relations.flat_map do |relation|
    [JSON.generate(index: relation.target.index_name_with_prefix, type: relation.target.type_name), JSON.generate(relation.request)]
  end

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

  ElasticSearch::HTTPClient.headers(accept: "application/json", content_type: "application/x-ndjson").post("#{ElasticSearch::Config[:base_url]}/_msearch", body: payload).parse["responses"].map.with_index do |response, index|
    ElasticSearch::Response.new(relations[index], response)
  end
end

.versionString

Queries and returns the ElasticSearch server version used.

Examples:

ElasticSearch.version # => e.g. 2.4.1

10
11
12
# File 'lib/elastic_search/config.rb', line 10

def self.version
  @version ||= ElasticSearch::HTTPClient.get("#{Config[:base_url]}/").parse["version"]["number"]
end