2015-04-27 21:26:37 +02:00
|
|
|
require 'elasticsearch/model'
|
2015-04-27 21:03:58 +02:00
|
|
|
require 'json'
|
2015-09-27 13:51:13 +02:00
|
|
|
require 'parseable_date_validator'
|
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-09-27 13:51:13 +02:00
|
|
|
validates :name, presence: true, length: { maximum: 1000 }
|
|
|
|
validates :url, presence: true,
|
|
|
|
length: { maximum: 1000 },
|
|
|
|
uniqueness: true, # TODO use unique index instead
|
|
|
|
url: true
|
|
|
|
validates :reference, presence: true, length: { maximum: 100 }
|
|
|
|
validates :body, presence: true, length: { maximum: 100 }
|
|
|
|
validates :content, presence: true, length: { maximum: 100_000 }
|
|
|
|
validates :originator, presence: true, length: { maximum: 300 }
|
|
|
|
validates :paper_type, presence: true, length: { maximum: 50 }
|
|
|
|
validates :published_at, presence: true, parseable_date: true
|
|
|
|
validates :resolution, length: { maximum: 30_000 }
|
2015-05-17 17:49:09 +02:00
|
|
|
|
2016-02-17 20:45:29 +01:00
|
|
|
index_name ['srm', Rails.env, self.base_class.to_s.pluralize.underscore].join('_')
|
|
|
|
|
2016-12-29 18:22:10 +01:00
|
|
|
settings index: {
|
|
|
|
number_of_shards: 1,
|
|
|
|
analysis: {
|
|
|
|
filter: {
|
|
|
|
german_stop: {
|
|
|
|
type: "stop",
|
|
|
|
stopwords: "_german_"
|
|
|
|
},
|
|
|
|
german_stemmer: {
|
|
|
|
type: "stemmer",
|
|
|
|
language: "light_german"
|
|
|
|
},
|
|
|
|
decomp: {
|
|
|
|
type: "decompound"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
analyzer: {
|
|
|
|
german: {
|
|
|
|
tokenizer: "standard",
|
|
|
|
filter: [
|
|
|
|
"lowercase",
|
|
|
|
"german_stop",
|
|
|
|
"german_normalization",
|
|
|
|
"german_stemmer",
|
|
|
|
"decomp"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} do mappings dynamic: false do
|
2015-06-23 00:00:41 +02:00
|
|
|
indexes :name, type: :string, analyzer: "german"
|
|
|
|
indexes :content, type: :string, analyzer: "german"
|
|
|
|
indexes :resolution, type: :string, analyzer: "german"
|
2016-12-29 18:22:10 +01:00
|
|
|
indexes :reference, type: :string, index: :not_analyzed
|
2015-06-13 21:18:55 +02:00
|
|
|
indexes :paper_type, type: :string, index: :not_analyzed
|
2016-03-26 13:33:27 +01:00
|
|
|
indexes :published_at, type: :date, 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
|
2015-09-27 10:25:40 +02:00
|
|
|
end
|
2015-06-13 21:18:55 +02:00
|
|
|
|
2015-06-22 23:51:36 +02:00
|
|
|
def split_originator
|
|
|
|
originator.split(/\d\.\s/).reject {|s| s.blank?} || originator
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_indexed_json(options={})
|
|
|
|
as_json.merge(originator: split_originator)
|
|
|
|
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-10-05 22:13:53 +02:00
|
|
|
def search(search_definition)
|
|
|
|
Rails.logger.debug "Query: #{search_definition.to_json}"
|
|
|
|
__elasticsearch__.search(search_definition)
|
2015-06-01 23:53:45 +02:00
|
|
|
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
|