yeraydiazdiaz / lunr.py

A Python implementation of Lunr.js 🌖
http://lunr.readthedocs.io
MIT License
187 stars 16 forks source link

Incorrect search results (partial match) #147

Open wolfiex opened 5 months ago

wolfiex commented 5 months ago

When using the query tas lunr matches on ta rather than the full word.

yeraydiazdiaz commented 5 months ago

This is expected behaviour as tas is stemmed to ta, both on index building time and when searching.

To disable the stemmer you'll need to disable it from both the indexing pipeline and the search results pipeline, something like:

from lunr import get_default_builder
from lunr.stemmer import stemmer

docs = [
    {
        "id": 1,
        "text": "rax tas ras",
    },
    {
        "id": 2,
        "text": "rex tas res",
    },
]

builder = get_default_builder()
builder.ref("id")
builder.field("text")
builder.pipeline.remove(stemmer)
builder.search_pipeline.remove(stemmer)
for doc in docs:
    builder.add(doc)

idx = builder.build()
results = idx.search("tas")
assert len(results) == 2