graphene_elastic.filter_backends.filtering package

Submodules

graphene_elastic.filter_backends.filtering.common module

class graphene_elastic.filter_backends.filtering.common.FilteringFilterBackend(connection_field, args=None)[source]

Bases: BaseBackend, FilteringFilterMixin

Filtering filter backend.

field_belongs_to(field_name)[source]

Check if given filter field belongs to the backend.

Parameters:

field_name

Returns:

filter(queryset)[source]

Filter.

property filter_args_mapping
property filter_fields

Filtering filter fields.

get_backend_query_fields(items, is_filterable_func, get_type_func)[source]

Fail proof override.

Parameters:
  • items

  • is_filterable_func

  • get_type_func

Returns:

get_field_lookup_param(field_name)[source]

Get field lookup param.

Parameters:

field_name

Returns:

get_field_options(field_name, filter_fields=None)[source]

默认从Node中配置中读取,如果没有则从document中读取

可能的字段名: 1. author 2. comments.author 3. comments.author.name

get_field_type(field_name, field_value, base_field_type)[source]

Get field type.

Returns:

get_filter_query_params()[source]

Get query params to be filtered on.

We can either specify it like this:

query_params = {
‘category’: {

‘value’: ‘Elastic’,

}

}

Or using specific lookup:

query_params = {
‘category’: {

‘term’: ‘Elastic’, ‘range’: {

‘lower’: Decimal(‘3.0’)

}

}

}

Note, that value would only work on simple types (string, integer, decimal). For complex types you would have to use complex param anyway. Therefore, it should be forbidden to set default_lookup to a complex field type.

Sample values:

query_params = {
‘category’: {

‘value’: ‘Elastic’,

}, ‘comments’: {

‘author’: {
‘name’: {

‘value’: ‘Elastic’

}

}

}

}

filter_fields = {
‘category’: {

‘field’: ‘category.raw’, ‘type’: ‘normal’, ‘default_lookup’: ‘term’, ‘lookups’: (

‘term’, ‘terms’, ‘range’, ‘exists’, ‘prefix’, ‘wildcard’, ‘contains’, ‘in’, ‘gt’, ‘gte’, ‘lt’, ‘lte’, ‘starts_with’, ‘ends_with’, ‘is_null’, ‘exclude’

)

}, ‘comments’: {

‘field’: ‘comments’, ‘type’: ‘nested’, ‘properties’: {

‘author’: {

‘type’: ‘object’, ‘path’: ‘comments.author’, ‘properties’: {

‘name’: {

‘type’: ‘normal’, ‘field’: ‘author.name’, ‘default_lookups’: ‘term’, ‘lookups’: (

)

}

}

}

}

}

}

field_name = ‘category’ {

‘inventory_type’: {‘value’: ‘1’}, ‘spu’: {

‘supplier_entityms_entity_id’: {

‘contains’: ‘Elastic’

}, ‘brand’: {

‘code’: {

‘term’: ‘Elastic’

}

}

}

}

get_nested_field_type(field_name, field_value, base_field_type, field_options)[source]
has_query_fields = True
prefix = 'filter'
prepare_filter_fields()[source]

Prepare filter fields

Assume that we have a document like this.

```python class Comment(InnerDoc):

author = Text(fields={‘raw’: Keyword()}) content = Text(analyzer=’snowball’) created_at = Date()

def age(self):

return datetime.now() - self.created_at

class Post(Document):

title = Text() title_suggest = Completion() created_at = Date() published = Boolean() category = Text(

analyzer=html_strip, fields={‘raw’: Keyword()}

)

comments = Nested(Comment)

```

Possible structures:

filter_fields = {
‘title’: {

‘type’: ‘normal|object|nested’, ‘field’: ‘title’ # custom field name ‘lookups’: [

… # custom lookup list

], ‘default_lookup’: … # custom default lookup

}, ‘created_at’: {

‘type’: ‘normal’, ‘field’: ‘created_at’, ‘lookups’: [

LOOKUP_FILTER_RANGE # only range

]

}, ‘published’: LOOKUP_FILTER_TERM # treated as default lookup …

}

We shall finally have:

filter_fields = {
‘title’: {

‘type’: ‘normal’, ‘field’: ‘title.raw’, ‘lookups’: [

], ‘default_lookup’: …

}, … # any else fields indexed of this document ‘comments’: {

‘type’: ‘nested’, ‘properties’: {

‘author’: {

‘type’: ‘normal’, ‘field’: ‘comments.author’, …

}, ‘content’: {

‘type’: ‘normal’, ‘field’: ‘comments.content’, …

}

}

}

}

prepare_query_params()[source]

Prepare query params.

Returns:

graphene_elastic.filter_backends.filtering.mixins module

class graphene_elastic.filter_backends.filtering.mixins.FilteringFilterMixin[source]

Bases: object

Filtering filter mixin.

apply_filter: Callable
classmethod apply_filter_prefix(queryset, options, value)[source]

Apply prefix filter.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{category:{prefix:”Pyth”}}) {
edges {
node {

category title content numViews comments

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_filter_range(queryset, options, value)[source]

Apply range filter.

Syntax:

TODO

Example:

{
allPostDocuments(filter:{numViews:{range:{

lower:{decimal:”100”}, upper:{decimal:”200”}

}}}) {

edges {
node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_filter_term(queryset, options, value)[source]

Apply term filter.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{category:{term:”Python”}}) {
edges {
node {

category title content numViews comments

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_filter_terms(queryset, options, value)[source]

Apply terms filter.

Syntax:

TODO

Note, that number of values is not limited.

Example:

query {
allPostDocuments(filter:{category:{

terms:[“Python”, “Django”]

}}) {

edges {
node {

category title content numViews comments

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (mixed: either str or iterable (list, tuple).) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

apply_query: Callable
classmethod apply_query_contains(queryset, options, value)[source]

Apply contains filter.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{category:{contains:”tho”}}) {
edges {
node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_endswith(queryset, options, value)[source]

Apply endswith filter.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{category:{endsWith:”thon”}}) {
edges {
node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_exclude(queryset, options, value)[source]

Apply exclude functional query.

Syntax:

TODO

Note, that number of values is not limited.

Example:

query {
allPostDocuments(filter:{category:{exclude:”Python”}}) {
edges {
node {

category title content numViews

}

}

}

}

Or exclude multiple terms at once:

query {
allPostDocuments(filter:{category:{exclude:[“Ruby”, “Java”]}}) {
edges {
node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_exists(queryset, options, value)[source]

Apply exists filter.

Syntax:

TODO

Example:

{
allPostDocuments(filter:{category:{exists:true}}) {
edges {
node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_gt(queryset, options, value)[source]

Apply gt functional query.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{numViews:{

gt:{decimal:”100”}

}}) { edges {

node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_gte(queryset, options, value)[source]

Apply gte functional query.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{numViews:{

gte:{decimal:”100”}

}}) { edges {

node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_in(queryset, options, value)[source]

Apply in functional query.

Syntax:

TODO

Note, that number of values is not limited.

Example:

query {
allPostDocuments(postFilter:{tags:{

in:[“photography”, “models”]

}}) { edges {

node {

category title content numViews tags

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_isnull(queryset, options, value)[source]

Apply isnull functional query.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{category:{isNull:true}}) {
edges {
node {

category title content numViews comments

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_lt(queryset, options, value)[source]

Apply lt functional query.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{numViews:{

lt:{decimal:”200”}

}}) { edges {

node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_lte(queryset, options, value)[source]

Apply lte functional query.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{numViews:{

lte:{decimal:”200”}

}}) { edges {

node {

category title content numViews

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod apply_query_wildcard(queryset, options, value)[source]

Apply wildcard filter.

Syntax:

TODO

Example:

query {
allPostDocuments(filter:{category:{wildcard:”ytho”}}) {
edges {
node {

category title content numViews comments

}

}

}

}

Parameters:
  • queryset (elasticsearch_dsl.search.Search) – Original queryset.

  • options (dict) – Filter options.

  • value (str) – value to filter on.

Returns:

Modified queryset.

Return type:

elasticsearch_dsl.search.Search

classmethod get_gte_lte_params(value, lookup, options)[source]

Get params for gte, gt, lte and lt query.

Syntax:

TODO

Example:

{
allPostDocuments(filter:{numViews:{

gt:{decimal:”100”}, lt:{decimal:”200”}

}}) { edges {

node {

category title content numViews

}

}

}

}

Parameters:
  • value (graphene_elastic.filter_backends.filtering.queries.InputObjectType) –

  • lookup (str) –

  • options (dict) –

Returns:

Params to be used in range query.

Return type:

dict

classmethod get_range_param_value(value)[source]

Get range param value.

Parameters:

value (graphene_elastic.filter_backends.filtering.queries.InputObjectType) –

Returns:

classmethod get_range_params(value, options)[source]

Get params for range query.

Syntax:

TODO

Example:

{
allPostDocuments(filter:{numViews:{range:{

lower:{decimal:”100”}, upper:{decimal: “200”}, boost:”2.0”

}}}) {

edges {
node {

category title content numViews

}

}

}

}

Parameters:
  • value (graphene_elastic.filter_backends.filtering.queries.InputObjectType) –

  • options (dict) –

Returns:

Params to be used in range query.

Return type:

dict

split_lookup_complex_value: Callable

graphene_elastic.filter_backends.filtering.queries module

Module contents