Module: Barton::Data

Defined in:
lib/barton/core.rb

Class Method Summary collapse

Class Method Details

.configObject

Set elasticsearch config



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/barton/core.rb', line 137

def self.config
  case Barton.environment
  when 'test'
    @index_name = 'test_electorates'
  when ENV['FOUNDELASTICSEARCH_URL']
    Tire::Configuration.url ENV['FOUNDELASTICSEARCH_URL']
    @index_name = 'electorates'
  else
    @index_name = 'electorates'
  end
  #if ENV['BONSAI_INDEX_URL']
  #  Tire.configure do
  #    url "http://index.bonsai.io"
  #  end
  #  @index_name = ENV['BONSAI_INDEX_URL'][/[^\/]+$/]
  #end
  return Barton.environment
end

.purge_esObject

Purge electorate data from elasticsearch



157
158
159
# File 'lib/barton/core.rb', line 157

def self.purge_es
    self.query_es( 'purge' )
end

.query_es(action, data = nil) ⇒ Object

Query elasticsearch



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/barton/core.rb', line 167

def self.query_es( action, data=nil )
    self.config
    begin
      Tire.index "#{@index_name}" do
        delete if action == 'purge'
        create if action == 'purge'
        import data if action == 'update' and not data.nil?
      end
    rescue Exception => e
      puts "Elasticsearch error: #{e}"
    end
end

.search(terms, geo, type = nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/barton/core.rb', line 180

def self.search( terms, geo, type=nil )
  self.config
  s = Tire.search "#{@index_name}" do
	query do
	boolean do
		if geo
			long, lat = geo.split( ',' )
			must { range :north, { :gte => lat } }
			must { range :south, { :lte => lat } }
			must { range :east, { :gte => long } }
			must { range :west, { :lte => long } }
		elsif terms
			terms.each { |t| must { string t } }
		end
		must { string "type:#{type}" } if type
	end
end
#	apply filters only with geo search
filter :terms, :_all => terms if geo and terms
size 100
  end
  s
end

.update_es(data) ⇒ Object

Load electorate data to elasticsearch



162
163
164
# File 'lib/barton/core.rb', line 162

def self.update_es( data )
    self.query_es( 'update', data )
end