Class: ElasticAdapter::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_adapter/index.rb

Overview

This class encapsulates the access to a Elasticsearch::Transport::Client and provides an implementation of the repository pattern

Examples:

Initialization

index = ElasticAdapter::Index.new(
  name: "test_index",
  settings: { number_of_shards: 1 },
  document_type: ElasticAdapter::DocumentType.new("test_doc", mappings: {
      test_doc: {
        properties: {
          foo: { type: "string" }
        }
      }
    }
  ),
  url: "localhost:9200",
  log: true
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Index

Returns a new instance of Index.

Options Hash (params):

  • :name (String)

    required

  • :settings (Hash)

    required

  • :document_type (DocumentType)

    required

  • :url (String)

    required

  • :log (Boolean)

    required

  • client (Client)

    optional: A client. Defaults to Elasticsearch#Transport#Client


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/elastic_adapter/index.rb', line 40

def initialize(params)
  @name = params.fetch(:name)
  @settings = params.fetch(:settings)
  @document_type = params.fetch(:document_type)
  @url = params.fetch(:url)
  @log = params.fetch(:log)
  @client = params.fetch :client do
    Elasticsearch::Client.new(url: url, log: log)
  end

  self
end

Instance Attribute Details

#clientClient (readonly)

the client who handles the communication with elasticsearch


29
30
31
# File 'lib/elastic_adapter/index.rb', line 29

def client
  @client
end

#document_typeDocumentType (readonly)

a DocumentType with the document name and it's mappings


29
30
31
# File 'lib/elastic_adapter/index.rb', line 29

def document_type
  @document_type
end

#logBoolean (readonly)

print degub log to $stdout


29
30
31
# File 'lib/elastic_adapter/index.rb', line 29

def log
  @log
end

#nameString (readonly)

the name of the index


29
30
31
# File 'lib/elastic_adapter/index.rb', line 29

def name
  @name
end

#settingsHash (readonly)

the index settings


29
30
31
# File 'lib/elastic_adapter/index.rb', line 29

def settings
  @settings
end

#urlString (readonly)

the url to elasticsearch


29
30
31
# File 'lib/elastic_adapter/index.rb', line 29

def url
  @url
end

Instance Method Details

#count(query = { query: { match_all: {} } }) ⇒ Decoration::CountResponse

Returns the document count for the index


87
88
89
90
91
# File 'lib/elastic_adapter/index.rb', line 87

def count(query = { query: { match_all: {} } })
  handle_api_call do
    client.count index: name, body: query
  end
end

#create_indexElasticAdapter::Response

Creates the index with it's settings and mappings


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/elastic_adapter/index.rb', line 58

def create_index
  handle_api_call do
    client.indices.create(
      index: name,
      body: {
        mappings: document_type.mappings,
        settings: settings
      }
    )
  end
end

#delete_indexElasticAdapter::Response

Deletes the index


75
76
77
78
79
# File 'lib/elastic_adapter/index.rb', line 75

def delete_index
  handle_api_call do
    client.indices.delete index: name
  end
end

#get(id) ⇒ ElasticAdapter::HitDecorator

Returns the document with the given id from the index


127
128
129
130
131
132
133
134
135
# File 'lib/elastic_adapter/index.rb', line 127

def get(id)
  handle_api_call do
    client.get(
      index: name,
      type: document_type.name,
      id: id
    )
  end
end

#index(document) ⇒ Response

Indexes a Hash or anything that responds to to_hash as a document in the index

Examples:

test_index = ElasticAdapter::Index.new(...)
test_index.index(id: 1, name: "foo")

See Also:


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/elastic_adapter/index.rb', line 104

def index(document)
  doc = document.to_hash.merge({})

  params = {
    index: name,
    id: doc.delete(:id),
    type: document_type.name,
    body: doc
  }

  response = handle_api_call do
    client.index(params)
  end

  response
end

#search(query) ⇒ ElasticAdapter::SearchResponse

Searches the index for documents matching the passed query.

Examples:

test_index = ElasticAdatper::Index.new(...)
test_index.seach(query: {match: {foo: "bar"}})

See Also:


147
148
149
150
151
152
153
154
# File 'lib/elastic_adapter/index.rb', line 147

def search(query)
  handle_api_call do
    client.search(
      index: name,
      body: query
    )
  end
end

#suggest(query) ⇒ ElasticAdapter::SuggestResponse

Searches the index for suggestions for the passed suggest query

Examples:

test_index = ElasticAdatper::Index.new(...)
test_index.seach(name_suggestions: {text: "foo", completion: {field: "name"}})

See Also:


166
167
168
169
170
171
172
173
# File 'lib/elastic_adapter/index.rb', line 166

def suggest(query)
  handle_api_call do
    client.suggest(
      index: name,
      body: query
    )
  end
end

#validate(query) ⇒ ElasticAdapter::ValidationResponse

Validates the passed query


181
182
183
184
185
186
187
188
189
# File 'lib/elastic_adapter/index.rb', line 181

def validate(query)
  handle_api_call do
    client.indices.validate_query(
      index: name,
      explain: true,
      body: query
    )
  end
end