fix: repair `order_by_field` with date fields (#262)

master
Caleb Hattingh 2024-05-03 23:49:32 +02:00 committed by GitHub
parent a14649f103
commit 52f7d6d08d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 1 deletions

View File

@ -182,7 +182,7 @@ impl Searcher {
if let Some(order_by) = order_by_field {
let collector = TopDocs::with_limit(limit)
.and_offset(offset)
.order_by_fast_field(order_by, order.into());
.order_by_u64_field(order_by, order.into());
let top_docs_handle =
multicollector.add_collector(collector);
let ret = self.inner.search(query.get(), &multicollector);

View File

@ -232,6 +232,57 @@ class TestClass(object):
result = searcher.search(query, 10, order_by_field="order")
assert len(result.hits) == 0
def test_order_by_search_date(self):
schema = (
SchemaBuilder()
.add_date_field("order", fast=True)
.add_text_field("title", stored=True)
.build()
)
index = Index(schema)
writer = index.writer()
doc = Document()
doc.add_date("order", datetime.datetime(2020, 1, 1))
doc.add_text("title", "Test title")
writer.add_document(doc)
doc = Document()
doc.add_date("order", datetime.datetime(2022, 1, 1))
doc.add_text("title", "Final test title")
writer.add_document(doc)
doc = Document()
doc.add_date("order", datetime.datetime(2021, 1, 1))
doc.add_text("title", "Another test title")
writer.add_document(doc)
writer.commit()
index.reload()
query = index.parse_query("test")
searcher = index.searcher()
result = searcher.search(query, 10, order_by_field="order")
assert len(result.hits) == 3
_, doc_address = result.hits[0]
searched_doc = index.searcher().doc(doc_address)
assert searched_doc["title"] == ["Final test title"]
_, doc_address = result.hits[1]
searched_doc = index.searcher().doc(doc_address)
assert searched_doc["title"] == ["Another test title"]
_, doc_address = result.hits[2]
searched_doc = index.searcher().doc(doc_address)
assert searched_doc["title"] == ["Test title"]
def test_with_merges(self):
# This test is taken from tantivy's test suite:
# https://github.com/quickwit-oss/tantivy/blob/42acd334f49d5ff7e4fe846b5c12198f24409b50/src/indexer/index_writer.rs#L1130