Skip to content

Commit

Permalink
Add scope support in nested queries and date histogram facet. from ma…
Browse files Browse the repository at this point in the history
…rkbirbeck branch
  • Loading branch information
Alberto Paro committed Feb 15, 2012
1 parent ac77c08 commit 4ea2bcb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 6 additions & 2 deletions pyes/facets.py
Expand Up @@ -117,7 +117,7 @@ def __init__(self, name,
field=None, interval=None, zone=None,
key_field=None, value_field=None,
value_script=None, params=None,
**kwargs):
scope=None, **kwargs):
super(DateHistogramFacet, self).__init__(**kwargs)
self.name = name
self.field = field
Expand All @@ -127,6 +127,7 @@ def __init__(self, name,
self.value_field = value_field
self.value_script = value_script
self.params = params
self.scope = scope

def serialize(self):
data = {}
Expand All @@ -150,7 +151,10 @@ def serialize(self):
else:
raise RuntimeError("Invalid key_field: value_field or value_script required")

return {self.name:{self._internal_name:data}}
facet = {self._internal_name:data}
if self.scope is not None:
facet['scope'] = self.scope
return {self.name:facet}

class RangeFacet(Facet):
_internal_name = "range"
Expand Down
11 changes: 7 additions & 4 deletions pyes/query.py
Expand Up @@ -479,11 +479,12 @@ class NestedQuery(Query):
"""
_internal_name = "nested"

def __init__(self, path, query, score_mode="avg", **kwargs):
def __init__(self, path, query, _scope=None, score_mode="avg", **kwargs):
super(NestedQuery, self).__init__(**kwargs)
self.path = path
self.score_mode = score_mode
self.query = query
self._scope = _scope

def serialize(self):

Expand All @@ -493,6 +494,8 @@ def serialize(self):
'path':self.path,
'score_mode':self.score_mode,
'query':self.query.serialize()}
if self._scope is not None:
data['_scope'] = self._scope
return {self._internal_name:data}

class DisMaxQuery(Query):
Expand Down Expand Up @@ -1278,7 +1281,7 @@ def to_search_json(self):

class CustomFiltersScoreQuery(Query):
_internal_name = "custom_filters_score"

class ScoreMode(object):
FIRST = "first"
MIN = "min"
Expand All @@ -1291,7 +1294,7 @@ class Filter(EqualityComparableUsingAttributeDictionary):
def __init__(self, filter_, boost=None, script=None):
if (boost is None) == (script is None):
raise ValueError("Exactly one of boost and script must be specified")

self.filter_ = filter_
self.boost = boost
self.script = script
Expand Down Expand Up @@ -1321,4 +1324,4 @@ def serialize(self):
data['params'] = self.params
if self.lang is not None:
data['lang'] = self.lang
return {self._internal_name: data}
return {self._internal_name: data}

0 comments on commit 4ea2bcb

Please sign in to comment.