Class: Cauchy::Elastic::Index
- Inherits:
-
Object
- Object
- Cauchy::Elastic::Index
- Defined in:
- lib/cauchy/elastic/index.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #alias=(alias_name) ⇒ Object
- #aliases ⇒ Object
- #close ⇒ Object
- #create(settings: {}, mappings: {}) ⇒ Object
- #delete ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(server, name) ⇒ Index
constructor
A new instance of Index.
- #mappings ⇒ Object
- #mappings=(mappings) ⇒ Object
- #open ⇒ Object
- #scroll(options = {}) ⇒ Object
- #settings ⇒ Object
- #settings=(settings) ⇒ Object
Constructor Details
#initialize(server, name) ⇒ Index
Returns a new instance of Index.
26 27 28 29 |
# File 'lib/cauchy/elastic/index.rb', line 26 def initialize(server, name) @server = server @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/cauchy/elastic/index.rb', line 24 def name @name end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
24 25 26 |
# File 'lib/cauchy/elastic/index.rb', line 24 def server @server end |
Instance Method Details
#alias=(alias_name) ⇒ Object
51 52 53 |
# File 'lib/cauchy/elastic/index.rb', line 51 def alias=(alias_name) server.indices.put_alias index: name, name: alias_name end |
#aliases ⇒ Object
35 36 37 |
# File 'lib/cauchy/elastic/index.rb', line 35 def aliases get 'aliases' end |
#close ⇒ Object
91 92 93 |
# File 'lib/cauchy/elastic/index.rb', line 91 def close server.indices.close index: name end |
#create(settings: {}, mappings: {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cauchy/elastic/index.rb', line 72 def create(settings: {}, mappings: {}) server.indices.create index: name, body: { settings: settings, mappings: mappings } rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e if e. =~ /IndexAlreadyExists/ raise IndexAlreadyExistsError.new(name) else raise end end |
#delete ⇒ Object
83 84 85 |
# File 'lib/cauchy/elastic/index.rb', line 83 def delete server.indices.delete index: name end |
#exists? ⇒ Boolean
31 32 33 |
# File 'lib/cauchy/elastic/index.rb', line 31 def exists? server.indices.exists? index: name end |
#mappings ⇒ Object
39 40 41 42 43 |
# File 'lib/cauchy/elastic/index.rb', line 39 def mappings get('mapping')[name]['mappings'] rescue {} end |
#mappings=(mappings) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/cauchy/elastic/index.rb', line 55 def mappings=(mappings) mappings.each do |type, mapping| server.indices.put_mapping index: name, type: type, body: { type => mapping } end end |
#open ⇒ Object
87 88 89 |
# File 'lib/cauchy/elastic/index.rb', line 87 def open server.indices.open index: name end |
#scroll(options = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/cauchy/elastic/index.rb', line 95 def scroll( = {}) = { index: name, search_type: 'scan', scroll: '5m', size: 100 }.merge() scroll_id = server.search()['_scroll_id'] begin results = server.scroll(scroll_id: scroll_id, scroll: [:scroll]) documents, scroll_id = results['hits']['hits'], results['_scroll_id'] yield documents, results['hits']['total'] if documents.any? end while documents.size > 0 end |
#settings ⇒ Object
45 46 47 48 49 |
# File 'lib/cauchy/elastic/index.rb', line 45 def settings get('settings')[name]['settings']['index'] rescue {} end |
#settings=(settings) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/cauchy/elastic/index.rb', line 62 def settings=(settings) server.indices.put_settings index: name, body: settings rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e if e. =~ /update non dynamic settings/ raise CannotUpdateNonDynamicSettingsError.new(name) else raise end end |