Class: Wukong::Elasticsearch::IndexAndMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/wonderdog/index_and_mapping.rb

Overview

A convenient class for parsing Elasticsearch index and mapping URIs like

  • es://my_index
  • es://my_index/my_mapping
  • es://first_index,second_index,third_index
  • es://my_index/first_mapping,second_mapping,third_mapping

Constant Summary collapse

ES_SCHEME_REGEXP =

A regular expression that matches URIs describing an Elasticsearch index and/or mapping to read/write from/to.

%r{^es://}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ IndexAndMapping

Create a new index and mapping specification from the given +uri..

Parameters:

  • uri (String)


43
44
45
# File 'lib/wonderdog/index_and_mapping.rb', line 43

def initialize uri
  self.uri = uri
end

Instance Attribute Details

#indexObject (readonly)

The Elasticsearch index.

Parameters:

  • (String)


22
23
24
# File 'lib/wonderdog/index_and_mapping.rb', line 22

def index
  @index
end

#mappingObject (readonly)

The Elasticsearch mapping.

Parameters:

  • (String)


27
28
29
# File 'lib/wonderdog/index_and_mapping.rb', line 27

def mapping
  @mapping
end

Class Method Details

.matches?(string) ⇒ true, false

Does the given string look like a possible Elasticsearch /index/mapping specification?

Parameters:

  • string (String)

Returns:

  • (true, false)


34
35
36
37
# File 'lib/wonderdog/index_and_mapping.rb', line 34

def self.matches? string
  return false unless string
  string =~ ES_SCHEME_REGEXP
end

Instance Method Details

#uri=(uri) ⇒ Object

Set the URI of this index and mapping specification, parsing it for an index and mapping.

Will raise an error if the given URI is malformed.

Parameters:

  • uri (String)

Raises:

  • (Wukong::Error)


53
54
55
56
57
58
59
60
61
# File 'lib/wonderdog/index_and_mapping.rb', line 53

def uri= uri
  raise Wukong::Error.new("'#{uri}' is not an ElasticSearch es://index/mapping specification") unless self.class.matches?(uri)
  parts = uri.gsub(ES_SCHEME_REGEXP, '').gsub(/^\/+/,'').gsub(/\/+$/,'').split('/')
  
  raise Wukong::Error.new("'#{uri}' is not an ElasticSearch es://index/mapping specification") unless parts.size.between?(1,2)

  @index   = parts[0]
  @mapping = parts[1]
end