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.

Parameters:

  • params (Hash)

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

Returns:

  • (Client)

    the current value of client



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

Returns:



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

def document_type
  @document_type
end

#logBoolean (readonly)

print degub log to $stdout

Returns:

  • (Boolean)

    the current value of log



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

def log
  @log
end

#nameString (readonly)

the name of the index

Returns:

  • (String)

    the current value of name



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

def name
  @name
end

#settingsHash (readonly)

the index settings

Returns:

  • (Hash)

    the current value of settings



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

def settings
  @settings
end

#urlString (readonly)

the url to elasticsearch

Returns:

  • (String)

    the current value of url



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

def url
  @url
end

Instance Method Details

#aggregate(query) ⇒ Hash

Executes a search request and returns the response

Parameters:

  • query (Hash)

Returns:

  • (Hash)


177
178
179
180
181
182
# File 'lib/elastic_adapter/index.rb', line 177

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

#count(query = { query: { match_all: {} } }) ⇒ Hash

Returns the document count for the index

Parameters:

  • query (Hash) (defaults to: { query: { match_all: {} } })

    a query to count the documents for a given query. Defaults to match all

Returns:

  • (Hash)

    the count

See Also:



83
84
85
# File 'lib/elastic_adapter/index.rb', line 83

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

#create_indexHash

Creates the index with it’s settings and mappings

Returns:

  • (Hash)

See Also:



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

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

#delete_indexHash

Deletes the index

Returns:

  • (Hash)

See Also:



73
74
75
# File 'lib/elastic_adapter/index.rb', line 73

def delete_index
  client.indices.delete index: name
end

#get(id) ⇒ ElasticAdapter::HitDecorator

Returns the document with the given id from the index

Parameters:

  • id (Integer)

Returns:

  • (ElasticAdapter::HitDecorator)

See Also:



117
118
119
120
121
122
123
# File 'lib/elastic_adapter/index.rb', line 117

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

#index(document) ⇒ Hash

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")

Parameters:

  • document (Hash)

Returns:

  • (Hash)

See Also:



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/elastic_adapter/index.rb', line 98

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

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

  client.index(params)
end

#search(query) ⇒ Hash

Searches the index for documents matching the passed query.

Examples:

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

Parameters:

  • query (Hash)

Returns:

  • (Hash)

See Also:



135
136
137
138
139
140
# File 'lib/elastic_adapter/index.rb', line 135

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

#suggest(query) ⇒ Hash

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"}})

Parameters:

  • query (Hash)

Returns:

  • (Hash)

See Also:



152
153
154
155
156
157
# File 'lib/elastic_adapter/index.rb', line 152

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

#validate(query) ⇒ Hash

Validates the passed query

Parameters:

  • query (Hash)

Returns:

  • (Hash)

See Also:



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

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