Skip to content

Instantly share code, notes, and snippets.

@gbit-is
Created December 10, 2019 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbit-is/d9eab152d4b2ddd54e6bc5d8db9d3ce5 to your computer and use it in GitHub Desktop.
Save gbit-is/d9eab152d4b2ddd54e6bc5d8db9d3ce5 to your computer and use it in GitHub Desktop.
Small python script to compare fields in elasticsearch index template vs elasticsearch indices
#! /usr/bin/python3
import json
# This is not a beutiful piece of code, however it works.
# If you have an elasticsearch index with defined fields in an index template but also dynamic mapping enabled
# you can run into not knowing what dynamic fields exist, which is not a good idea
# so the idea here is to be able to list fields existing in templates and indices and compare them
# I recomend using: "./compare.py | column -t -s ','"
# Since this is not something I am publishing as a tool, just a snipped I decided not to spend any time adding pretty-print functions to it
# Line 29-32 sets a few variables as True or False, those control what data is displayed
template = "/path/to/index/template.json" # GET _template/template_1
index = "/path/to/mapping/info/from/index.json" # GET /my-index/_mapping
fList = []
iList = []
pil = False # List all fields defined in the index
pfl = False # List all fields defined in the index template
umap = False # List all fields in the index template that are not defined in the index template
rep = True # get some basic numeric info
with open(template) as json_file:
template = json.load(json_file)
with open(index) as json_file:
index = json.load(json_file)
tData = template["mappings"]["properties"]
iData = index["mapping"]["properties"]
# Recursion for nested fields
def dig(data,name,liist):
if "type" in data:
type = data["type"]
msg = name + "," + type
liist.append(msg)
else:
if "properties" in data:
d = data["properties"]
else:
d = data
for x in d:
dd = d[x]
n = name + "." + x
dig(dd,n,liist)
# what builds the data
def makeList(fields,liist):
for cat in fields:
cdat = fields[cat]
if "type" in cdat:
type = cdat["type"]
msg = cat + "," + type
liist.append(msg)
else:
ddat = cdat["properties"]
for d in ddat:
cdat = ddat[d]
name = cat + "." + d
dig(cdat,name,liist)
# Make the data
makeList(tData,fList)
makeList(iData,iList)
# Depending on what is selected in the beginning, here the actual output is generated
if pil:
for entry in iList:
print(entry)
if pfl:
for entry in fList:
print(entry)
if umap:
for entry in iList:
if entry in fList:
pass
else:
print(entry)
if rep:
ilen = len(iList)
flen = len(fList)
diff = abs(ilen - flen)
m1 = "Number of fields defined in template:," + str(flen)
m2 = "Number of fields defined in index:," + str(ilen)
m3 = "Diffirence is:," + str(diff)
print()
print(m1)
print(m2)
print(m3)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment