Module: Elastics::UtilityMethods
- Included in:
- Elastics
- Defined in:
- lib/elastics/utility_methods.rb
Instance Method Summary collapse
- #build_bulk_string(document, options = {}) ⇒ Object
- #doc(*args) ⇒ Object
- #dump_all(*vars, &block) ⇒ Object
-
#dump_one(*vars) ⇒ Object
refresh and pull the full document from the index.
- #find(pattern) ⇒ Object
- #json2yaml(json) ⇒ Object
-
#post_bulk_collection(collection, options = {}) ⇒ Object
You should use Elastics.post_bulk_string if you have an already formatted bulk data-string.
- #reload! ⇒ Object
- #scan_all(*vars, &block) ⇒ Object
- #scan_search(*args, &block) ⇒ Object
- #search(data, vars = {}) ⇒ Object
-
#slim_search(data, vars = {}) ⇒ Object
like Elastics.search, but it will use the Elastics::Template::SlimSearch instead.
- #usage(*args) ⇒ Object
- #yaml2json(yaml) ⇒ Object
Instance Method Details
#build_bulk_string(document, options = {}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/elastics/utility_methods.rb', line 85 def build_bulk_string(document, ={}) case document when Hash bulk_string_from_hash(document, ) when Elastics::ModelIndexer, Elastics::ActiveModel bulk_string_from_elastics(document, ) else raise NotImplementedError, "Unable to convert the document #{document.inspect} to a bulk string." end end |
#doc(*args) ⇒ Object
35 36 37 |
# File 'lib/elastics/utility_methods.rb', line 35 def doc(*args) elastics.doc(*args) end |
#dump_all(*vars, &block) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/elastics/utility_methods.rb', line 58 def dump_all(*vars, &block) refresh_index(*vars) scan_all({:params => {:_source => '*'}}, *vars) do |batch| batch.map!{|document| document.delete('_score'); document} block.call(batch) end end |
#dump_one(*vars) ⇒ Object
refresh and pull the full document from the index
67 68 69 70 71 72 |
# File 'lib/elastics/utility_methods.rb', line 67 def dump_one(*vars) refresh_index(*vars) document = search_by_id({:params => {:_source => '*'}}, *vars) document.delete('_score') document end |
#find(pattern) ⇒ Object
39 40 41 |
# File 'lib/elastics/utility_methods.rb', line 39 def find(pattern) elastics.find(pattern) end |
#json2yaml(json) ⇒ Object
21 22 23 |
# File 'lib/elastics/utility_methods.rb', line 21 def json2yaml(json) YAML.dump(MultiJson.decode(json)) end |
#post_bulk_collection(collection, options = {}) ⇒ Object
You should use Elastics.post_bulk_string if you have an already formatted bulk data-string
75 76 77 78 79 80 81 82 83 |
# File 'lib/elastics/utility_methods.rb', line 75 def post_bulk_collection(collection, ={}) raise ArgumentError, "Array expected as :collection, got #{collection.inspect}" \ unless collection.is_a?(Array) bulk_string = '' collection.each do |d| bulk_string << build_bulk_string(d, ) end post_bulk_string(:bulk_string => bulk_string) unless bulk_string.empty? end |
#reload! ⇒ Object
29 30 31 32 33 |
# File 'lib/elastics/utility_methods.rb', line 29 def reload! elastics.variables.deep_merge! Conf.variables Templates.contexts.each {|c| c.elastics.reload!} true end |
#scan_all(*vars, &block) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/elastics/utility_methods.rb', line 51 def scan_all(*vars, &block) elastics.scan_search(:match_all, *vars) do |raw_result| batch = raw_result['hits']['hits'] block.call(batch) end end |
#scan_search(*args, &block) ⇒ Object
47 48 49 |
# File 'lib/elastics/utility_methods.rb', line 47 def scan_search(*args, &block) elastics.scan_search(*args, &block) end |
#search(data, vars = {}) ⇒ Object
4 5 6 |
# File 'lib/elastics/utility_methods.rb', line 4 def search(data, vars={}) Template::Search.new(data, vars).setup(Elastics.elastics).render(vars) end |
#slim_search(data, vars = {}) ⇒ Object
like Elastics.search, but it will use the Elastics::Template::SlimSearch instead
9 10 11 |
# File 'lib/elastics/utility_methods.rb', line 9 def slim_search(data, vars={}) Template::SlimSearch.new(data, vars).setup(Elastics.elastics).render(vars) end |
#usage(*args) ⇒ Object
43 44 45 |
# File 'lib/elastics/utility_methods.rb', line 43 def usage(*args) elastics.usage(*args) end |
#yaml2json(yaml) ⇒ Object
25 26 27 |
# File 'lib/elastics/utility_methods.rb', line 25 def yaml2json(yaml) MultiJson.encode(YAML.load(yaml)) end |