Skip to content

Commit

Permalink
Add text score as first sort criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
leahfitch committed Jan 29, 2015
1 parent f45c2ba commit ad43a15
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions cellardoor/storage/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def get(self, entity, filter=None, fields=None, sort=None, offset=0, limit=0, ve
if filter and '_id' in filter and isinstance(filter['_id'], basestring):
filter['_id'] = self._objectid(filter['_id'])

if sort is None:
if filter and '$text' in filter:
sort = [('score', {'$meta':'textScore'})]
if not fields:
fields = {}
fields['score'] = {'$meta':'textScore'}
else:
sort = [(field[1:], 1) if field[0] == '+' else (field[1:], -1) for field in sort]
sort_pairs = []
if filter and '$text' in filter:
sort_pairs.append(('score', {'$meta':'textScore'}))
if not fields:
fields = {}
fields['score'] = {'$meta':'textScore'}
if sort:
sort_pairs.extend([(field[1:], 1) if field[0] == '+' else (field[1:], -1) for field in sort])

collection = self.get_collection(entity, shadow=versions)

Expand All @@ -63,9 +63,11 @@ def get(self, entity, filter=None, fields=None, sort=None, offset=0, limit=0, ve
else:
filter.update(type_filter)

print sort_pairs

results = collection.find(spec=filter,
fields=fields,
sort=sort,
sort=sort_pairs,
skip=offset,
limit=limit)

Expand Down

0 comments on commit ad43a15

Please sign in to comment.