2019-08-08 13:48:26 +00:00
|
|
|
[![Build Status](https://travis-ci.org/tantivy-search/tantivy-py.svg?branch=master)](https://travis-ci.org/tantivy-search/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
|
|
|
|
|
|
|
Python bindings for tantivy.
|
|
|
|
|
|
|
|
|
|
|
|
# Installation
|
|
|
|
|
|
|
|
The bindings can be installed using setuptools:
|
|
|
|
|
2019-06-06 10:12:57 +00:00
|
|
|
python3 setup.py install --user
|
2019-06-04 09:09:58 +00:00
|
|
|
|
|
|
|
Note that this requires setuptools-rust to be installed. Another thing to note
|
|
|
|
is that the bindings are using [PyO3](https://github.com/PyO3/pyo3), which
|
2019-06-06 10:12:57 +00:00
|
|
|
requires rust nightly and only supports python3.
|
2019-06-04 09:09:58 +00:00
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
2019-06-06 10:12:57 +00:00
|
|
|
tantivy-py has 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
|
|
|
|
builder = tantivy.SchemaBuilder()
|
|
|
|
|
|
|
|
title = builder.add_text_field("title", stored=True)
|
|
|
|
body = builder.add_text_field("body")
|
|
|
|
|
|
|
|
schema = builder.build()
|
|
|
|
index = tantivy.Index(schema)
|
|
|
|
|
|
|
|
writer = index.writer()
|
|
|
|
|
|
|
|
doc = tantivy.Document()
|
|
|
|
doc.add_text(title, "The Old Man and the Sea")
|
|
|
|
doc.add_text(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."))
|
|
|
|
writer.add_document(doc)
|
2019-08-01 08:22:40 +00:00
|
|
|
writer.commit()
|
|
|
|
|
2019-06-04 09:09:58 +00:00
|
|
|
reader = index.reader()
|
|
|
|
searcher = reader.searcher()
|
|
|
|
|
|
|
|
query_parser = tantivy.QueryParser.for_index(index, [title, body])
|
|
|
|
query = query_parser.parse_query("sea whale")
|
|
|
|
|
|
|
|
top_docs = tantivy.TopDocs(10)
|
|
|
|
result = searcher.search(query, top_docs)
|
|
|
|
|
|
|
|
_, doc_address = result[0]
|
|
|
|
|
|
|
|
searched_doc = searcher.doc(doc_address)
|
|
|
|
assert searched_doc.get_first(title) == "The Old Man and the Sea"
|
|
|
|
```
|