Created
August 2, 2017 14:36
-
-
Save derickson/5fd8bda0172382753d2f13baba8b4dd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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