Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A quick and very dirty script to list the effective lucene field list from an elasticsearch index mapping
#!/usr/bin/env ruby
# A quick and very dirty script to list the effective lucene schema from an elasticsearch index mapping
#
# USAGE:
# cat index_mappings.json | lucene-list-fields-from-elasticsearch-index-mappings.rb
# cat cluster_mappings.json | jq '.["index_name"].mappings' | lucene-list-fields-from-elasticsearch-index-mappings.rb
#
#
# Copyright 2018 Ry Biesemeyer (@yaauie)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
require 'json'
require 'set'
class IndexValidator
attr_reader :lucene
def initialize(index_mapping)
@index_mapping = index_mapping
@lucene = Set.new
end
def validate
@index_mapping.each do |type_name, type_mapping|
next unless type_mapping.include?('properties')
crawl(type_mapping)
end
end
def crawl(blob, path=[])
path_str = path.join('.')
if(blob.include? 'type')
@lucene.add(path_str)
elsif(blob.include? 'properties')
blob['properties'].each do |key, value|
crawl(value, path + [key])
end
else
fail "ERROR(#{path_str}): node contained neither `properties` nor `type` (path: #{path_str} -> #{blob.to_json})"
end
end
def field_count
@lucene.size
end
end
iv = IndexValidator.new(JSON.load($stdin))
iv.validate
$stderr.puts("FIELD COUNT: #{iv.field_count}") if ENV['SHOW_COUNT']
$stdout.puts(iv.lucene.to_a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.