Merge pull request #74 from cjrh/fix-add-bytes

Include check for bytes in extract_value, fixes #72
master
Caleb Hattingh 2023-03-26 15:30:19 +02:00 committed by GitHub
commit 384311dec0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -194,6 +194,9 @@ pub(crate) fn extract_value(any: &PyAny) -> PyResult<Value> {
if let Ok(facet) = any.extract::<Facet>() { if let Ok(facet) = any.extract::<Facet>() {
return Ok(Value::Facet(facet.inner)); return Ok(Value::Facet(facet.inner));
} }
if let Ok(b) = any.extract::<Vec<u8>>() {
return Ok(Value::Bytes(b));
}
Err(to_pyerr(format!("Value unsupported {any:?}"))) Err(to_pyerr(format!("Value unsupported {any:?}")))
} }

View File

@ -1,3 +1,4 @@
from io import BytesIO
import tantivy import tantivy
import pytest import pytest
@ -531,3 +532,27 @@ class TestJsonField:
# ) # )
# result = index.searcher().search(query, 2) # result = index.searcher().search(query, 2)
# assert len(result.hits) == 1 # assert len(result.hits) == 1
@pytest.mark.parametrize('bytes_kwarg', [True, False])
@pytest.mark.parametrize('bytes_payload', [
b"abc",
bytearray(b"abc"),
memoryview(b"abc"),
BytesIO(b"abc").read(),
BytesIO(b"abc").getbuffer(),
])
def test_bytes(bytes_kwarg, bytes_payload):
schema = SchemaBuilder().add_bytes_field("embedding").build()
index = Index(schema)
writer = index.writer()
if bytes_kwarg:
doc = Document(id=1, embedding=bytes_payload)
else:
doc = Document(id=1)
doc.add_bytes("embedding", bytes_payload)
writer.add_document(doc)
writer.commit()
index.reload()