Go to file
Damir Jelić c84c3ad403 Cargo.toml: Add some additional python metadata. 2020-01-05 16:49:23 +01:00
ci travis: Add deployment configuration. 2020-01-05 16:48:49 +01:00
src index: Remove a redundant clone. 2020-01-05 16:00:02 +01:00
tantivy Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
tests Merge remote-tracking branch 'origin/search_api_simplification' 2020-01-05 13:52:35 +01:00
.gitignore gitignore: Add the new maturin provided so file to the ignores. 2020-01-05 14:58:38 +01:00
.travis.yml travis: Add deployment configuration. 2020-01-05 16:48:49 +01:00
Cargo.toml Cargo.toml: Add some additional python metadata. 2020-01-05 16:49:23 +01: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 Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
Pipfile Add a Pipfile. 2020-01-05 14:56:50 +01:00
Pipfile.lock Add a Pipfile. 2020-01-05 14:56:50 +01:00
README.md Update README.md 2019-10-06 21:59:08 +01:00
pyproject.toml pyptoject: Use maturin from now on. 2020-01-05 13:52:53 +01:00
rust-toolchain rust-toolchain: Update the toolchain version. 2020-01-05 12:40:37 +01:00
rustfmt.toml Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
tox.ini tantivy: Add a tox.ini file. 2019-08-08 14:22:23 +02:00

README.md

Build Status License: MIT

tantivy-py

Python bindings for tantivy.

Installation

The bindings can be installed using setuptools:

python3 setup.py install --user

Note that this requires setuptools-rust to be installed. Another thing to note is that the bindings are using PyO3, which requires rust nightly and only supports python3.

Usage

tantivy-py has 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"])
top_docs = tantivy.TopDocs(3)

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