Skip to content

Commit

Permalink
Add max score to queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgross committed Aug 21, 2014
1 parent abb4e79 commit 7a60ee6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pyeqs/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, host, query=None, index=''):
self._wrappers = []
self._post_query_actions = []
self._count = None
self._max_score = None
self._conn = None
self._finalized_query = None
self._aggregations = None
Expand Down Expand Up @@ -70,6 +71,9 @@ def post_query_actions(self, action):
def count(self):
return self._count

def max_score(self):
return self._max_score

def aggregations(self):
return self._aggregations

Expand Down Expand Up @@ -138,11 +142,15 @@ def _search(self, start, end):
pagination_kwargs = self._get_pagination_kwargs(start, end)
raw_results = conn.search(index=self._index, body=self._query, **pagination_kwargs)
self._count = self._get_result_count(raw_results)
self._max_score = self._get_max_score(raw_results)
return raw_results

def _get_result_count(self, results):
return int(results["hits"]["total"])

def _get_max_score(self, results):
return results.get("hits", {}).get("max_score")

def _get_pagination_kwargs(self, start, end):
size = end - start
kwargs = {
Expand Down
1 change: 1 addition & 0 deletions tests/functional/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def test_search_with_iterator(context):

len(t).should.equal(5)
t.count().should.equal(5)
t.max_score().should_not.be(None)


@scenario(prepare_data, cleanup_data)
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,17 @@ def test_queryset_count():
t.count().should.equal(None)


def test_queryset_max_score():
"""
Get QuerySet Max Score
"""
# When I create a query block
t = QuerySet("foobar")

# Then I get an appropriate max score
t.max_score().should.equal(None)


def test_queryset_string():
"""
Create QuerySet with String query
Expand Down Expand Up @@ -641,6 +652,7 @@ def test_queryset_getitem_with_wrapper():
results = t[0:1]
len(results).should.equal(1)
t.count().should.equal(1)
t.max_score().should.equal(10)
int(results[0]).should.equal(1)


Expand Down

0 comments on commit 7a60ee6

Please sign in to comment.