Skip to content

Instantly share code, notes, and snippets.

@derickson
Created August 2, 2017 14:36
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 derickson/5fd8bda0172382753d2f13baba8b4dd8 to your computer and use it in GitHub Desktop.
Save derickson/5fd8bda0172382753d2f13baba8b4dd8 to your computer and use it in GitHub Desktop.
#!/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