Skip to content

Instantly share code, notes, and snippets.

@weimzh
Last active May 14, 2020 07:59
Show Gist options
  • Save weimzh/c3b1ed2dbbf8401b1e65134b537eba05 to your computer and use it in GitHub Desktop.
Save weimzh/c3b1ed2dbbf8401b1e65134b537eba05 to your computer and use it in GitHub Desktop.
simple code to convert json to elasticsearch painless for update_by_query.
# Copyright (c) 2020, Wei Mingzhi <whistler_wmz at users.sf.net>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY WEI MINGZHI ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL WEI MINGZHI BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import json
import random
import string
def convert_object(ob, prefix):
painless = ''
for item in ob:
painless += convert_item(item, ob[item], prefix);
return painless
def convert_array_elem(array_prefix, elem):
typ = type(elem).__name__
if typ == 'int':
# integer
return array_prefix + 'add(' + str(elem) + 'L);'
elif typ == 'float':
# float
return array_prefix + 'add(' + str(elem) + ');'
elif typ == 'str' or typ == 'unicode':
# string
return array_prefix + "add('" + str(elem).replace("'", "\\'") + "');"
elif typ == 'dict':
# object
painless = array_prefix + 'add(new HashMap());'
painless += convert_object(elem, array_prefix[:-1] + '[' + array_prefix[:-1] + '.length-1].')
return painless
elif typ == 'list':
painless = array_prefix + 'add(new ArrayList());'
painless += convert_array(elem, array_prefix[:-1] + '[' + array_prefix[:-1] + '.length-1].')
return painless
def convert_array(arr, prefix):
painless = ''
for item in arr:
painless += convert_array_elem(prefix, item);
return painless
def convert_item(name, item, prefix):
typ = type(item).__name__
if typ == 'int':
# integer
return prefix + str(name) + '=' + str(item) + 'L;';
elif typ == 'float':
# integer
return prefix + str(name) + '=' + str(item) + ';';
elif typ == 'str' or typ == 'unicode':
# string
return prefix + str(name) + "='" + str(item).replace("'", "\\'") + "';";
elif typ == 'dict':
# object
painless = ''
painless = 'if(!' + prefix[:-1] + ".containsKey('" + name + "'))"
painless += prefix + name + '=new HashMap();'
painless += convert_object(item, prefix + name + '.')
return painless
elif typ == 'list':
# array
painless = 'if(!' + prefix[:-1] + ".containsKey('" + name + "'))"
painless += prefix + name + '=new ArrayList();'
painless += convert_array(item, prefix + name + '.')
return painless
def convert_json(json_text):
painless = ''
ob = json.loads(json_text)
for item in ob:
painless += convert_item(item, ob[item], 'ctx._source.')
return painless
print convert_json('{"tags": {"weather": [{"start_time": 1571970937999,"name": "sunny","end_time": 1571970939999},{"start_time": 1571970939999,"name": "cloud","end_time": 1571970942999},{"start_time": 1571970942999,"name": "rain","end_time": 1571970944999},{"start_time": 1571970942999,"name": "sunny","end_time": 1571970947999}]}}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment