Class: ROM::Elasticsearch::Gateway

Inherits:
Gateway
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/elasticsearch/gateway.rb

Overview

Elasticsearch gateway

Examples:

basic configuration

conf = ROM::Configuration.new(:elasticsearch, 'http://localhost:9200')

class Posts < ROM::Relation[:elasticsearch]
  schema(:posts) do
    attribute :id, Types::Int
    attribute :title, Types::String

    primary_key :id
  end

  def like(title)
    query(prefix: { title: title })
  end
end

conf.register_relation(Posts)

rom = ROM.container(conf)

posts = rom.relations[:posts]

posts.command(:create).call(id: 1, title: 'Hello World')

posts.like('Hello').first

using an existing client

client = Elasticsearch::Client.new('http://localhost:9200')
conf = ROM::Configuration.new(:elasticsearch, client: client)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject (readonly)



57
58
59
# File 'lib/rom/elasticsearch/gateway.rb', line 57

def client
  @client
end

#urlObject (readonly)



53
54
55
# File 'lib/rom/elasticsearch/gateway.rb', line 53

def url
  @url
end

Instance Method Details

#dataset(index) ⇒ Dataset Also known as: []

Get a dataset by its :index name

Parameters:

  • index (Symbol)

    The name of the index

Returns:



82
83
84
85
# File 'lib/rom/elasticsearch/gateway.rb', line 82

def dataset(index)
  idx_name = IndexName[index]
  Dataset.new(client, params: {index: idx_name.to_sym})
end

#dataset?(index) ⇒ Boolean Also known as: index?

Return true if a dataset with the given :index exists

Parameters:

  • index (Symbol)

    The name of the index

Returns:

  • (Boolean)


70
71
72
# File 'lib/rom/elasticsearch/gateway.rb', line 70

def dataset?(index)
  client.indices.exists?(index: index)
end