You could give graphene-elastic a try.
As examples state, you will need to declare which fields do you want to filter on and it will automatically make them available for filtering using specified lookup filters.
Some code parts (not related to your project, but simply to indicate how it works, copied from the project documentation).
Sample document definition
import datetime
from elasticsearch_dsl import (
Boolean,
Date,
Document,
Keyword,
Nested,
Text,
Integer,
)
class Post(Document):
title = Text(
fields={'raw': Keyword()}
)
content = Text()
created_at = Date()
published = Boolean()
category = Text(
fields={'raw': Keyword()}
)
tags = Text(
analyzer='snowball',
fields={'raw': Keyword(multi=True)},
multi=True
)
num_views = Integer()
class Index:
name = 'blog_post'
settings = {
'number_of_shards': 1,
'number_of_replicas': 1,
'blocks': {'read_only_allow_delete': None},
}
def add_tag(self, name):
self.tags.append(name)
def save(self, ** kwargs):
self.created_at = datetime.datetime.now()
return super().save(** kwargs)
Sample schema definition
import graphene
from graphene_elastic import (
ElasticsearchObjectType,
ElasticsearchConnectionField,
)
from graphene_elastic.filter_backends import (
FilteringFilterBackend,
SearchFilterBackend,
)
from graphene_elastic.constants import (
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_TERM,
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_EXCLUDE,
LOOKUP_QUERY_IN,
)
# Object type definition
class Post(ElasticsearchObjectType):
class Meta(object):
document = PostDocument
interfaces = (Node,)
filter_backends = [
FilteringFilterBackend,
SearchFilterBackend,
]
filter_fields = {
'title': {
'field': 'title.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,
},
'category': 'category.raw',
'tags': 'tags.raw',
'num_views': 'num_views',
}
search_fields = {
'title': {'boost': 4},
'content': {'boost': 2},
'category': None,
}
# Query definition
class Query(graphene.ObjectType):
all_post_documents = ElasticsearchConnectionField(Post)
# Schema definition
schema = graphene.Schema(query=Query)
Sample queries
query PostsQuery {
allPostDocuments(filter:{
category:{terms:["Elastic", "Python"]}
}) {
edges {
node {
id
title
category
content
createdAt
comments
}
}
}
}
Or:
{
allPostDocuments(filter:{
category:{term:"Python"},
numViews:{gt:"700"}
}) {
edges {
node {
category
title
comments
numViews
}
}
}
}
Study documentation for more.