{"id":7385,"library":"lunr","title":"Lunr.py","description":"Lunr.py is a Python implementation of Lunr.js, a lightweight full-text search library designed for client-side search. It enables developers to create search indexes from Python data structures, often for serialization and consumption by a JavaScript frontend. The library is actively maintained, with its current version being 0.8.0, and targets close compatibility with the original Lunr.js implementation.","status":"active","version":"0.8.0","language":"en","source_language":"en","source_url":"https://github.com/yeraydiazdiaz/lunr.py","tags":["full-text search","search engine","client-side search","lunr.js","indexing"],"install":[{"cmd":"pip install lunr","lang":"bash","label":"Standard installation"},{"cmd":"pip install lunr[languages]","lang":"bash","label":"Installation with language support (NLTK)"}],"dependencies":[{"reason":"Required for optional language stemming support.","package":"nltk","optional":true}],"imports":[{"note":"The primary index creation function is named `lunr` and needs to be explicitly imported from the `lunr` package.","wrong":"import lunr","symbol":"lunr","correct":"from lunr import lunr"}],"quickstart":{"code":"from lunr import lunr\n\ndocuments = [\n    {\n        \"id\": \"1\",\n        \"title\": \"Alice's Adventures in Wonderland\",\n        \"body\": \"Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?'\"\n    },\n    {\n        \"id\": \"2\",\n        \"title\": \"Through the Looking-Glass\",\n        \"body\": \"One thing was certain, that the white kitten had had nothing to do with it: it was the black kitten's fault entirely.\"\n    },\n    {\n        \"id\": \"3\",\n        \"title\": \"The Hunting of the Snark\",\n        \"body\": \"'Just the place for a Snark!' the Bellman cried, As he landed his crew with a thump and a shake. 'Just the place for a Snark! I have sought it for years!'\"\n    }\n]\n\nidx = lunr(ref='id', fields=('title', 'body'), documents=documents)\n\nresults = idx.search(\"Alice sister\")\nfor result in results:\n    print(f\"Document Ref: {result['ref']}, Score: {result['score']}\")\n\nresults_exact = idx.search(\"+snark -alice\")\nprint(f\"\\nExact search results for '+snark -alice':\")\nfor result in results_exact:\n    print(f\"Document Ref: {result['ref']}, Score: {result['score']}\")","lang":"python","description":"This quickstart demonstrates how to create a Lunr index from a list of dictionaries. It defines a unique reference field (`id`), fields to be searched (`title`, `body`), and the documents themselves. It then performs a basic search and a more precise search using term presence modifiers (`+` for required, `-` for prohibited)."},"warnings":[{"fix":"Upgrade Python to 3.7 or newer. Alternatively, pin `lunr<0.7.0`.","message":"Version 0.7.0 dropped support for Python 3.6. Users on older Python versions must upgrade to Python 3.7+ or use an earlier Lunr.py version.","severity":"breaking","affected_versions":">=0.7.0"},{"fix":"Be prepared for potential API adjustments in future minor or patch releases, and review changelogs carefully during upgrades.","message":"The Lunr.py API is considered in 'alpha stage' and is explicitly stated as 'likely to change' by the maintainers.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Test thoroughly when using language support, especially if interoperability with Lunr.js is a key requirement. Be aware of NLTK corpus licensing.","message":"Using the optional `lunr[languages]` feature for non-English stemming relies on NLTK and currently does not guarantee full compatibility with the JavaScript Lunr.js index format or search results.","severity":"gotcha","affected_versions":"All versions with `[languages]` extra"},{"fix":"Monitor memory usage with large datasets. Consider pre-building and serializing indexes for client-side consumption (e.g., by Lunr.js) or for faster loading in Python, if applicable.","message":"Lunr stores its inverted index entirely in memory. For very large document corpuses, this can consume significant RAM and may require recreation or re-reading at each application startup, impacting performance.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure you use `from lunr import lunr` at the top of your script.","cause":"The `lunr` function, which is the main entry point for creating an index, was not correctly imported from the `lunr` package.","error":"NameError: name 'lunr' is not defined"},{"fix":"The `lunr` function is called directly with arguments to create an index: `idx = lunr(...)`. The `idx.search()` method returns a list of dictionaries, so iterate `for result in results:` before accessing `result['ref']` etc.","cause":"Attempting to access results from `idx.search()` as if it were a dictionary before iterating over the list of result objects, or calling `lunr` directly with parentheses, which returns the builder function itself.","error":"TypeError: 'builtin_function_or_method' object is not subscriptable"},{"fix":"By default, Lunr searches with logical OR. To enforce required terms, prefix them with `+` (e.g., `'+term1 term2'`). To prohibit terms, prefix with `-` (e.g., `'term1 -term2'`).","cause":"Misunderstanding the default OR logic of Lunr search queries or incorrect use of term presence modifiers (`+` for required, `-` for prohibited).","error":"Unexpected search results (e.g., too many results or missing specific terms)"}]}