Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export tantivy query classes #53

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tantivy"
version = "0.17.0"
version = "0.17.1"
readme = "README.md"
authors = ["Damir Jelić <[email protected]>"]
edition = "2018"
Expand All @@ -11,15 +11,17 @@ name = "tantivy"
crate-type = ["cdylib"]

[build-dependencies]
pyo3-build-config = "0.16.3"
pyo3-build-config = "0.15.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why re going backwards here on version?


[dependencies]
chrono = "0.4.19"
tantivy = "0.17"
itertools = "0.10.3"
futures = "0.3.21"
serde_json = "1.0.64"
serde = "1.0"
serde_derive = "1.0"

[dependencies.pyo3]
version = "0.16.3"
version = "0.15.0"
features = ["extension-module"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Running the tests is done using:

make test

Building python wheel
pip install maturin
maturin build

# Usage

The Python bindings have a similar API to Tantivy. To create a index first a schema
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use index::Index;
use schema::Schema;
use schemabuilder::SchemaBuilder;
use searcher::{DocAddress, Searcher};
use query::Query;

/// Python bindings for the search engine library Tantivy.
///
Expand Down Expand Up @@ -75,6 +76,7 @@ fn tantivy(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Index>()?;
m.add_class::<DocAddress>()?;
m.add_class::<Facet>()?;
m.add_class::<Query>()?;
Ok(())
}

Expand Down
88 changes: 87 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::to_pyerr;
use pyo3::prelude::*;
use std::ops::Bound;
use tantivy as tv;
use tv::schema::{Field, Term};

/// Tantivy's Query
#[pyclass]
Expand All @@ -15,7 +18,90 @@ impl Query {

#[pymethods]
impl Query {
#[staticmethod]
fn term(field_id: u32, text: &str) -> Query {
let term = Term::from_field_text(Field::from_field_id(field_id), text);
Query {
inner: Box::new(tv::query::TermQuery::new(
term,
tv::schema::IndexRecordOption::Basic,
)),
}
}

#[staticmethod]
fn fuzzy_term(field_id: u32, distance: u8, text: &str) -> Query {
let ftq = tv::query::FuzzyTermQuery::new(
Term::from_field_text(Field::from_field_id(field_id), text),
distance,
true,
);
Query {
inner: (Box::new(ftq)),
}
}

#[staticmethod]
fn regex(field_id: u32, pattern: &str) -> PyResult<Query> {
let rq = tv::query::RegexQuery::from_pattern(
pattern,
Field::from_field_id(field_id),
)
.map_err(to_pyerr)?;
Ok(Query {
inner: Box::new(rq),
})
}

#[staticmethod]
fn phrase(field_id: u32, words: Vec<&str>) -> Query {
let terms = words
.iter()
.map(|&w| Term::from_field_text(Field::from_field_id(field_id), w))
.collect::<Vec<_>>();
Query {
inner: Box::new(tv::query::PhraseQuery::new(terms)),
}
}

#[staticmethod]
fn boost(q: &Query, boost: f32) -> Query {
let bq = tv::query::BoostQuery::new(q.get().box_clone(), boost);
Query {
inner: Box::new(bq),
}
}

#[staticmethod]
fn and_q(qs : Vec<PyRef<Query>>) -> Query {
Query {
inner: Box::new(tv::query::BooleanQuery::intersection(
qs.iter().map(|q| q.get().box_clone()).collect::<Vec<_>>()
))
}
}

#[staticmethod]
fn or_q(qs : Vec<PyRef<Query>>) -> Query {
Query {
inner: Box::new(tv::query::BooleanQuery::union(
qs.iter().map(|q| q.get().box_clone()).collect::<Vec<_>>()
))
}
}

#[staticmethod]
fn range_q(field_id: u32, left: &str, right: &str) -> Query {
Query {
inner: Box::new(tv::query::RangeQuery::new_str_bounds(
Field::from_field_id(field_id),
Bound::Included(left),
Bound::Included(right),
)),
}
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("Query({:?})", self.get()))
Ok(format!("{:#?}", self.get()))
}
}