Skip to content

Commit

Permalink
post filter backend
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Sep 28, 2019
1 parent e641fe6 commit f6bd994
Show file tree
Hide file tree
Showing 26 changed files with 1,957 additions and 849 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -15,6 +15,12 @@ are used for versioning (schema follows below):
0.3.4 to 0.4).
- All backwards incompatible changes are mentioned in this document.

0.5
---
2019-09-28 (not yet released)

- PostFilter backend.

0.4
---
2019-09-23
Expand Down
29 changes: 29 additions & 0 deletions examples/schema/animal.py
Expand Up @@ -6,6 +6,7 @@
)
from graphene_elastic.filter_backends import (
FilteringFilterBackend,
PostFilterFilteringBackend,
SearchFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
Expand Down Expand Up @@ -38,10 +39,13 @@ class Meta:
interfaces = (Node,)
filter_backends = [
FilteringFilterBackend,
PostFilterFilteringBackend,
SearchFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
]

# For filter backend
filter_fields = {
'id': {
'field': 'id',
Expand All @@ -60,22 +64,47 @@ class Meta:
'default_lookup': LOOKUP_FILTER_TERM,
},
}

# For search backend
search_fields = {
'action': None,
'entity': None,
}

# For ordering backend
ordering_fields = {
'id': 'id',
'publish_date': 'publish_date',
'action': 'action.raw',
'entity': 'entity.raw',
}

# For default ordering backend
ordering_defaults = (
'id',
'publish_date'
)

# For filter backend
post_filter_fields = {
'id': {
'field': 'id',
'default_lookup': LOOKUP_FILTER_TERM,
},
'action': {
'field': 'action.raw',
'default_lookup': LOOKUP_FILTER_TERM,
},
'entity': {
'field': 'entity.raw',
'default_lookup': LOOKUP_FILTER_TERM,
},
'app': {
'field': 'app.raw',
'default_lookup': LOOKUP_FILTER_TERM,
},
}


class Query(graphene.ObjectType):
"""Animal query."""
Expand Down
58 changes: 56 additions & 2 deletions examples/schema/post/object_type.py
Expand Up @@ -6,6 +6,7 @@
FilteringFilterBackend,
SearchFilterBackend,
OrderingFilterBackend,
PostFilterFilteringBackend,
DefaultOrderingFilterBackend,
HighlightFilterBackend,
SourceFilterBackend,
Expand All @@ -20,7 +21,7 @@
)

from search_index.documents import Post as PostDocument
from ..custom_backends import CustomFilterBackend
# from ..custom_backends import CustomFilterBackend

__all__ = (
'Post',
Expand All @@ -35,6 +36,7 @@ class Meta:
interfaces = (Node,)
filter_backends = [
FilteringFilterBackend,
PostFilterFilteringBackend,
SearchFilterBackend,
HighlightFilterBackend,
SourceFilterBackend,
Expand All @@ -45,7 +47,7 @@ class Meta:
]
# For `FilteringFilterBackend` backend
filter_fields = {
'id': '_id',
# 'id': '_id',
# The dictionary key (in this case `title`) is the name of
# the corresponding GraphQL query argument. The dictionary
# value could be simple or complex structure (in this case
Expand Down Expand Up @@ -180,3 +182,55 @@ class Meta:
}
},
}

# For `PostFilterFilteringBackend` backend
post_filter_fields = {
# 'pf_id': '_id',
# The dictionary key (in this case `title`) is the name of
# the corresponding GraphQL query argument. The dictionary
# value could be simple or complex structure (in this case
# complex). The `field` key points to the `title.raw`, which
# is the field name in the Elasticsearch document
# (`PostDocument`). Since `lookups` key is provided, number
# of lookups is limited to the given set, while term is the
# default lookup (as specified in `default_lookup`).
'title': {
'field': 'title.raw', # Field name in the Elastic doc
# Available lookups
'lookups': [
LOOKUP_FILTER_TERM,
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
# Default lookup
'default_lookup': LOOKUP_FILTER_TERM,
},

# The dictionary key (in this case `category`) is the name of
# the corresponding GraphQL query argument. Since no lookups
# or default_lookup is provided, defaults are used (all lookups
# available, term is the default lookup). The dictionary value
# (in this case `category.raw`) is the field name in the
# Elasticsearch document (`PostDocument`).
'category': 'category.raw',

# The dictionary key (in this case `tags`) is the name of
# the corresponding GraphQL query argument. Since no lookups
# or default_lookup is provided, defaults are used (all lookups
# available, term is the default lookup). The dictionary value
# (in this case `tags.raw`) is the field name in the
# Elasticsearch document (`PostDocument`).
'tags': 'tags.raw',

# The dictionary key (in this case `num_views`) is the name of
# the corresponding GraphQL query argument. Since no lookups
# or default_lookup is provided, defaults are used (all lookups
# available, term is the default lookup). The dictionary value
# (in this case `num_views`) is the field name in the
# Elasticsearch document (`PostDocument`).
'num_views': 'num_views',
'i_do_not_exist': 'i_do_not_exist',
}
2 changes: 1 addition & 1 deletion examples/schema/read_only_animal.py
Expand Up @@ -37,7 +37,7 @@ class Meta:
document = ReadOnlyAnimalDocument
interfaces = (Node,)
filter_backends = [
FilteringFilterBackend,
# FilteringFilterBackend,
SearchFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
Expand Down
33 changes: 32 additions & 1 deletion examples/schema/user.py
Expand Up @@ -6,6 +6,7 @@
)
from graphene_elastic.filter_backends import (
FilteringFilterBackend,
PostFilterFilteringBackend,
SearchFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
Expand Down Expand Up @@ -36,7 +37,8 @@ class Meta:
document = UserDocument
interfaces = (Node,)
filter_backends = [
FilteringFilterBackend,
# FilteringFilterBackend,
# PostFilterFilteringBackend,
SearchFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
Expand Down Expand Up @@ -87,6 +89,35 @@ class Meta:
ordering_defaults = (
'created_at',
)
post_filter_fields = {
'pf_first_name': {
'field': 'first_name.raw',
'lookups': [
LOOKUP_FILTER_TERM,
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
'default_lookup': LOOKUP_FILTER_TERM,
},
'pf_last_name': {
'field': 'last_name.raw',
'lookups': [
LOOKUP_FILTER_TERM,
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
'default_lookup': LOOKUP_FILTER_TERM,
},
'pf_email': 'email.raw',
'pf_created_at': 'created_at',
'pf_is_active': 'is_active',
}


class Query(graphene.ObjectType):
Expand Down
2 changes: 1 addition & 1 deletion src/graphene_elastic/arrayconnection.py
Expand Up @@ -109,7 +109,7 @@ def connection_from_list_slice(
max(start_offset - slice_start, 0):
list_slice_length - (slice_end - end_offset)
]
logger.debug(_slice_qs.to_dict())
logger.debug_json(_slice_qs.to_dict())

_slice = _slice_qs.execute()

Expand Down
34 changes: 17 additions & 17 deletions src/graphene_elastic/fields.py
Expand Up @@ -25,12 +25,12 @@
convert_elasticsearch_field,
ElasticsearchConversionError,
)
from .filter_backends import (
SearchFilterBackend,
FilteringFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
)
# from .filter_backends import (
# SearchFilterBackend,
# FilteringFilterBackend,
# OrderingFilterBackend,
# DefaultOrderingFilterBackend,
# )
from .logging import logger
from .registry import get_global_registry
from .settings import graphene_settings
Expand Down Expand Up @@ -123,10 +123,10 @@ def args(self):
@property
def default_filter_backends(self):
return [
SearchFilterBackend,
FilteringFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
# SearchFilterBackend,
# FilteringFilterBackend,
# OrderingFilterBackend,
# DefaultOrderingFilterBackend,
]

@property
Expand Down Expand Up @@ -185,19 +185,19 @@ def get_type(v):
for backend_cls in self.filter_backends:
if backend_cls.has_query_fields:
backend = backend_cls(self)
params.update(
backend.get_backend_query_fields(
items=items,
is_filterable_func=is_filterable,
get_type_func=get_type,
)
_query_fields = backend.get_backend_query_fields(
items=items,
is_filterable_func=is_filterable,
get_type_func=get_type,
)
if _query_fields:
params.update(_query_fields)

return params

@property
def field_args(self):
return self._field_args(self.fields.items())
return self._field_args(list(self.fields.items()))

@property
def reference_args(self):
Expand Down
3 changes: 2 additions & 1 deletion src/graphene_elastic/filter_backends/__init__.py
@@ -1,9 +1,10 @@
from .faceted_search import *
from .post_filter import *
from .filtering import *
from .highlight import *
from .ordering import *
from .search import *
from .source import *
from .ordering import *

__title__ = 'graphene_elastic.filter_backends'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
Expand Down
Expand Up @@ -77,11 +77,12 @@ def alter_connection(self, connection, slice):
@property
def faceted_search_fields(self):
"""Faceted search filter fields."""
return getattr(
search_fields = getattr(
self.connection_field.type._meta.node._meta,
'filter_backend_options',
{}
).get('faceted_search_fields', {})
return copy.deepcopy(search_fields)

def field_belongs_to(self, field_name):
"""Check if given filter field belongs to the backend.
Expand Down

0 comments on commit f6bd994

Please sign in to comment.