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.

The typical use case is to integrate Lunr in a web application, an example would be the MkDocs documentation library. In order to do this, you'd integrate Lunr.js in the Javascript code of your application, which will need to fetch and parse a JSON of your documents and create the index at startup of your application. Depending on the size of your document set this can take some time and potentially block the browser's main thread.

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

Of course you could also use Lunr.py to power full text search in desktop applications or backend services to search on your documents mimicking Elasticsearch.

Installation

Simply pip install lunr for the english only, best compatibility with Lunr.js version.

An optional and experimental support for other languages via the Natural Language Toolkit stemmers is also available via pip install lunr[languages]. Please refer to the documentation page on languages for more information.

Current state

Each version of lunr.py targets a specific version of lunr.js and produces the same results as it both in Python 2.7 and 3 for non-trivial corpus of documents.

Lunr.py also serializes Index instances respecting the lunr-schema which are consumable by Lunr.js and viceversa.

The API is in alpha stage and likely to change.

Usage

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.6.tar.gz (43.8 kB view details)

Uploaded Source

Built Distribution

lunr-0.5.6-py2.py3-none-any.whl (36.3 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: lunr-0.5.6.tar.gz
  • Upload date:
  • Size: 43.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for lunr-0.5.6.tar.gz
Algorithm Hash digest
SHA256 7be69d7186f65784a4f2adf81e5c58efd6a9921aa95966babcb1f2f2ada75c20
MD5 170c4721ce687dcca9ce1e38ceae5535
BLAKE2b-256 380d7246e257d4b6013047b46b2ab97b148fe076df1a6ca0c55c70688490565f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lunr-0.5.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 36.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for lunr-0.5.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1208622930c915a07e6f8e8640474357826bad48534c0f57969b6fca9bffc88e
MD5 235f6f1535d7549a7dcc739bf8785402
BLAKE2b-256 2dc47fcfac60ec57e8bbad3ff13bb3981f4cec99a84a82435d532dc92d416188

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