Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add elasticsearch indexing support #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions exe/es_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env ruby

require "perseus"
require "elasticsearch"

host = ENV['ES_HOST'] || "localhost"
port = ENV['ES_PORT'] || 9200
ES_CONFIG = [{
host: host,
port: port
}].freeze
@es_client = Elasticsearch::Client.new hosts: ES_CONFIG
@_index = "perseus-cts-by-edition"
@_type = "edition"

DESTRUCTIVE = true
def extract_field field, string
match = "Perseus:bib:#{field},"
f = string[/#{match}\d+/]
unless f.nil?
f.gsub(match, "")
end
end

file = Perseus::ALL_EDITIONS_JSON
json = JSON.parse(File.read(file))
json.each do |doc|
_id = doc["urn"]
doc["collection"] = doc["memberof"]["collection"].gsub("Perseus:collection:", "")
doc["project"] = doc["projid"]
doc["group"] = doc["groupname"]
# extract some info from the description like oclc and isbn codes if they exist
description = doc["description"]
isbn = extract_field("isbn", description)
oclc = extract_field("oclc", description)
doc["isbn"] = isbn unless isbn.nil?
doc["oclc"] = oclc unless oclc.nil?
doc["edition"] = description.gsub(/Perseus:bib:isbn,\d+, /, "").gsub(/Perseus:bib:oclc,\d+, /, "")
if DESTRUCTIVE
doc.delete("online")
doc.delete("urn")
doc.delete("memberof")
doc.delete("projid")
doc.delete("groupname")
doc.delete("xml:lang")
doc.delete("description")
end
begin
@es_client.get(index: @_index, type:@_type, id:_id)
puts "Updating existing document: #{doc.inspect}"
@es_client.update(index: @_index, type:@_type, id:_id, body: { doc: doc })
rescue Elasticsearch::Transport::Transport::Errors::NotFound => not_found
#puts not_found.message
puts "Creating new document: #{doc.inspect}"
@es_client.create(index: @_index, type:@_type, id:_id, body: doc)
puts "Document indexed"
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "perseus"
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
Expand Down