Skip to content

Instantly share code, notes, and snippets.

@brianmcgue
Created August 18, 2020 22:21
Show Gist options
  • Save brianmcgue/def0d020c1a7060ec6291e13d62ae221 to your computer and use it in GitHub Desktop.
Save brianmcgue/def0d020c1a7060ec6291e13d62ae221 to your computer and use it in GitHub Desktop.
This is
require 'httpclient'
require 'json'
require 'uri'
# --------------------------------------
# Edit these variables
HOST = 'http://localhost:9200'
ELASTICSEARCH_USERNAME = 'elastic'
ELASTICSEARCH_PASSWORD = 'changeme'
DRY_RUN = false # don't actually edit anything yet
# --------------------------------------
INDEX_POINTER_INDEX_NAME = '.ent-search-actastic-index_pointers_v2'
client = HTTPClient.new
client.set_auth(nil, ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
host = URI.parse(HOST)
index_pointer_search_uri = URI.join(host, "#{INDEX_POINTER_INDEX_NAME}/_search")
search_response = client.get(index_pointer_search_uri, nil, 'Content-Type' => 'application/json')
recreated_count = 0
index_pointers = JSON.parse(search_response.body).dig('hits', 'hits').select do |result|
result.fetch('_id').include?('.app-search')
end
puts "There are #{index_pointers.size} records that need to be fixed."
puts
index_pointers.each do |index_pointer|
index_pointer_id = index_pointer.fetch('_id')
new_id = index_pointer_id.gsub('app-search', 'ent-search')
source = index_pointer.fetch('_source')
new_index_pointer_doc = source.merge(
'name' => source.fetch('name').gsub('app-search', 'ent-search'),
'read_weight' => 1.0,
'write_enabled' => true
)
index_pointer_create_uri = URI.join(host, "#{INDEX_POINTER_INDEX_NAME}/_doc/#{URI.encode(new_id)}")
puts "Creating a new record for index pointer id: #{index_pointer_id}"
unless DRY_RUN
create_response = client.put(
index_pointer_create_uri,
JSON.dump(new_index_pointer_doc),
'Content-Type' => 'application/json'
)
raise StandardError.new(JSON.parse(create_response.body)) if create_response.status >= 400
end
index_pointer_delete_uri = URI.join(host, "#{INDEX_POINTER_INDEX_NAME}/_doc/#{URI.encode(index_pointer_id)}")
puts "\tDeleting the old record for index pointer id: #{index_pointer_id}"
unless DRY_RUN
delete_response = client.delete(index_pointer_delete_uri)
raise StandardError.new(JSON.parse(delete_response.body)) if delete_response.status >= 400
end
recreated_count += 1
end
puts
if DRY_RUN
puts "Would have recreated #{index_pointers.size} index pointers."
else
puts "Successfully recreated #{index_pointers.size} index pointers."
end
index_pointer_refresh_uri = URI.join(host, "#{INDEX_POINTER_INDEX_NAME}/_refresh")
puts "Refreshing the index."
refresh_response = client.post(
index_pointer_refresh_uri,
'',
'Content-Type' => 'application/json'
)
raise StandardError.new(JSON.parse(refresh_response.body)) if refresh_response.status >= 400
puts "The underlying Elasticsearch index has been refreshed and documents can now be indexed into your Enterprise Search instance."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment