Skip to content

Instantly share code, notes, and snippets.

@salami-art
Last active February 1, 2021 17:17
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 salami-art/7224b00fcf9532a7eceec5c535406291 to your computer and use it in GitHub Desktop.
Save salami-art/7224b00fcf9532a7eceec5c535406291 to your computer and use it in GitHub Desktop.
def combine_query(query, piece)
# this function always receives its previous return value
_query = {}.merge(query) # also tried Hash.new(), query.dup, query.clone
_query['query'] ||= {}
_query['query']['bool'] ||= {}
_query['query']['bool']['must'] ||= []
_query['query']['bool']['must'] << piece
_query
end
def aggregate(by: {}, query: {}, &block)
results = search_index(body: query.dup.merge({aggs: get_aggregations(by)}))
results.each do |result|
display, value = result['key'].split('|')
yield value, display
end
end
def main_job_perform_function
# [ .... within a loop ....]
q = get_desided_query.freeze
# q is the object that is being unexpectedly mutated
aggregate by: {field: 'my_field'}, query: q do |k, v|
new_query = combine_query(q, get_other_condition('my_field', k))
update_index(query: new_query['query'], script: populate_array_field({ myfield: myvalue }))
new_query = 'ASDASAd'
log_debug(new_query)
end
# [ .... ]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment