Skip to content

Commit

Permalink
Fix fuzzy_term_query() documentation. (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
mocobeta authored Apr 13, 2024
1 parent 0a320fc commit b857dfe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl Query {
/// * `field_name` - Field name to be searched.
/// * `text` - String representation of the query term.
/// * `distance` - (Optional) Edit distance you are going to alow. When not specified, the default is 1.
/// * `transposition_cost_one` - (Optional) If true, a transposition cost will be 1; otherwise it will be 2. When not specified, the default is true.
/// * `prefix` - (Optional) If true, only prefix matched results are returned. When not specified, the default is false.
/// * `transposition_cost_one` - (Optional) If true, a transposition (swapping) cost will be 1; otherwise it will be 2. When not specified, the default is true.
/// * `prefix` - (Optional) If true, prefix levenshtein distance is applied. When not specified, the default is false.
#[staticmethod]
#[pyo3(signature = (schema, field_name, text, distance = 1, transposition_cost_one = true, prefix = false))]
pub(crate) fn fuzzy_term_query(
Expand Down
36 changes: 30 additions & 6 deletions tests/tantivy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,24 +775,48 @@ def test_all_query(self, ram_index):
def test_fuzzy_term_query(self, ram_index):
index = ram_index
query = Query.fuzzy_term_query(index.schema, "title", "ice")

# the query "ice" should match "mice"
result = index.searcher().search(query, 10)
assert len(result.hits) == 1
_, doc_address = result.hits[0]
searched_doc = index.searcher().doc(doc_address)
assert searched_doc["title"] == ["Of Mice and Men"]

def test_fuzzy_term_query_prefix(self, ram_index):
index = ram_index
query = Query.fuzzy_term_query(index.schema, "title", "man", prefix=True)

# the query "man" should match both "man" and "men"
query = Query.fuzzy_term_query(index.schema, "title", "mna")
# the query "mna" should match "man" since the default transposition cost is 1.
result = index.searcher().search(query, 10)
assert len(result.hits) == 1
titles = set()
for _, doc_address in result.hits:
titles.update(index.searcher().doc(doc_address)["title"])
assert titles == {"The Old Man and the Sea"}

query = Query.fuzzy_term_query(index.schema, "title", "mna", transposition_cost_one=False)
# the query "mna" should not match any doc since the default distance is 1 and transposition cost is set to 2.
result = index.searcher().search(query, 10)
assert len(result.hits) == 0

query = Query.fuzzy_term_query(index.schema, "title", "mna", distance=2, transposition_cost_one=False)
# the query "mna" should match both "man" and "men" since distance is set to 2.
result = index.searcher().search(query, 10)
assert len(result.hits) == 2
titles = set()
for _, doc_address in result.hits:
titles.update(index.searcher().doc(doc_address)["title"])
assert titles == {"The Old Man and the Sea", "Of Mice and Men"}

query = Query.fuzzy_term_query(index.schema, "title", "fraken")
# the query "fraken" should not match any doc.
result = index.searcher().search(query, 10)
assert len(result.hits) == 0

query = Query.fuzzy_term_query(index.schema, "title", "fraken", prefix=True)
# the query "fraken" should match "franken", the prefix of "frankenstein", with edit distance 1.
result = index.searcher().search(query, 10)
assert len(result.hits) == 1
titles = set()
for _, doc_address in result.hits:
titles.update(index.searcher().doc(doc_address)["title"])
assert titles == {"Frankenstein", "The Modern Prometheus"}


0 comments on commit b857dfe

Please sign in to comment.