Go to file
messense f901b8b081 Switch from pipenv to nox 2022-01-17 21:26:11 +08:00
.cargo travis: Enable mac build. 2020-01-06 15:13:10 +01:00
.github/workflows Switch from pipenv to nox 2022-01-17 21:26:11 +08:00
ci ci: Don't use the master branch of the maturin docker image. 2020-10-11 14:21:45 +02:00
src Update to pyo3 0.15 (#40) 2022-01-17 10:16:08 +09:00
tantivy Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
tests Avoid truncating utf-8 strings in the middle of a codepoint in 2020-10-01 22:36:28 +09:00
.gitignore gitignore: Add the new maturin provided so file to the ignores. 2020-01-05 14:58:38 +01:00
.travis.yml Fixed links 2021-10-18 13:31:07 +00:00
Cargo.toml Switch from pipenv to nox 2022-01-17 21:26:11 +08:00
LICENSE Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
MANIFEST.in Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
Makefile Fix build issues due to maturin & py03 versions (#37) 2022-01-03 22:50:19 +09:00
README.md Switch from pipenv to nox 2022-01-17 21:26:11 +08:00
build.rs Update to pyo3 0.15 (#40) 2022-01-17 10:16:08 +09:00
noxfile.py Switch from pipenv to nox 2022-01-17 21:26:11 +08:00
pyproject.toml Switch from pipenv to nox 2022-01-17 21:26:11 +08:00
requirements-dev.txt Switch from pipenv to nox 2022-01-17 21:26:11 +08:00
rustfmt.toml Initial python bindings implementation. 2019-06-04 11:09:58 +02:00

README.md

Build Status License: MIT

tantivy-py

Python bindings for Tantivy.

Installation

The bindings can be installed using from pypi using pip:

pip install tantivy

If no binary wheel is present for your operating system the bindings will be build from source, this means that Rust needs to be installed before building can succeed.

Note that the bindings are using PyO3, which only supports python3.

Development

Setting up a development environment can be done in a virtual environment using nox or using local packages using the provided Makefile.

For the nox setup install the virtual environment and build the bindings using:

python3 -m pip install nox
nox

For the Makefile based setup run:

make

Running the tests is done using:

make test

Usage

The Python bindings have a similar API to Tantivy. To create a index first a schema needs to be built. After that documents can be added to the index and a reader can be created to search the index.

import tantivy

# Declaring our schema.
schema_builder = tantivy.SchemaBuilder()
schema_builder.add_text_field("title", stored=True)
schema_builder.add_text_field("body", stored=True)
schema = schema_builder.build()

# Creating our index (in memory, but filesystem is available too)
index = tantivy.Index(schema)


# Adding one document.
writer = index.writer()
writer.add_document(tantivy.Document(
    title=["The Old Man and the Sea"],
    body=["""He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish."""],
))
# ... and committing
writer.commit()


# Reload the index to ensure it points to the last commit.
index.reload()
searcher = index.searcher()
query = index.parse_query("fish days", ["title", "body"])

(best_score, best_doc_address) = searcher.search(query, 3).hits[0]
best_doc = searcher.doc(best_doc_address)
assert best_doc["title"] == ["The Old Man and the Sea"]
print(best_doc)