Documentation for FilterBuilder
lexy_py.filters.FilterBuilder
Helper class for creating filters.
Attributes:
Name | Type | Description |
---|---|---|
conditions |
list[dict]
|
A list of conditions to be combined. Each condition
consists of a field, an operation, a value, and a negation option. Valid operations include the following.
|
combination |
str
|
The combination of conditions - either 'AND' or 'OR' |
Methods:
Name | Description |
---|---|
include |
Adds a condition to the filter object with a positive match |
exclude |
Adds a condition to the filter object with a negative match |
to_dict |
Returns the filter object as a dictionary |
to_json |
Returns the filter object as a JSON string |
Examples:
Restrict documents to those that contain the word 'mathematics' regardless of the case:
>>> from lexy_py import FilterBuilder
>>> builder = FilterBuilder()
>>> builder.include("content", "contains_ci", "mathematics")
>>> print(builder.to_json())
{
"conditions": [
{"field": "content", "operation": "contains_ci", "value": "mathematics", "negate": false}
],
"combination": "AND"
}
Restrict documents to those with a size less than 30,000 bytes and a file type that is not an image or video:
>>> builder = FilterBuilder()
>>> builder.include("meta.size", "less_than", 30000)
>>> builder.exclude("meta.type", "in", ["image", "video"])
>>> print(builder.to_json())
{
"conditions": [
{"field": "meta.size", "operation": "less_than", "value": 30000, "negate": false},
{"field": "meta.type", "operation": "in", "value": ["image", "video"], "negate": true}
],
"combination": "AND"
}
Restrict documents to those where URL is not None and does not start with 'https://www.youtube.com':
>>> builder = FilterBuilder()
>>> builder.exclude("meta.url", "starts_with", "https://www.youtube.com")
>>> builder.exclude("meta.url", "equals", None)
>>> builder.to_dict()
{
"conditions": [
{"field": "meta.url", "operation": "starts_with", "value": "https://www.youtube.com", "negate": True},
{"field": "meta.url", "operation": "equals", "value": None, "negate": True}
],
"combination": "AND"
}
Source code in sdk-python/lexy_py/filters.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
exclude(field, operation, value)
Adds a condition to the filter object with a negative match
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
str
|
The field to be matched |
required |
operation |
str
|
The operation to be performed on the field. Must be one of the following.
|
required |
value |
Any
|
The value to be matched against |
required |
Source code in sdk-python/lexy_py/filters.py
include(field, operation, value)
Adds a condition to the filter object with a positive match
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
str
|
The field to be matched |
required |
operation |
str
|
The operation to be performed on the field. Must be one of the following.
|
required |
value |
Any
|
The value to be matched against |
required |