Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: repair order_by_field with date fields #262

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading