| 1 |
from enum import Enum |
| 2 |
|
| 3 |
|
| 4 |
class QueryPresence(Enum): |
| 5 |
"""Defines possible behaviours for the term's presence in a document.""" |
| 6 |
|
| 7 |
OPTIONAL = 1 # default |
| 8 |
REQUIRED = 2 |
| 9 |
PROHIBITED = 3 # documents that contain this term will not be returned |
| 10 |
|
| 11 |
|
| 12 |
class Query: |
| 13 |
"""A `lunr.Query` provides a programmatic way of defining queries to be |
| 14 |
performed against a `lunr.Index`. |
| 15 |
|
| 16 |
Prefer constructing a `lunr.Query` using `the lunr.Index.query` method |
| 17 |
so the query object is pre-initialized with the right index fields. |
| 18 |
""" |
| 19 |
|
| 20 |
# Constants for indicating what kind of automatic wildcard insertion will |
| 21 |
# be used when constructing a query clause. |
| 22 |
# This allows wildcards to be added to the beginning and end of a term |
| 23 |
# without having to manually do any string concatenation. |
| 24 |
# The wildcard constants can be bitwise combined to select both leading and |
| 25 |
# trailing wildcards. |
| 26 |
WILDCARD = "*" |
| 27 |
WILDCARD_NONE = 0 |
| 28 |
WILDCARD_LEADING = 1 |
| 29 |
WILDCARD_TRAILING = 2 |
| 30 |
|
| 31 |
def __init__(self, all_fields): |
| 32 |
self.clauses = [] |
| 33 |
self.all_fields = all_fields |
| 34 |
|
| 35 |
def __repr__(self): |
| 36 |
return '<Query fields="{}" clauses="{}">'.format( |
| 37 |
",".join(self.all_fields), ",".join(c.term for c in self.clauses) |
| 38 |
) |
| 39 |
|
| 40 |
def clause(self, *args, **kwargs): |
| 41 |
"""Adds a `lunr.Clause` to this query. |
| 42 |
|
| 43 |
Unless the clause contains the fields to be matched all fields will be |
| 44 |
matched. In addition a default boost of 1 is applied to the clause. |
| 45 |
|
| 46 |
If the first argument is a `lunr.Clause` it will be mutated and added, |
| 47 |
otherwise args and kwargs will be used in the constructor. |
| 48 |
|
| 49 |
Returns: |
| 50 |
lunr.Query: The Query itself. |
| 51 |
""" |
| 52 |
if args and isinstance(args[0], Clause): |
| 53 |
clause = args[0] |
| 54 |
else: |
| 55 |
clause = Clause(*args, **kwargs) |
| 56 |
|
| 57 |
if not clause.fields: |
| 58 |
clause.fields = self.all_fields |
| 59 |
|
| 60 |
if (clause.wildcard & Query.WILDCARD_LEADING) and ( |
| 61 |
clause.term[0] != Query.WILDCARD |
| 62 |
): |
| 63 |
clause.term = Query.WILDCARD + clause.term |
| 64 |
|
| 65 |
if (clause.wildcard & Query.WILDCARD_TRAILING) and ( |
| 66 |
clause.term[-1] != Query.WILDCARD |
| 67 |
): |
| 68 |
clause.term = clause.term + Query.WILDCARD |
| 69 |
|
| 70 |
self.clauses.append(clause) |
| 71 |
return self |
| 72 |
|
| 73 |
def term(self, term, **kwargs): |
| 74 |
"""Adds a term to the current query, creating a Clause and adds it to |
| 75 |
the list of clauses making up this Query. |
| 76 |
|
| 77 |
The term is not tokenized and used "as is". Any conversion to token |
| 78 |
or token-like strings should be performed before calling this method. |
| 79 |
|
| 80 |
For example: |
| 81 |
query.term(lunr.Tokenizer("foo bar")) |
| 82 |
|
| 83 |
Args: |
| 84 |
term (Token or iterable): Token or iterable of tokens to add. |
| 85 |
kwargs (dict): Additional properties to add to the Clause. |
| 86 |
""" |
| 87 |
if isinstance(term, (list, tuple)): |
| 88 |
for t in term: |
| 89 |
self.term(t, **kwargs) |
| 90 |
else: |
| 91 |
self.clause(str(term), **kwargs) |
| 92 |
|
| 93 |
return self |
| 94 |
|
| 95 |
def is_negated(self): |
| 96 |
"""A negated query is one in which every clause has a presence of |
| 97 |
prohibited. These queries require some special processing to return |
| 98 |
the expected results. |
| 99 |
""" |
| 100 |
return all( |
| 101 |
clause.presence == QueryPresence.PROHIBITED for clause in self.clauses |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
class Clause: |
| 106 |
"""A single clause in a `lunr.Query` contains a term and details on |
| 107 |
how to match that term against a `lunr.Index` |
| 108 |
|
| 109 |
Args: |
| 110 |
term (str, optional): The term for the clause. |
| 111 |
field (iterable, optional): The fields for the term to be searched |
| 112 |
against. |
| 113 |
edit_distance (int, optional): The character distance to use, defaults |
| 114 |
to 0. |
| 115 |
use_pipeline (bool, optional): Whether the clause should be pre |
| 116 |
processed by the index's pipeline, default to True. |
| 117 |
boost (int, optional): Boost to apply to the clause, defaults to 1. |
| 118 |
wildcard (Query.WILDCARD_*, optional): Any of the Query.WILDCARD |
| 119 |
constants defining if a wildcard is to be used and how, defaults |
| 120 |
to Query.WILDCARD_NONE. |
| 121 |
presence (QueryPresence, optional): Behaviour for a terms presence |
| 122 |
in a document. |
| 123 |
""" |
| 124 |
|
| 125 |
def __init__( |
| 126 |
self, |
| 127 |
term=None, |
| 128 |
fields=None, |
| 129 |
edit_distance=0, |
| 130 |
use_pipeline=True, |
| 131 |
boost=1, |
| 132 |
wildcard=Query.WILDCARD_NONE, |
| 133 |
presence=QueryPresence.OPTIONAL, |
| 134 |
): |
| 135 |
super().__init__() |
| 136 |
self.term = term |
| 137 |
self.fields = fields or [] |
| 138 |
self.edit_distance = edit_distance |
| 139 |
self.use_pipeline = use_pipeline |
| 140 |
self.boost = boost |
| 141 |
self.wildcard = wildcard |
| 142 |
self.presence = presence |
| 143 |
|
| 144 |
def __repr__(self): |
| 145 |
return '<Clause term="{}">'.format(self.term) |