-
-
Save serj-p/c4fcc9810b3b627de294 to your computer and use it in GitHub Desktop.
education aggregation
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
def _education_facet(company_id, text=None): | |
return { | |
"size": 0, | |
"aggs": { | |
"company_filtered": { | |
"filter": { | |
"and": [ | |
{ | |
{ | |
"type": { | |
"value": "facebook_profile" | |
} | |
} | |
}, | |
{ | |
"has_parent": { | |
"type": "contact", | |
"filter": { | |
"term": { | |
"company_id.na": str(company_id) | |
} | |
} | |
} | |
}, | |
{ | |
"query": { | |
"nested": { | |
"query": { | |
"match_phrase_prefix": { | |
"education.name": text | |
} | |
}, | |
"path": "facebook_profile.education" | |
} | |
} | |
} | |
] | |
}, | |
"aggs": { | |
"educations": { | |
"scripted_metric": { | |
"init_script": "_agg['education_to_parents'] = [:];", | |
"map_script": | |
"def pid = doc['_parent'].join('');" | |
"def map = _agg.education_to_parents;" | |
"def text = '%s';" % (text if text else '') + | |
"def edu_getter = { " | |
" e -> " | |
" def education = e['name'].toLowerCase(); " | |
" def include = false;" | |
" if (text) {" | |
" for (p in education.split(' ')*.trim()) {" | |
" if (p.startsWith(text)) {" | |
" include = true;" | |
" }" | |
" }" | |
" } else {" | |
" include = true;" | |
" };" | |
" if (include) {" | |
" if (map[education]) {" | |
" map[education].add(pid)" | |
" } " | |
" else {" | |
" map[education] = [pid] " | |
" } " | |
" }" | |
"};" | |
"if (_source.containsKey('education')) { " | |
" for(e in _source['education']) { " | |
" edu_getter(e) " | |
" } " | |
"};" | |
"if (_source.containsKey('organizations')) { " | |
" for(e in _source['organizations']) { " | |
" if (e['otype'] == 'school') { " | |
" edu_getter(e) " | |
" } " | |
" }" | |
"};", | |
"combine_script": | |
"def map = [:]; " | |
"def top = [:]; " | |
"_agg.education_to_parents.each {" | |
" e, parents -> map[e] = (parents as Set).size() " | |
"}; " | |
"map = map.sort {-it.value }; " | |
"map.eachWithIndex { val, idx -> if (idx<100) {top[val.key]=val.value }}; " | |
"return top", | |
"reduce_script": | |
"def top = [:]; " | |
"for (shard_top in _aggs) {" | |
" shard_top.each {e, count -> top[e] = count}" | |
"}; return top" | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment