Skip to main content

In memory text search engine written in python

Project description

TextSearchPy

TextSearchPy is a high performance, easy to use, in-memory text search engine library written in Python.

Install

pip install textsearchpy

Quickstart

Default Index is created with SimpleTokenizer and LowerCaseNormalizer

from textsearchpy import Index, Document

# create index with default settings
index = Index()

# add to index
index.append(["The quick brown fox"])

# alternatively, create Document object and append
doc = Document(text="jumps over the lazy dog")
index.append([doc])

# search with query string, returns list of Document objects
print(index.search("fox"))
print(index.search("fox OR dog"))
print(index.search("fox AND dog"))
print(index.search("fox NOT quick"))

# shows how input text is tokenized & normalized
print(index.text_to_index_tokens("The quick brown fox"))

Query Syntax

Query can be written in string format (shown in quickstart) or by creating different Query objects

query = "fox"

query = "fox AND dog NOT quick"

query = "(fox OR dog) AND (quick OR lazy)"

# proximity search with distance of 1
query = '"quick fox"~1'

Supported Query Types:

TermQuery - query with single token

from textsearchpy.query import TermQuery

query = "fox"
query = TermQuery(term="fox)

BooleanQuery - sub queries with boolean condition

Clause - "SHOULD", "MUST", "MUST_NOT"

BooleanClause accept any query type, thus it is possible to create a tree of grouped boolean queries

from textsearchpy.query import TermQuery, BooleanClause, BooleanQuery

query = "(fox OR dog) AND (quick NOT lazy)"

term_1 = TermQuery(term="fox")
term_2 = TermQuery(term="dog")
query_left = BooleanQuery(
    clauses=[
        BooleanClause(query=term_1, clause="SHOULD"),
        BooleanClause(query=term_2, clause="SHOULD"),
    ]
)

term_3 = TermQuery(term="quick")
term_4 = TermQuery(term="lazy")
query_right = BooleanQuery(
    clauses=[
        BooleanClause(query=term_3, clause="SHOULD"),
        BooleanClause(query=term_4, clause="MUST_NOT"),
    ]
)

final_query = BooleanQuery(
    clauses=[
        BooleanClause(query=query_left, clause="MUST"),
        BooleanClause(query=query_right, clause="MUST"),
    ]
)

PhraseQuery - multi term query with option to set proximity distance

by default, terms in phrase is not order sensitive

the distance represents a term based edit distance, i.e. how many other terms are allowed within the boundry of the search terms

from textsearchpy.query import PhraseQuery

query = '"brown fox"'
query = PhraseQuery(terms=["brown", "fox"], distance=0)
# to enforce order sensitivity
query = PhraseQuery(terms=["brown", "fox"], distance=0, ordered=True)


query = '"jumps dog"~3'
query = PhraseQuery(terms=["jumps", "dog"], distance=3)
# to enforce order sensitivity
query = PhraseQuery(terms=["jumps", "dog"], distance=3, ordered=True)

# any two additional terms are allowed between these three terms for searching
query = '"jumps lazy dog"~2'

WildcardQuery - query with wildcard character to match Note: current implementation scans through entire index for simplicity, which can be slow

from textsearchpy.query import WildcardQuery

# allow a single wildcard character, i.e. brown brawn
query = "br?wn"
query = WildcardQuery(term="br?wn")

# allow many wildcard character to match, i.e. bun, barn, brown
query = "b*n"
query = WildcardQuery(term=term)

Benchmark

see ./benchmark for more info

Index Performance

index created with SimpleTokenizer and LowerCaseNormalizer

terms - searchable tokens after tokenization and normalization

dataset # of docs # of unique terms # of terms total index time raw data size memory use
Reuters 10788 29188 1330266 2.7s 8.84 MB ~135 MiB
Gutenberg 1000 424612 68132774 120s 396.30 MB ~3700 MiB

Query Performance

calculated as the average latency of few different preset queries

dataset TermQuery BooleanQuery PhraseQuery WildcardQuery
Reuters 0.08ms 0.11ms 0.06ms 8.46 ms
Gutenberg 0.07ms 0.10ms 55ms TODO

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

textsearchpy-1.1.1.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textsearchpy-1.1.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file textsearchpy-1.1.1.tar.gz.

File metadata

  • Download URL: textsearchpy-1.1.1.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for textsearchpy-1.1.1.tar.gz
Algorithm Hash digest
SHA256 fe4b1a4678dba2180c697e75431634296da7dda0017478554bf4adf5523cf0ad
MD5 a4cfe30bacb6d86624dbe3ec75abcd12
BLAKE2b-256 00c98a13e281dcdb467572b70943e414b8fb5e118e27e0976808a848cae38f68

See more details on using hashes here.

Provenance

The following attestation bundles were made for textsearchpy-1.1.1.tar.gz:

Publisher: python-publish.yml on KimiJL/textsearchpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file textsearchpy-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: textsearchpy-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for textsearchpy-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8398d9d22da8212ceef903bde055389e070a84e6bb41e640536a8c268cddf323
MD5 60a3c75a124c4d141f0579bd2a9e71a4
BLAKE2b-256 b45ef2744fab5e94525914bb27b3ed0c3b11788b7c69a1b6119bfbd40b372498

See more details on using hashes here.

Provenance

The following attestation bundles were made for textsearchpy-1.1.1-py3-none-any.whl:

Publisher: python-publish.yml on KimiJL/textsearchpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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