Skip to content

Commit 384311d

Browse files
authored
Merge pull request #74 from cjrh/fix-add-bytes
Include check for bytes in extract_value, fixes #72
2 parents 2b1439c + efe43d3 commit 384311d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/document.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ pub(crate) fn extract_value(any: &PyAny) -> PyResult<Value> {
194194
if let Ok(facet) = any.extract::<Facet>() {
195195
return Ok(Value::Facet(facet.inner));
196196
}
197+
if let Ok(b) = any.extract::<Vec<u8>>() {
198+
return Ok(Value::Bytes(b));
199+
}
197200
Err(to_pyerr(format!("Value unsupported {any:?}")))
198201
}
199202

tests/tantivy_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from io import BytesIO
12
import tantivy
23
import pytest
34

@@ -531,3 +532,27 @@ def test_query_from_json_field(self):
531532
# )
532533
# result = index.searcher().search(query, 2)
533534
# assert len(result.hits) == 1
535+
536+
537+
@pytest.mark.parametrize('bytes_kwarg', [True, False])
538+
@pytest.mark.parametrize('bytes_payload', [
539+
b"abc",
540+
bytearray(b"abc"),
541+
memoryview(b"abc"),
542+
BytesIO(b"abc").read(),
543+
BytesIO(b"abc").getbuffer(),
544+
])
545+
def test_bytes(bytes_kwarg, bytes_payload):
546+
schema = SchemaBuilder().add_bytes_field("embedding").build()
547+
index = Index(schema)
548+
writer = index.writer()
549+
550+
if bytes_kwarg:
551+
doc = Document(id=1, embedding=bytes_payload)
552+
else:
553+
doc = Document(id=1)
554+
doc.add_bytes("embedding", bytes_payload)
555+
556+
writer.add_document(doc)
557+
writer.commit()
558+
index.reload()

0 commit comments

Comments
 (0)