2

My data is stored in elasticsearch and it is very big. So I want to use graphql to query it properly. I am using django graphene package to query it. For database models it is working fine. My json schema of elasticsearch

https://pastebin.com/EQBnnCBU below is my Type definition and query code

https://pastebin.com/fsr9V1Rf

Problem is that I am not able to understand how to write the query schema for the elastic json schema.

need only initial help or any explanation that can help me

I have checked the answer here django-graphene without model But is of not help

My current ElasticType schema

class ElasticType(graphene.ObjectType):
    id = graphene.ID()
    index = graphene.String()
    found = graphene.String()
    properties = graphene.String()

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I also have need to search in the nested data. like I have one key user then inside its addresses. so can i fetch addresses ?
@pawan: That feature is not yet available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.