Skip to content

Commit

Permalink
Hit keys renamed to sortkeys.
Browse files Browse the repository at this point in the history
  • Loading branch information
coady committed Dec 2, 2019
1 parent 45319c0 commit a669024
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Optional server extras:
# Changes
dev
* PyLucene >=8 required
* `Hit.keys` renamed to `Hit.sortkeys`

2.3
* PyLucene >=7.7 required
Expand Down
13 changes: 8 additions & 5 deletions lupyne/engine/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,22 @@ def dict(self, *names, **defaults):


class Hit(Document):
"""A Document from a search result, with :attr:`id`, :attr:`score`, and optional sort :attr:`keys`."""
"""A Document from a search result, with :attr:`id`, :attr:`score`, and optional :attr:`sortkeys`.
def __init__(self, doc, id, score, keys=()):
.. versionchanged:: 2.4 keys renamed to :attr:`sortkeys`
"""

def __init__(self, doc, id, score, sortkeys=()):
Document.__init__(self, doc)
self.id, self.score = id, score
self.keys = tuple(map(convert, keys))
self.sortkeys = tuple(map(convert, sortkeys))

def dict(self, *names, **defaults):
"""Return dict representation of document with __id__, __score__, and any sort __keys__."""
result = Document.dict(self, *names, **defaults)
result.update(__id__=self.id, __score__=self.score)
if self.keys:
result['__keys__'] = self.keys
if self.sortkeys:
result['__sortkeys__'] = self.sortkeys
return result


Expand Down
3 changes: 2 additions & 1 deletion lupyne/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ def search(
"""Run query and return documents.
.. versionchanged:: 2.3 maxscore option and result removed
.. versionchanged:: 2.4 __keys__ renamed to __sortkeys__
**GET** /search?
Return array of document objects and total doc count.
Expand Down Expand Up @@ -435,7 +436,7 @@ def search(
| {
| "query": *string*\|null,
| "count": *int*\|null,
| "docs": [{"__id__": *int*, "__score__": *number*, "__keys__": *array*,
| "docs": [{"__id__": *int*, "__score__": *number*, "__sortkeys__": *array*,
"__highlights__": {*string*: *array*,... }, *string*: *value*,... },... ],
| "facets": {*string*: {*string*: *int*,... },... },
| "groups": [{"count": *int*, "value": *value*, "docs": [*object*,... ]},... ]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ def test_spatial(indexer, zipcodes):
x, y = (float(hit[l]) for l in ['longitude', 'latitude'])
query = field.within(x, y, 1e4)
hits = indexer.search(query, sort=field.distances(x, y))
distances = {hit.id: hit.keys[0] for hit in hits}
assert hits[0]['zipcode'] == zipcode and hits[0].keys < (1,)
distances = {hit.id: hit.sortkeys[0] for hit in hits}
assert hits[0]['zipcode'] == zipcode and hits[0].sortkeys < (1,)
cities = {hit['city'] for hit in hits}
assert city in cities and len(cities) == 12
groups = hits.groupby(lambda id: bisect.bisect_left([100, 5000], distances[id]))
Expand Down Expand Up @@ -480,7 +480,7 @@ def test_fields(indexer, constitution):
hits.select('amendment')
hit = hits[0].dict()
assert math.isnan(hit.pop('__score__'))
assert hit == {'amendment': '20', '__id__': 19, '__keys__': ('1923',)}
assert hit == {'amendment': '20', '__id__': 19, '__sortkeys__': ('1923',)}
query = Q.range('size', None, '1000')
assert indexer.count(query) == len(sizes) - len(ids)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ def test_search(resource):
assert docs[:5] == result['docs'] and result['count'] == len(docs)
result = resource.search(q='text:people', count=5, sort='-year:int')
assert all(math.isnan(doc['__score__']) for doc in result['docs'])
assert result['docs'][0]['__keys__'] == [1913] and result['docs'][-1]['__keys__'] == [1791]
assert result['docs'][0]['__sortkeys__'] == [1913] and result['docs'][-1]['__sortkeys__'] == [1791]
result = resource.search(q='text:people', sort='-year:int')
assert result['docs'][0]['__keys__'] == [1913] and result['docs'][-1]['__keys__'] == [0]
assert result['docs'][0]['__sortkeys__'] == [1913] and result['docs'][-1]['__sortkeys__'] == [0]
result = resource.search(q='text:people', count=5, sort='-year:int', **{'sort.scores': ''})
result = resource.search(q='text:people', count=1, sort='-year:int', **{'sort.scores': 'max'})
result = resource.search(q='text:people', count=5, sort='-date,year:int')
assert result['docs'][0]['__keys__'] == ['1913-04-08', 1913] and result['docs'][-1]['__keys__'] == ['1791-12-15', 1791]
assert result['docs'][0]['__sortkeys__'] == ['1913-04-08', 1913] and result['docs'][-1]['__sortkeys__'] == ['1791-12-15', 1791]
result = resource.search(q='text:people', start=2, count=2, facets='date')
assert result['count'] == 8 and result['facets']['date'] == {'1791-12-15': 5, '1913-04-08': 1}
result = resource.search(q='text:president', facets='date')
Expand Down

0 comments on commit a669024

Please sign in to comment.