yeraydiazdiaz / lunr.py

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

Field-specific pipeline skips are not respected in search #152

Open dhdaines opened 1 month ago

dhdaines commented 1 month ago

Lunr allows you to skip steps in the pipeline for specific fields, as detailed in the customization document. Unfortunately this functionality is not very useful, because these skips are only applied during indexing and not during search. Here is an example:

from lunr import lunr, get_default_builder, stop_word_filter, stemmer

builder = get_default_builder()
builder.pipeline.skip(stop_word_filter.stop_word_filter, ["title"])
# Add the stop word filter to the pipeline (see #151)
builder.search_pipeline.before(stemmer.stemmer, stop_word_filter.stop_word_filter)
builder.search_pipeline.skip(stop_word_filter.stop_word_filter, ["title"])

index = lunr(
    ref="id",
    fields=["title", "body"],
    documents=[
        # The title is entirely stopwords, but will index them anyway
        {"id": "1", "title": "To be or not to be?", "body": "That is the question!"}
    ],
    builder=builder,
)
print(index.search("title:to"))  # Should print something, but doesn't
print(index.search("body:the"))  # Should print []
print(index.search("to"))        # Should print something, but doesn't

Because skipping fields isn't a super common thing to do, but also because of #151 (the stop word filter is never applied to the query anyway) this isn't always noticeable in normal use, which is probably why it never got fixed in lunr.js.

As with #151 this can't be fixed without breaking bug-compatibility with lunr.js (see https://github.com/olivernn/lunr.js/blob/master/lib/index.js#L173), but I wanted to document the workaround.