Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
def prettyPrint(doc):
print(json.dumps(doc, indent=4, sort_keys=True))
def combinatorialSet( tags ):
results = []
def termPick( arrayOfTags, arrayLen, startingAt, depthLeft, stuffSoFar ):
if(startingAt + depthLeft <= arrayLen-1):
for x in range(startingAt, arrayLen):
pick = arrayOfTags[x]
accumulatedPick = stuffSoFar + [ pick ]
if(depthLeft > 0 and startingAt < arrayLen - 1):
termPick(arrayOfTags, arrayLen, max(x+1,startingAt + 1), depthLeft -1, accumulatedPick)
else:
results.append( accumulatedPick )
for x in range(0,len(tags)):
termPick( tags, len(tags), 0, x, [])
return results
tags = [ "Beer", "DomesticBeer"]
field = "securityTags"
count_field = "securityTag_Count"
combinations = combinatorialSet(tags)
shoulds = []
for combo in combinations:
comboLen = len(combo)
mustTerms = []
for term in combo:
mustTerms.append( {"term": {field: term}} )
mustTerms.append( {"term": {count_field: comboLen}} )
shoulds.append({"bool":{"must":[mustTerms]}})
query = {"bool": {"should": [shoulds]}}
prettyPrint(query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment