Filters¶
Filters typically have corresponding elasticsearch filters. ESFluent helps
intelligently compose them and provides a common interface, via
to_query() whose responsibility is to
generate the corresponding elasticsearch filter dictionary.
A Filter can Geometry clean up data and
also generate novel queries.
Basic Filters¶
-
class
es_fluent.filters.Age(name, gte=None, lte=None, lt=None, gt=None)[source]¶ Bases:
es_fluent.filters.DictSimilar to a range filter. Operates on times. When querified, we convert the age in seconds into a datetime relative to the current
datetime.datetime.now().-
add_filter(filter_or_string, *args, **kwargs)¶ Appends a filter.
-
and_filter(filter_or_string, *args, **kwargs)¶ Adds a list of
Andclauses, automatically generatingAndfilter if it does not exist.
-
find_filter(filter_cls)¶ Find or create a filter instance of the provided
filter_cls. If it is found, use remaining arguments to augment the filter otherwise create a new instance of the desired type and add it to the currentQueryBuilderaccordingly.
-
is_empty()¶ Returns: Trueif this filter has nested clausesFalse.
-
name= 'age'¶
-
-
class
es_fluent.filters.And[source]¶ Bases:
es_fluent.filters.GenericA multi-clause filter that ands’s all sub-filters added to it.
-
add_filter(filter_or_string, *args, **kwargs)¶ Appends a filter.
-
and_filter(filter_or_string, *args, **kwargs)¶ Adds a list of
Andclauses, automatically generatingAndfilter if it does not exist.
-
find_filter(filter_cls)¶ Find or create a filter instance of the provided
filter_cls. If it is found, use remaining arguments to augment the filter otherwise create a new instance of the desired type and add it to the currentQueryBuilderaccordingly.
-
is_empty()¶ Returns: Trueif this filter has nested clausesFalse.
-
name= 'and'¶
-
-
class
es_fluent.filters.Custom(query)[source]¶ Bases:
es_fluent.filters.TerminalAllows for an entirely custom dictionary to be used and passed verbatim when to_query is invoked.
-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'custom'¶
-
-
class
es_fluent.filters.Dict[source]¶ Bases:
es_fluent.filters.GenericContains a generic dictionary of filters e.g. in a top level ES Query we may have:
{ "filtered": {"filter": {"and": {...}, "or": {...}, "exists": {...} }The Dict filter may represent the dictionary inside of “filtered.filter”.
-
add_filter(filter_or_string, *args, **kwargs)¶ Appends a filter.
-
and_filter(filter_or_string, *args, **kwargs)¶ Adds a list of
Andclauses, automatically generatingAndfilter if it does not exist.
-
find_filter(filter_cls)¶ Find or create a filter instance of the provided
filter_cls. If it is found, use remaining arguments to augment the filter otherwise create a new instance of the desired type and add it to the currentQueryBuilderaccordingly.
-
is_empty()¶ Returns: Trueif this filter has nested clausesFalse.
-
name= None¶
-
or_filter(filter_or_string, *args, **kwargs)¶ Adds a list of
Orclauses, automatically generating the anOrfilter if it does not exist.
-
to_query()[source]¶ Iterates over all filters and converts them to an Elastic HTTP API suitable query.
Note: each
Filteris free to set it’s own filter dictionary. ESFluent does not attempt to guard against filters that may clobber one another. If you wish to ensure that filters are isolated, nest them inside of a boolean filter such asAndorOr.
-
-
class
es_fluent.filters.Exists(value)[source]¶ Bases:
es_fluent.filters.TerminalChecks whether a field exists in the source document.
-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'exists'¶
-
-
class
es_fluent.filters.Filter[source]¶ Bases:
objectThe base filter.
-
name= None¶ The shorthand name of the filter.
-
-
class
es_fluent.filters.FilterRegistry[source]¶ Bases:
typeMetaclass used to automatically register new filter classes in our filter registry. Enables shorthand filter notation.
>>> from es_fluent.builder import QueryBuilder >>> query_builder = QueryBuilder() >>> query_builder.add_filter('missing', 'boop').to_query()['filter'] {'missing': {'field': 'field_name'}}
-
mro() → list¶ return a type’s method resolution order
-
-
class
es_fluent.filters.Generic[source]¶ Bases:
es_fluent.filters.FilterContains a generic list of filters. Serialized as a dictionary.
-
and_filter(filter_or_string, *args, **kwargs)[source]¶ Adds a list of
Andclauses, automatically generatingAndfilter if it does not exist.
-
find_filter(filter_cls)[source]¶ Find or create a filter instance of the provided
filter_cls. If it is found, use remaining arguments to augment the filter otherwise create a new instance of the desired type and add it to the currentQueryBuilderaccordingly.
-
name= None¶
-
or_filter(filter_or_string, *args, **kwargs)[source]¶ Adds a list of
Orclauses, automatically generating the anOrfilter if it does not exist.
-
to_query()¶ Serializes this
Filterand any descendants into a json-serializable dictionary suitable for use with the elasticsearch api.
-
-
class
es_fluent.filters.Missing(name)[source]¶ Bases:
es_fluent.filters.TerminalFilters documents that to do not contain a given field.
-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'missing'¶
-
-
class
es_fluent.filters.Not[source]¶ Bases:
es_fluent.filters.DictA filter that inverts it’s clauses.
-
add_filter(filter_or_string, *args, **kwargs)¶ Appends a filter.
-
and_filter(filter_or_string, *args, **kwargs)¶ Adds a list of
Andclauses, automatically generatingAndfilter if it does not exist.
-
find_filter(filter_cls)¶ Find or create a filter instance of the provided
filter_cls. If it is found, use remaining arguments to augment the filter otherwise create a new instance of the desired type and add it to the currentQueryBuilderaccordingly.
-
is_empty()¶ Returns: Trueif this filter has nested clausesFalse.
-
name= 'not'¶
-
-
class
es_fluent.filters.Or[source]¶ Bases:
es_fluent.filters.GenericA multi-clause filter that
orsall sub-filters added to it.-
add_filter(filter_or_string, *args, **kwargs)¶ Appends a filter.
-
and_filter(filter_or_string, *args, **kwargs)¶ Adds a list of
Andclauses, automatically generatingAndfilter if it does not exist.
-
find_filter(filter_cls)¶ Find or create a filter instance of the provided
filter_cls. If it is found, use remaining arguments to augment the filter otherwise create a new instance of the desired type and add it to the currentQueryBuilderaccordingly.
-
is_empty()¶ Returns: Trueif this filter has nested clausesFalse.
-
name= 'or'¶
-
-
class
es_fluent.filters.Range(name, gte=None, lte=None, lt=None, gt=None)[source]¶ Bases:
es_fluent.filters.DictA Filter for ranges of values, supporting
lt,lte,gt,gtecomparisons.-
add_filter(filter_or_string, *args, **kwargs)¶ Appends a filter.
-
and_filter(filter_or_string, *args, **kwargs)¶ Adds a list of
Andclauses, automatically generatingAndfilter if it does not exist.
-
find_filter(filter_cls)¶ Find or create a filter instance of the provided
filter_cls. If it is found, use remaining arguments to augment the filter otherwise create a new instance of the desired type and add it to the currentQueryBuilderaccordingly.
-
is_empty()¶ Returns: Trueif this filter has nested clausesFalse.
-
name= 'range'¶
-
-
class
es_fluent.filters.RegExp(name, expression)[source]¶ Bases:
es_fluent.filters.TerminalMatches documents whose given field matches a provided regular expression.
-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'regexp'¶
-
-
class
es_fluent.filters.Script[source]¶ Bases:
es_fluent.filters.GenericRepresents script criteria.
-
add_filter(filter_or_string, *args, **kwargs)¶ Appends a filter.
-
and_filter(filter_or_string, *args, **kwargs)¶ Adds a list of
Andclauses, automatically generatingAndfilter if it does not exist.
-
find_filter(filter_cls)¶ Find or create a filter instance of the provided
filter_cls. If it is found, use remaining arguments to augment the filter otherwise create a new instance of the desired type and add it to the currentQueryBuilderaccordingly.
-
is_empty()¶ Returns: Trueif this filter has nested clausesFalse.
-
name= 'script'¶
-
-
class
es_fluent.filters.ScriptID(name, script_id, script_params, lang='groovy')[source]¶ Bases:
es_fluent.filters.TerminalRepresents a pre-indexed script filter.
-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'script_id'¶
-
-
class
es_fluent.filters.Term(name, value)[source]¶ Bases:
es_fluent.filters.Terminal-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'term'¶
-
-
class
es_fluent.filters.Terminal[source]¶ Bases:
es_fluent.filters.FilterA filter that cannot contain nested filters. Merge behavior is to clobber existing clauses rather than appending additional clauses.
-
find_filter(filter_cls)[source]¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= None¶
-
to_query()¶ Serializes this
Filterand any descendants into a json-serializable dictionary suitable for use with the elasticsearch api.
-
-
class
es_fluent.filters.Terms(name, values)[source]¶ Bases:
es_fluent.filters.TerminalMatches documents that contain multiple exact values.
-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'terms'¶
-
Geometry Filters¶
Geometry related filters require additional dependencies. Hence they’re broken out into their own module.
-
class
es_fluent.filters.geometry.GeoJSON(name, geojson)[source]¶ Bases:
es_fluent.filters.TerminalManages querying by GeoJSON. Automatically converts incoming GeoJSON to elasticsearch friendly geometry. This generally means:
#. CW orientation of polygons. #. Re-casting of Features and FeatureCollections to Geometry and
GeometryCollections.-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'geometry'¶
-
-
class
es_fluent.filters.geometry.IndexedShape(name, shape_id, index_name, doc_type, path)[source]¶ Bases:
es_fluent.filters.TerminalSearches by a previously indexed Geometry.
-
find_filter(filter_cls)¶ As a
Terminalfilter, adding nested filters is not allowed and therefore, finding sub-filters is not supported.
-
name= 'indexed_geometry'¶
-
Utilities¶
-
class
es_fluent.filters.FilterRegistry[source] Metaclass used to automatically register new filter classes in our filter registry. Enables shorthand filter notation.
>>> from es_fluent.builder import QueryBuilder >>> query_builder = QueryBuilder() >>> query_builder.add_filter('missing', 'boop').to_query()['filter'] {'missing': {'field': 'field_name'}}