Go to file
Paul Masurel ccd8021ff9 CR: Adding trailing lines and removing some trailing spaces. 2019-08-30 07:37:20 +09:00
src CR: Adding trailing lines and removing some trailing spaces. 2019-08-30 07:37:20 +09:00
tantivy CR: Adding trailing lines and removing some trailing spaces. 2019-08-30 07:37:20 +09:00
tests CR: Adding trailing lines and removing some trailing spaces. 2019-08-30 07:37:20 +09:00
.gitignore Fixing build. 2019-08-01 17:22:40 +09:00
.travis.yml travis: Add the cargo bin path to the PATH. 2019-08-08 15:14:45 +02:00
Cargo.toml Fixed tantivy rev 2019-08-21 07:19:00 +09: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
README.md API simplification. 2019-08-14 17:49:18 +09:00
pyproject.toml Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
rust-toolchain Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
rustfmt.toml Initial python bindings implementation. 2019-06-04 11:09:58 +02:00
setup.py setup.py: Add a description and long description. 2019-08-03 12:17:04 +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({
    "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("sea whale", ["title", "body"])
top_docs = tantivy.TopDocs(3)

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