Skip to content

Commit

Permalink
fix: repair order_by_field with date fields (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrh authored May 3, 2024
1 parent a14649f commit 52f7d6d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/searcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions tests/tantivy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,57 @@ def test_order_by_search_without_fast_field(self):
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
Expand Down

0 comments on commit 52f7d6d

Please sign in to comment.