Skip to content

Expose const score query #256

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

Merged
merged 3 commits into from
Apr 28, 2024
Merged
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
16 changes: 16 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl Query {
})
}

/// Construct a Tantivy's BooleanQuery
#[staticmethod]
#[pyo3(signature = (subqueries))]
pub(crate) fn boolean_query(
Expand Down Expand Up @@ -199,6 +200,7 @@ impl Query {
})
}

/// Construct a Tantivy's BoostQuery
#[staticmethod]
#[pyo3(signature = (query, boost))]
pub(crate) fn boost_query(query: Query, boost: f32) -> PyResult<Query> {
Expand All @@ -208,6 +210,7 @@ impl Query {
})
}

/// Construct a Tantivy's RegexQuery
#[staticmethod]
#[pyo3(signature = (schema, field_name, regex_pattern))]
pub(crate) fn regex_query(
Expand All @@ -226,4 +229,17 @@ impl Query {
Err(e) => Err(to_pyerr(e)),
}
}

/// Construct a Tantivy's ConstScoreQuery
#[staticmethod]
#[pyo3(signature = (query, score))]
pub(crate) fn const_score_query(
query: Query,
score: f32,
) -> PyResult<Query> {
let inner = tv::query::ConstScoreQuery::new(query.inner, score);
Ok(Query {
inner: Box::new(inner),
})
}
}
4 changes: 4 additions & 0 deletions tantivy/tantivy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ class Query:
def regex_query(schema: Schema, field_name: str, regex_pattern: str) -> Query:
pass

@staticmethod
def const_score_query(query: Query, score: float) -> Query:
pass

class Order(Enum):
Asc = 1
Desc = 2
Expand Down
31 changes: 31 additions & 0 deletions tests/tantivy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,3 +1057,34 @@ def test_regex_query(self, ram_index):
ValueError, match=r"An invalid argument was passed: 'fish\('"
):
Query.regex_query(index.schema, "body", "fish(")

def test_const_score_query(self, ram_index):
index = ram_index

query = Query.regex_query(index.schema, "body", "fish")
const_score_query = Query.const_score_query(
query, score = 1.0
)
result = index.searcher().search(const_score_query, 10)
assert len(result.hits) == 1
score, _ = result.hits[0]
# the score should be 1.0
assert score == pytest.approx(1.0)

const_score_query = Query.const_score_query(
Query.const_score_query(
query, score = 1.0
), score = 0.5
)

result = index.searcher().search(const_score_query, 10)
assert len(result.hits) == 1
score, _ = result.hits[0]
# nested const score queries should retain the
# score of the outer query
assert score == pytest.approx(0.5)

# wrong score type
with pytest.raises(TypeError, match = r"argument 'score': must be real number, not str"):
Query.const_score_query(query, "0.1")