Skip to content

Commit a829abe

Browse files
committed
Luke: first stage of facets implmented
1 parent bc81b11 commit a829abe

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

elasticpy.py

+70
Original file line numberDiff line numberDiff line change
@@ -853,3 +853,73 @@ def term(self, field, value):
853853

854854

855855

856+
class ElasticFacet(dict):
857+
'''
858+
Facets for search
859+
http://www.elasticsearch.org/guide/reference/api/search/facets/
860+
861+
The usual purpose of a full-text search engine is to return a small number of documents matching your query.
862+
863+
Facets provide aggregated data based on a search query. In the simplest case, a terms facet can return facet counts for various facet values for a specific field. ElasticSearch supports more facet implementations, such as statistical or date histogram facets.
864+
865+
The field used for facet calculations must be of type numeric, date/time or be analyzed as a single token - see the Mapping guide for details on the analysis process.
866+
You can give the facet a custom name and return multiple facets in one request.
867+
'''
868+
869+
def terms(self, facet_name, field, size=10, order=None, all_terms=False, exclude=[], regex='', regex_flags=''):
870+
'''
871+
Allow to specify field facets that return the N most frequent terms.
872+
873+
Ordering: Allow to control the ordering of the terms facets, to be ordered by count, term, reverse_count or reverse_term. The default is count.
874+
All Terms: Allow to get all the terms in the terms facet, ones that do not match a hit, will have a count of 0. Note, this should not be used with fields that have many terms.
875+
Excluding Terms: It is possible to specify a set of terms that should be excluded from the terms facet request result.
876+
Regex Patterns: The terms API allows to define regex expression that will control which terms will be included in the faceted list.
877+
'''
878+
879+
self[facet_name] = dict(terms=dict(field=field,size=size))
880+
if order:
881+
self[facet_name][terms]['order'] = order
882+
if all_terms:
883+
self[facet_name][terms]['all_terms'] = True
884+
if exclude:
885+
self[facet_name][terms]['exclude'] = exclude
886+
if regex:
887+
self[facet_name][terms]['regex'] = regex
888+
if regex_flags:
889+
self[facet_name][terms]['regex_flags'] = regex_flags
890+
891+
return self
892+
893+
def range(self,facet_name, field, ranges=[]):
894+
'''
895+
Range facet allow to specify a set of ranges and get both the number of docs (count) that fall within each range, and aggregated data either based on the field, or using another field.
896+
http://www.elasticsearch.org/guide/reference/api/search/facets/range-facet.html
897+
898+
> ElasticFacet().range('range1', 'field_name', [ slice(50), slice(20,70), slice(50,-1) ])
899+
{
900+
"range1" : {
901+
"range" : {
902+
"field" : "field_name",
903+
"ranges" : [
904+
{ "to" : 50 },
905+
{ "from" : 20, "to" : 70 },
906+
{ "from" : 70, "to" : 120 },
907+
{ "from" : 150 }
908+
]
909+
}
910+
}
911+
}
912+
'''
913+
914+
self[facet_name] = {'range' : { 'field' : field,'ranges' : [] }}
915+
for s in ranges:
916+
if not isinstance(s, slice):
917+
continue
918+
entry = dict()
919+
if s.start:
920+
entry['from'] = s.start
921+
if s.stop != -1:
922+
entry['to'] = s.stop
923+
self[facet_name]['range']['ranges'].append(entry)
924+
925+
return self

0 commit comments

Comments
 (0)