Skip to main content

Build Status codecov Supported Python Versions PyPI Read the Docs Downloads

Lunr.py

A Python implementation of Lunr.js by Oliver Nightingale.

A bit like Solr, but much smaller and not as bright.

This Python version of Lunr.js aims to bring the simple and powerful full text search capabilities into Python guaranteeing results as close as the original implementation as possible.

What does this even do?

Lunr is a simple full text search solution for situations where deploying a full scale solution like Elasticsearch isn't possible, viable or you're simply prototyping. Lunr parses a set of documents and creates an inverted index for quick full text searches in the same way other more complicated solution.

The trade-off is that Lunr keeps the inverted index in memory and requires you to recreate or read the index at the start of your application.

Interoperability with Lunr.js

A core objective of Lunr.py is to provide interoperability with the JavaScript version.

An example can be found in the MkDocs documentation library. MkDocs produces a set of documents from the pages of the documentation and uses Lunr.js in the frontend to power its built-in searching engine. This set of documents is in the form of a JSON file which needs to be fetched and parsed by Lunr.js to create the inverted index at startup of your application.

While this is not a problem for most sites, depending on the size of your document set, this can take some time.

Lunr.py provides a backend solution, allowing you to parse the documents in Python of time and create a serialized Lunr.js index you can pass have the browser version read, minimizing start up time of your application.

Each version of lunr.py targets a specific version of lunr.js and produces the same results for a non-trivial corpus of documents.

Installation

pip install lunr

An optional and experimental support for other languages thanks to the Natural Language Toolkit stemmers is also available via pip install lunr[languages]. The usage of the language feature is subject to NTLK corpus licensing clauses.

Please refer to the documentation page on languages for more information.

Usage

First, you'll need a list of dicts representing the documents you want to search on. These documents must have a unique field which will serve as a reference and a series of fields you'd like to search on.

Lunr provides a convenience lunr function to quickly index this set of documents:

>>> from lunr import lunr
>>>
>>> documents = [{
...     'id': 'a',
...     'title': 'Mr. Green kills Colonel Mustard',
...     'body': 'Mr. Green killed Colonel Mustard in the study with the candlestick.',
... }, {
...     'id': 'b',
...     'title': 'Plumb waters plant',
...     'body': 'Professor Plumb has a green plant in his study',
... }]
>>> idx = lunr(
...     ref='id', fields=('title', 'body'), documents=documents
... )
>>> idx.search('kill')
[{'ref': 'a', 'score': 0.6931722372559913, 'match_data': <MatchData "kill">}]
>>> idx.search('study')
[{'ref': 'b', 'score': 0.23576799568081389, 'match_data': <MatchData "studi">}, {'ref': 'a', 'score': 0.2236629211724517, 'match_data': <MatchData "studi">}]

Please refer to the documentation for more usage examples.

Development

Clone this repo and follow these steps:

  1. Create a virtualenv on the lastest supported Python version
  2. Install node.js if necessary, it will be needed for acceptance tests
  3. make install-dev
  4. make tests-full to check the installation is working
  5. To run the tests on all Python versions you'll need to install the different supported interpreters.

Metadata

Release files for lunr 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lunr 0.8.1
File Size Uploaded Provenance Actions
lunr-0.8.1.tar.gz 850.7 kB Without provenance Details for lunr-0.8.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lunr 0.8.1
File Interpreter ABI Platform Actions
lunr-0.8.1-py3-none-any.whl Python 3 none any Details for lunr-0.8.1-py3-none-any.whl

Total release size: 885.7 kB

Release files / lunr-0.8.1.tar.gz

Download URL lunr-0.8.1.tar.gz
Size 850.7 kB
Tags Source
SHA-256 checksum
How to use checksums
46721d6b532edd25222cf1c95576e917a029d2c886457609840d661bd4c340b6
BLAKE2b-256 checksum
How to use checksums
9953f7bfd1ea94706fa516d1336347333d5d392a19524da71020cce4a93a6621
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Release files / lunr-0.8.1-py3-none-any.whl

Download URL lunr-0.8.1-py3-none-any.whl
Size 35.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4bbad00cf2d8e570d6f7f8029263af55b3626b0d085b941e301dcea43e8f95ce
BLAKE2b-256 checksum
How to use checksums
991f093c1b972f8427c86f30ebb23cb306cd81618915d09c37f700407b3c8fff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Security for lunr 0.8.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page