2015-04-27 21:26:37 +02:00
|
|
|
require 'elasticsearch/model'
|
2015-04-27 21:03:58 +02:00
|
|
|
require 'json'
|
2015-04-13 22:09:28 +02:00
|
|
|
|
|
|
|
class Paper < ActiveRecord::Base
|
2015-04-27 21:26:37 +02:00
|
|
|
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
|
2015-05-17 17:49:09 +02:00
|
|
|
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
|
2015-06-13 21:27:22 +02:00
|
|
|
indexes :originator, type: :string, index: :not_analyzed
|
2015-06-13 21:18:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-13 22:09:28 +02:00
|
|
|
class << self
|
2015-04-27 21:03:58 +02:00
|
|
|
def import_from_json(json_string)
|
2015-05-17 17:49:09 +02:00
|
|
|
old_count = count
|
2015-04-27 21:03:58 +02:00
|
|
|
JSON.parse(json_string).each do |record|
|
2015-04-13 22:09:28 +02:00
|
|
|
attributes = {
|
2015-05-17 17:49:09 +02:00
|
|
|
body: record['body'],
|
|
|
|
content: record['content'],
|
2015-04-27 21:03:58 +02:00
|
|
|
name: record['name'],
|
2015-05-17 17:49:09 +02:00
|
|
|
resolution: record['resolution'],
|
2015-04-27 21:03:58 +02:00
|
|
|
originator: record['originator'],
|
2015-05-17 17:49:09 +02:00
|
|
|
paper_type: record['paper_type'],
|
2015-04-27 21:03:58 +02:00
|
|
|
published_at: record['published_at'],
|
2015-05-17 17:49:09 +02:00
|
|
|
reference: record['reference'],
|
|
|
|
url: record['url'],
|
2015-04-13 22:09:28 +02:00
|
|
|
}
|
2015-04-28 00:47:26 +02:00
|
|
|
record = find_or_initialize_by(url: attributes[:url])
|
2015-05-17 17:49:09 +02:00
|
|
|
record.update_attributes(attributes)
|
2015-04-13 22:09:28 +02:00
|
|
|
end
|
2015-05-17 17:49:09 +02:00
|
|
|
puts "Imported #{count - old_count} Papers!"
|
2015-04-13 22:09:28 +02:00
|
|
|
end
|
2015-06-01 23:53:45 +02:00
|
|
|
|
|
|
|
# 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
|
2015-06-22 20:38:24 +02:00
|
|
|
def search(q, options={})
|
2015-06-01 23:53:45 +02:00
|
|
|
@search_definition = Elasticsearch::DSL::Search.search do
|
2015-06-13 21:18:55 +02:00
|
|
|
|
2015-06-01 23:53:45 +02:00
|
|
|
query do
|
2015-06-22 20:38:24 +02:00
|
|
|
filtered do
|
|
|
|
|
|
|
|
query do
|
|
|
|
# search query
|
|
|
|
unless q.blank?
|
|
|
|
multi_match do
|
|
|
|
query q
|
|
|
|
fields ["name", "content"]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
match_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# filters
|
|
|
|
filter do
|
|
|
|
bool do
|
|
|
|
must { term paper_type: options[:paper_type] } if options[:paper_type]
|
2015-06-22 20:43:06 +02:00
|
|
|
must { term originator: options[:originator] } if options[:originator]
|
|
|
|
# catchall when no filters set
|
|
|
|
must { match_all } if options.keys.none? {|k| [:paper_type, :originator].include?(k) }
|
2015-06-22 20:38:24 +02:00
|
|
|
end
|
2015-06-01 23:53:45 +02:00
|
|
|
end
|
2015-06-22 20:38:24 +02:00
|
|
|
|
2015-06-01 23:53:45 +02:00
|
|
|
end
|
|
|
|
end
|
2015-06-13 21:18:55 +02:00
|
|
|
|
|
|
|
aggregation :paper_types do
|
|
|
|
terms do
|
|
|
|
field 'paper_type'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-13 21:27:22 +02:00
|
|
|
aggregation :originators do
|
|
|
|
terms do
|
|
|
|
field 'originator'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-01 23:53:45 +02:00
|
|
|
end
|
2015-06-22 20:38:24 +02:00
|
|
|
Rails.logger.debug "Query: #{@search_definition.to_json}"
|
2015-06-01 23:53:45 +02:00
|
|
|
__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
|