Skip to content

Instantly share code, notes, and snippets.

@tatey
Last active January 28, 2016 06:19
Show Gist options
  • Save tatey/8f0138e2ff41882a3bde to your computer and use it in GitHub Desktop.
Save tatey/8f0138e2ff41882a3bde to your computer and use it in GitHub Desktop.
Identify conflicting field mappings in ElasticSearch 1.x > 2.0 upgrade
#!/usr/bin/env ruby
require 'json'
if ARGV.size < 1
puts "usage: #{__FILE__} <FILE1> [FILE2 ...]"
puts " eg: ./es_index_conflict_mapping applogs-2016.01.10.json applogs-2016.01.13.json applogs-2016.01.08.json applogs-2016.01.11.json applogs-2016.01.27.json"
abort
end
class PropertyMapping < Struct.new(:path, :property, :type)
end
def parse_node(node, path = [], property_mappings = [])
node.each do |key, value|
if value.is_a?(Hash)
properties = value['properties']
if properties
properties.each do |property, definition|
type = definition['type']
if type
property_mappings << PropertyMapping.new(path + [key], property, type)
elsif definition.has_key?('properties')
property_mappings << PropertyMapping.new(path + [key], property, 'object')
end
end
end
parse_node(value, path + [key], property_mappings)
end
end
property_mappings
end
document = {}
paths = ARGV.map { |path| File.expand_path(path.strip) }
paths.each do |path|
begin
document.merge!(JSON.parse(File.read(path)))
rescue JSON::ParserError
puts "malformed JSON in file."
abort
end
end
property_mappings = []
document.values.each do |index|
property_mappings += parse_node(index['mappings'])
end
conflicts = {}
property_mappings.each do |property_mapping|
conflicts[property_mapping.property] ||= {}
conflicts[property_mapping.property][property_mapping.type] ||= []
conflicts[property_mapping.property][property_mapping.type] << property_mapping.path.join('.')
conflicts[property_mapping.property][property_mapping.type].uniq!
end
conflicts.each do |property, types|
if types.size > 1
puts property
types.each do |type, paths|
puts " #{type}"
puts " #{paths.join(', ')}"
end
puts "---"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment