| 1 |
from typing import List |
| 2 |
|
| 3 |
from valkey import DataError |
| 4 |
|
| 5 |
|
| 6 |
class Field: |
| 7 |
NUMERIC = "NUMERIC" |
| 8 |
TEXT = "TEXT" |
| 9 |
WEIGHT = "WEIGHT" |
| 10 |
GEO = "GEO" |
| 11 |
TAG = "TAG" |
| 12 |
VECTOR = "VECTOR" |
| 13 |
SORTABLE = "SORTABLE" |
| 14 |
NOINDEX = "NOINDEX" |
| 15 |
AS = "AS" |
| 16 |
GEOSHAPE = "GEOSHAPE" |
| 17 |
|
| 18 |
def __init__( |
| 19 |
self, |
| 20 |
name: str, |
| 21 |
args: List[str] = None, |
| 22 |
sortable: bool = False, |
| 23 |
no_index: bool = False, |
| 24 |
as_name: str = None, |
| 25 |
): |
| 26 |
if args is None: |
| 27 |
args = [] |
| 28 |
self.name = name |
| 29 |
self.args = args |
| 30 |
self.args_suffix = list() |
| 31 |
self.as_name = as_name |
| 32 |
|
| 33 |
if sortable: |
| 34 |
self.args_suffix.append(Field.SORTABLE) |
| 35 |
if no_index: |
| 36 |
self.args_suffix.append(Field.NOINDEX) |
| 37 |
|
| 38 |
if no_index and not sortable: |
| 39 |
raise ValueError("Non-Sortable non-Indexable fields are ignored") |
| 40 |
|
| 41 |
def append_arg(self, value): |
| 42 |
self.args.append(value) |
| 43 |
|
| 44 |
def valkey_args(self): |
| 45 |
args = [self.name] |
| 46 |
if self.as_name: |
| 47 |
args += [self.AS, self.as_name] |
| 48 |
args += self.args |
| 49 |
args += self.args_suffix |
| 50 |
return args |
| 51 |
|
| 52 |
|
| 53 |
class TextField(Field): |
| 54 |
""" |
| 55 |
TextField is used to define a text field in a schema definition |
| 56 |
""" |
| 57 |
|
| 58 |
NOSTEM = "NOSTEM" |
| 59 |
PHONETIC = "PHONETIC" |
| 60 |
|
| 61 |
def __init__( |
| 62 |
self, |
| 63 |
name: str, |
| 64 |
weight: float = 1.0, |
| 65 |
no_stem: bool = False, |
| 66 |
phonetic_matcher: str = None, |
| 67 |
withsuffixtrie: bool = False, |
| 68 |
**kwargs, |
| 69 |
): |
| 70 |
Field.__init__(self, name, args=[Field.TEXT, Field.WEIGHT, weight], **kwargs) |
| 71 |
|
| 72 |
if no_stem: |
| 73 |
Field.append_arg(self, self.NOSTEM) |
| 74 |
if phonetic_matcher and phonetic_matcher in [ |
| 75 |
"dm:en", |
| 76 |
"dm:fr", |
| 77 |
"dm:pt", |
| 78 |
"dm:es", |
| 79 |
]: |
| 80 |
Field.append_arg(self, self.PHONETIC) |
| 81 |
Field.append_arg(self, phonetic_matcher) |
| 82 |
if withsuffixtrie: |
| 83 |
Field.append_arg(self, "WITHSUFFIXTRIE") |
| 84 |
|
| 85 |
|
| 86 |
class NumericField(Field): |
| 87 |
""" |
| 88 |
NumericField is used to define a numeric field in a schema definition |
| 89 |
""" |
| 90 |
|
| 91 |
def __init__(self, name: str, **kwargs): |
| 92 |
Field.__init__(self, name, args=[Field.NUMERIC], **kwargs) |
| 93 |
|
| 94 |
|
| 95 |
class GeoShapeField(Field): |
| 96 |
""" |
| 97 |
GeoShapeField is used to enable within/contain indexing/searching |
| 98 |
""" |
| 99 |
|
| 100 |
SPHERICAL = "SPHERICAL" |
| 101 |
FLAT = "FLAT" |
| 102 |
|
| 103 |
def __init__(self, name: str, coord_system=None, **kwargs): |
| 104 |
args = [Field.GEOSHAPE] |
| 105 |
if coord_system: |
| 106 |
args.append(coord_system) |
| 107 |
Field.__init__(self, name, args=args, **kwargs) |
| 108 |
|
| 109 |
|
| 110 |
class GeoField(Field): |
| 111 |
""" |
| 112 |
GeoField is used to define a geo-indexing field in a schema definition |
| 113 |
""" |
| 114 |
|
| 115 |
def __init__(self, name: str, **kwargs): |
| 116 |
Field.__init__(self, name, args=[Field.GEO], **kwargs) |
| 117 |
|
| 118 |
|
| 119 |
class TagField(Field): |
| 120 |
""" |
| 121 |
TagField is a tag-indexing field with simpler compression and tokenization. |
| 122 |
See https://valkey.io/topics/search/#tag-index |
| 123 |
""" |
| 124 |
|
| 125 |
SEPARATOR = "SEPARATOR" |
| 126 |
CASESENSITIVE = "CASESENSITIVE" |
| 127 |
|
| 128 |
def __init__( |
| 129 |
self, |
| 130 |
name: str, |
| 131 |
separator: str = ",", |
| 132 |
case_sensitive: bool = False, |
| 133 |
withsuffixtrie: bool = False, |
| 134 |
**kwargs, |
| 135 |
): |
| 136 |
args = [Field.TAG, self.SEPARATOR, separator] |
| 137 |
if case_sensitive: |
| 138 |
args.append(self.CASESENSITIVE) |
| 139 |
if withsuffixtrie: |
| 140 |
args.append("WITHSUFFIXTRIE") |
| 141 |
|
| 142 |
Field.__init__(self, name, args=args, **kwargs) |
| 143 |
|
| 144 |
|
| 145 |
class VectorField(Field): |
| 146 |
""" |
| 147 |
Allows vector similarity queries against the value in this attribute. |
| 148 |
See https://valkey.io/topics/search/ |
| 149 |
""" |
| 150 |
|
| 151 |
def __init__(self, name: str, algorithm: str, attributes: dict, **kwargs): |
| 152 |
""" |
| 153 |
Create Vector Field. Notice that Vector cannot have sortable or no_index tag, |
| 154 |
although it's also a Field. |
| 155 |
|
| 156 |
``name`` is the name of the field. |
| 157 |
|
| 158 |
``algorithm`` can be "FLAT" or "HNSW". |
| 159 |
|
| 160 |
``attributes`` each algorithm can have specific attributes. Some of them |
| 161 |
are mandatory and some of them are optional. See |
| 162 |
https://valkey.io/topics/search/ |
| 163 |
for more information. |
| 164 |
""" |
| 165 |
sort = kwargs.get("sortable", False) |
| 166 |
noindex = kwargs.get("no_index", False) |
| 167 |
|
| 168 |
if sort or noindex: |
| 169 |
raise DataError("Cannot set 'sortable' or 'no_index' in Vector fields.") |
| 170 |
|
| 171 |
if algorithm.upper() not in ["FLAT", "HNSW"]: |
| 172 |
raise DataError( |
| 173 |
"Realtime vector indexing supporting 2 Indexing Methods:" |
| 174 |
"'FLAT' and 'HNSW'." |
| 175 |
) |
| 176 |
|
| 177 |
attr_li = [] |
| 178 |
|
| 179 |
for key, value in attributes.items(): |
| 180 |
attr_li.extend([key, value]) |
| 181 |
|
| 182 |
Field.__init__( |
| 183 |
self, name, args=[Field.VECTOR, algorithm, len(attr_li), *attr_li], **kwargs |
| 184 |
) |