stadtratmonitor/app/models/paper.rb

77 lines
2.1 KiB
Ruby
Raw Normal View History

require 'elasticsearch/model'
require 'json'
2015-04-13 22:09:28 +02:00
class Paper < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
2015-07-08 20:04:50 +02:00
validates_presence_of :body, :content, :name, :originator, :paper_type, :reference, :url
validates_presence_of :published_at, allow_nil: true
validates :url, uniqueness: true
2015-06-13 21:18:55 +02:00
settings index: { number_of_shards: 1 } do
mappings dynamic: false do
indexes :name, type: :string
indexes :content, type: :string
indexes :resolution, type: :string
indexes :paper_type, type: :string, index: :not_analyzed
end
end
2015-04-13 22:09:28 +02:00
class << self
def import_from_json(json_string)
old_count = count
JSON.parse(json_string).each do |record|
2015-04-13 22:09:28 +02:00
attributes = {
body: record['body'],
content: record['content'],
name: record['name'],
resolution: record['resolution'],
originator: record['originator'],
paper_type: record['paper_type'],
published_at: record['published_at'],
reference: record['reference'],
url: record['url'],
2015-04-13 22:09:28 +02:00
}
record = find_or_initialize_by(url: attributes[:url])
record.update_attributes(attributes)
2015-04-13 22:09:28 +02:00
end
puts "Imported #{count - old_count} Papers!"
2015-04-13 22:09:28 +02:00
end
# use DSL to define search queries
# see https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl
# and https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-rails/lib/rails/templates
def search(q)
@search_definition = Elasticsearch::DSL::Search.search do
2015-06-13 21:18:55 +02:00
query do
unless q.blank?
multi_match do
query q
fields ["name", "content"]
end
else
match_all
end
end
2015-06-13 21:18:55 +02:00
aggregation :paper_types do
terms do
field 'paper_type'
end
end
end
puts @search_definition.to_hash
__elasticsearch__.search(@search_definition)
end
2015-06-13 21:18:55 +02:00
def reset_index!
__elasticsearch__.create_index! force: true
all.each {|p| p.__elasticsearch__.index_document }
end
2015-04-13 22:09:28 +02:00
end
end