2021-10-18 13:31:07 +00:00
|
|
|
[![Build Status](https://travis-ci.org/quickwit-inc/tantivy-py.svg?branch=master)](https://travis-ci.org/quickwit-inc/tantivy-py)
|
2019-08-08 13:54:27 +00:00
|
|
|
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
2019-08-08 13:48:26 +00:00
|
|
|
|
2019-06-06 10:12:57 +00:00
|
|
|
tantivy-py
|
|
|
|
==========
|
2019-06-04 09:09:58 +00:00
|
|
|
|
2020-01-25 09:11:27 +00:00
|
|
|
Python bindings for Tantivy.
|
2019-06-04 09:09:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Installation
|
|
|
|
|
2020-01-05 16:39:20 +00:00
|
|
|
The bindings can be installed using from pypi using pip:
|
2019-06-04 09:09:58 +00:00
|
|
|
|
2020-01-25 09:11:27 +00:00
|
|
|
pip install tantivy
|
2019-06-04 09:09:58 +00:00
|
|
|
|
2020-01-05 16:39:20 +00:00
|
|
|
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](https://github.com/PyO3/pyo3), which
|
2021-02-23 01:02:16 +00:00
|
|
|
only supports python3.
|
2019-06-04 09:09:58 +00:00
|
|
|
|
2020-01-05 16:39:20 +00:00
|
|
|
# Development
|
|
|
|
|
2021-03-19 09:26:19 +00:00
|
|
|
Setting up a development environment can be done in a virtual environment using
|
2022-01-17 12:57:34 +00:00
|
|
|
[`nox`](https://nox.thea.codes) or using local packages using the provided `Makefile`.
|
2020-01-05 16:39:20 +00:00
|
|
|
|
2022-01-17 12:57:34 +00:00
|
|
|
For the `nox` setup install the virtual environment and build the bindings using:
|
2020-01-05 16:39:20 +00:00
|
|
|
|
2022-01-17 12:57:34 +00:00
|
|
|
python3 -m pip install nox
|
|
|
|
nox
|
2020-01-05 16:39:20 +00:00
|
|
|
|
|
|
|
For the `Makefile` based setup run:
|
|
|
|
|
|
|
|
make
|
|
|
|
|
|
|
|
Running the tests is done using:
|
|
|
|
|
|
|
|
make test
|
|
|
|
|
2019-06-04 09:09:58 +00:00
|
|
|
# Usage
|
|
|
|
|
2020-01-25 09:11:27 +00:00
|
|
|
The Python bindings have a similar API to Tantivy. To create a index first a schema
|
2019-06-04 09:09:58 +00:00
|
|
|
needs to be built. After that documents can be added to the index and a reader
|
|
|
|
can be created to search the index.
|
|
|
|
|
|
|
|
```python
|
2019-08-02 11:23:10 +00:00
|
|
|
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()
|
2019-10-06 11:47:50 +00:00
|
|
|
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."""],
|
|
|
|
))
|
2019-08-02 11:23:10 +00:00
|
|
|
# ... and committing
|
|
|
|
writer.commit()
|
|
|
|
|
|
|
|
|
|
|
|
# Reload the index to ensure it points to the last commit.
|
2019-10-06 20:59:08 +00:00
|
|
|
index.reload()
|
2019-08-02 11:23:10 +00:00
|
|
|
searcher = index.searcher()
|
2019-10-06 11:47:50 +00:00
|
|
|
query = index.parse_query("fish days", ["title", "body"])
|
2019-08-02 11:23:10 +00:00
|
|
|
|
2020-01-05 16:22:58 +00:00
|
|
|
(best_score, best_doc_address) = searcher.search(query, 3).hits[0]
|
2020-01-05 16:39:20 +00:00
|
|
|
best_doc = searcher.doc(best_doc_address)
|
2019-08-02 11:23:10 +00:00
|
|
|
assert best_doc["title"] == ["The Old Man and the Sea"]
|
2019-10-06 11:47:50 +00:00
|
|
|
print(best_doc)
|
2019-06-04 09:09:58 +00:00
|
|
|
```
|