Skip to main content

A Python implementation of Lunr.js

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lunr-0.5.9.tar.gz (45.7 kB view details)

Uploaded Source

Built Distribution

lunr-0.5.9-py2.py3-none-any.whl (34.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file lunr-0.5.9.tar.gz.

File metadata

  • Download URL: lunr-0.5.9.tar.gz
  • Upload date:
  • Size: 45.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for lunr-0.5.9.tar.gz
Algorithm Hash digest
SHA256 5ee777b068e2234c4a9496ecbd0abe7aa5e8ad5fdc7b79a09876669252b71202
MD5 b1b12cfc5e983bc3e99d608baf245ea5
BLAKE2b-256 80443e8a6d96db25afe7b8ff1b9ae37338f245da459566a59fc4c29331ebd2b0

See more details on using hashes here.

File details

Details for the file lunr-0.5.9-py2.py3-none-any.whl.

File metadata

  • Download URL: lunr-0.5.9-py2.py3-none-any.whl
  • Upload date:
  • Size: 34.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for lunr-0.5.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 43db3642a95e8093f74c7e5991e506762f28a2ce90d231b5d3d61b70476d59db
MD5 efc1750d7acfba242dac4732fc693d6b
BLAKE2b-256 7e94bed12f54fe7af6b8b2854aad4437f1e8a65615740a909f50217ec85172ef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page