
GraphQL with Elasticsearch offers a scalable API development solution by leveraging Elasticsearch's powerful search capabilities and GraphQL's flexible query language.
Elasticsearch's inverted index allows for fast and efficient querying of large datasets, making it an ideal choice for building scalable APIs.
By using GraphQL, developers can define a schema that exposes the Elasticsearch index as a set of queries and mutations, enabling clients to request specific data in a flexible and efficient manner.
This approach enables developers to build scalable APIs that can handle large amounts of traffic and data, while also providing a high degree of flexibility and customization.
Explore further: Graphql Nextjs
System Architecture
We have multiple web frontends that require different data from our Elasticsearch backend and other 3rd party REST APIs.
Our setup involves several web frontends, each needing unique data from Elasticsearch and external APIs.
We'll focus on providing a GraphQL API endpoint for easy access to Elasticsearch data, which is our primary goal.
Take a look at this: Elasticsearch Spring Data
Our web frontends rely on Elasticsearch for various data, including recipe teasers and details.
We'll specifically look at the API for recipes, which is used to display search results and recipe detail pages.
The API for recipes is just one example of how we'll utilize Elasticsearch data through a GraphQL API endpoint.
Elasticsearch
You can integrate Elasticsearch with GraphQL to create a simple end-user friendly API. This is because existing GraphQL to Elasticsearch libraries expose all possible Elasticsearch methods with its complex query language, which is not ideal for most use cases.
One of the main goals of integrating Elasticsearch with GraphQL is to determine granularly which documents and fields should be available via the API and hide irrelevant fields. This is essential for keeping the API simple and flexible for future changes and enhancements.
We can build our GraphQL API granularly, based on the needs of our API customers. By explicitly defining the GraphQL API and its schema, we are keeping the flexibility to extend and enhance.
Readers also liked: Elasticsearch Fields
Some of the benefits of using Elasticsearch with GraphQL include:
- Ability to determine granularly which documents and fields should be available via the API
- Ability to hide irrelevant fields, such as metadata used for scoring
- Flexibility to extend and enhance the API without breaking it
- Ability to include 3rd party resources and APIs in a simple way
- Ability to make the GraphQL API for the end user as simple as possible and hide complex Elasticsearch queries
There are also some amazing tools available that can help us integrate Elasticsearch with GraphQL, such as ObjectTypeComposer. This module is a plugin for graphql-compose, which derives GraphQLType from your elastic mapping and generates tons of types, providing all available methods in QueryDSL, Aggregations, Sorting with field autocompletion.
Some of the awesome resolvers provided by ObjectTypeComposer include:
- search - greatly simplified elastic search method
- searchConnection - elastic search method that implements Relay Cursor Connection spec for infinite lists
- searchPagination - elastic search method that has page and perPage arguments
- findById - get elastic record by id
- updateById - update elastic record by id
Queries
Queries are a powerful tool in GraphQL with Elasticsearch. You can query exactly what you want from your Elasticsearch index and documents.
Hasura can filter data at the source with a predicate pushdown of queries to Elasticsearch. This means it can push down a majority of queries like projection, permissions, arguments, and filtering.
By leveraging GraphQL API, you can interact with your Elasticsearch database for all your read use cases. This allows for more efficient and targeted data retrieval.
For more insights, see: Next Js Graphql
Predicate Pushdown
Predicate pushdown is a powerful feature that filters data at the source. It allows you to reduce the amount of data transferred and processed.

Hasura can push down a majority of queries, including projection, permissions, arguments, and filtering. This means you can optimize your queries and improve performance.
By filtering data at the source, you can also reduce the load on your Elasticsearch database. This is especially useful for complex queries or large datasets.
Quick Guide
Queries can be overwhelming, especially if you're new to the process. In this Quick Guide, we'll break down the essential facts to get you started.
A query is a formal request for information, typically submitted to a university or college. It's usually the first step in the application process.
Queries are often used to gather information about a program or course, and can be submitted online or by mail. In fact, some institutions may require a query before an application can be considered.
Queries typically include basic information such as the applicant's name, address, and contact details. This information helps the institution to provide a response to the query.
A unique perspective: Elasticsearch Term Query

The query process can take several weeks or even months to complete, depending on the institution's workload. It's essential to allow sufficient time for the query to be processed.
Queries can be submitted at any time, but it's best to submit them well in advance of the application deadline. This ensures that the institution has enough time to respond to the query and provide the necessary information.
Discover more: Elasticsearch Field Query
Aggregation (Coming Soon)
You'll be able to perform various aggregations to get detailed statistics about your Elasticsearch data directly within your GraphQL queries.
This means you can leverage aggregate functions in Elasticsearch like count, sum, min, max, and avg to get the insights you need.
With aggregations, you'll be able to get a deeper understanding of your data and make more informed decisions.
You can use aggregate functions like count to get the total number of documents in your index, or sum to calculate the total value of a field.
This will save you time and effort, as you won't need to write separate queries to get the information you need.
Aggregations will enable you to get detailed statistics about your data, giving you a more complete picture of your Elasticsearch data.
Hasura Performance at Scale
Hasura can get you up to 10x faster API performance on Elasticsearch with end-to-end caching.
By combining Hasura and Elasticsearch, you can optimize performance through predicate pushdown, which reduces the overhead of N+1 queries and minimizes data over-fetching.
Hasura DDN intelligently compiles and pushes filters, limits, and sorts directly to Elasticsearch, allowing for optimized performance.
By requesting only the necessary fields (_source) and limiting the number of documents fetched (size), Hasura ensures that Elasticsearch performs optimally.
This is a significant improvement over traditional, manually-coded APIs, where each new requirement demands additional hand-written queries.
Explore further: Elasticsearch Performance
API Development
API Development is where the magic happens. With GraphQL, you can define a flexible and robust API that can handle a wide range of queries and mutations.
GraphQL mutations on Elasticsearch allow for robust data manipulation operations, making it easy to insert, update, or delete data in your Elasticsearch database.
Hasura’s native mutations are a game-changer, providing a seamless integration with Elasticsearch that saves you time and effort.
API Setup
To set up a GraphQL API for Elasticsearch, you'll need to connect Hasura DDN to a locally running Elasticsearch instance using Docker.
The recommended way to experience Elasticsearch in production environments is by using Elastic Cloud, which offers a managed, scalable, and secure deployment.
Copy the .env.example file to .env and set the value for ELASTICSEARCH_PASSWORD.
Setup A API
To set up a GraphQL API for Elasticsearch, you'll need to connect Hasura DDN to a locally running Elasticsearch instance using Docker. This is a great way to get started, but keep in mind that using Elastic Cloud is the recommended way to experience Elasticsearch in production environments.
You'll start by copying the .env.example file to .env and setting the values for ELASTICSEARCH_PASSWORD. This is a crucial step that will allow your API to connect to Elasticsearch.
Installation
When setting up your API, the initial installation process is crucial. Modules like graphql, graphql-compose, and elasticsearch should be installed explicitly in your app.
These modules are listed as peerDependencies, which means they need to be installed separately. I've encountered this requirement in my own projects, and it's essential to get it right from the start.
By installing these modules explicitly, you'll ensure a smooth setup process and avoid any potential issues down the line.
Rymaruk App
The Rymaruk App is a tool for building GraphQL APIs on top of Elasticsearch. It provides a simple way to create a GraphQL schema from an Elasticsearch index.
The app uses the Elasticsearch Query DSL to fetch data from the index and the GraphQL Schema DSL to define the schema. This allows developers to easily integrate their Elasticsearch data with a GraphQL API.
Rymaruk App supports various Elasticsearch features, including aggregations, filters, and sorting. It also supports multiple data types, such as integers, strings, and dates.
To use the Rymaruk App, you need to install it using npm or yarn. Once installed, you can run the app and start building your GraphQL schema.
Consider reading: Elasticsearch Schema
In the Field
Our GraphQL API on Elasticsearch powers one of the largest websites dedicated to cooking in Switzerland, with thousands of recipes.
It delivers relevant search results and personalized teasers in the blink of an eye by leveraging both the benefits of GraphQL and Elasticsearch.
This API is used on the website Migros Rezepte, a Swiss cooking website with thousands of recipes.
Subgraph
A subgraph is a powerful tool that allows you to expose Elasticsearch as a queryable API, providing a flexible and efficient way to perform complex searches, aggregations, and filtering through GraphQL.
To set up a GraphQL subgraph, you'll need to initialize a Supergraph, which connects Hasura DDN to your Elasticsearch instance. This will provide a flexible and efficient way to perform complex searches, aggregations, and filtering through GraphQL.
You can initialize the Elasticsearch Connector in the quickstart wizard by entering the necessary environment variables, including ELASTICSEARCH_URL, ELASTICSEARCH_USERNAME, and ELASTICSEARCH_PASSWORD.
The subgraph will then allow you to introspect the Elasticsearch instance and track all indices and collections, enabling you to generate GraphQL APIs.
To start the Supergraph locally, simply follow the instructions and you'll be up and running in no time.
Featured Images: pexels.com

