If you have ever used Firebase Database for sorting and manipulating data, you know that Firebase API is relatively limited for search queries. There are only few methods of sorting:
orderByChild(), orderByKey(), orderByValue()
and filtering data:
limitToFirst(), limitToLast(), startAt(), endAt(), equalTo()
It may be enough for small projects, but when it came to large e-commerce applications we quickly realized that Firebase Database API would definitely not be sufficient.
In case of an online store, for example, we wanted to group some products and find the most expensive of them, or make pagination or find products from some inner category. It cannot be done without kludges using standard Firebase Database API.
We started searching for solutions on how we could improve search queries and found an amazing program called
Firebase Flashlight by the Firebase team.
It's an adapter library for ElasticSearch.
With this script you work with Firebase Realtime Database using ElasticSearch Query DSL.
It allows you to construct powerful queries with aggregations, wildcards (like "produc?-*") and regexps.
You don’t have to use ElasticSearch for all queries. You can use plain Firebase Realtime Database API for most of them and ElasticSearch for more complicated use cases.
First of all, let’s talk about the architecture of Firebase Flashlight and how it changes the way you use Firebase Realtime Database.
Flashlight under the hood
Firebase Flashlight is a script that connects ElasticSearch with Firebase Realtime Database.
On the startup it indexes Firebase Database.
Then Flashlight waits for incoming requests in bucket search/request.
When the request comes it immediately processes it and returns a data response into bucket search/response.
Any updates in Firebase Database are tracked by Flashlight in real time.
You may start thinking that storing all queries in Firebase overfills our database with unnecessary information.
Firebase Flashlight takes care of it by cleaning processed queries every minute or time set in config.
To use Flashlight you must have a locally running script (for development) or it running on some server.
The Heroku server with Bonsai service is a good choice.
But in production we run Flashlight in Docker container because of its scalability and performance.
All configuration of Flashlight is stored as the config.example.js file.
You only need to configure your service-account file and routing and you are ready to use Firebase Flashlight in you project:
// Your Firebase instance where we will listen and write search results
exports.FB_URL = process.env.FB_URL || 'https://<YOUR APP>.firebaseio.com';
// ...
// See https://firebase.google.com/docs/server/setup for instructions
// to auto-generate the service-account.json file
exports.FB_SERVICEACCOUNT = process.env.FB_ACC || 'service-account.json';Also you must set paths that will be indexed by Flashlight.
For example, you don't want to index payment information, orders and other secret information.
To do this, add in configuration into the array paths your buckets without paths you don't want to index:
exports.paths = [
{
path : "users",
index: "firebase",
type : "user"
},
... other paths
];On the client you can use this method to request data from Firebase Realtime Database:
/**
* Gets data hits from firebase using [flashlight]{@link https://github.com/firebase/flashlight} lib for ElasticSearch
*
* @param {string} index ElasticSearch index
* @param {string} type ElasticSearch type
* @param {Object} queryObj query object for ElasticSearch
*
* @returns {Observable} Observable of requested data hits
*/
requestData(index, type, queryObj){
let key = firebase.database().ref().child('search/request').push({
index: index,
type: type,
body: queryObj
}).key;
return Observable.create( observer => {
firebase.database().ref().child('search/response/' + key + '/hits/hits').on('value', data => {
if(data.val()){
observer.next(data);
}
});
});
}If you are new to ElasticSearch Query DSL,
check this quick tutorial.
And now we’d like to tell you about some use cases when ElasticSearch helped us.
ElasticSearch with Firebase were used in our angular-commerce components.
Pagination
There are different ways to make pagination for web applications.
Using ElasticSearch Query DSL you can make it with only one query.
For example, we want to show a paginated product list in our online store.
To show products from a specific page pass current page number (page) and number of output products (size).
The query will look like this:
{
size: size,
from: page * size
}To request data call requestData method:
requestData(esIndex, 'product', queryObject);Products from a specific category
Let’s assume products are divided into categories. How do you find products from a specific category? It’s very easy:
{
size: size,
from: page * size,
query: {
bool: {
{
match: {
"category" : categoryId
}
}
}
}
}Filtering products by attributes
Let’s expand our products more. Products have attributes. What do we do in case we want to filter products using these attributes? For example, filtering products by red color and medium size will look like this:
{
size: size,
from: page * size,
query: {
bool: {
must: [
{
bool: {
should: [
{
match: {
"attributes.color" : "red"
}
},
{
match: {
"attributes.size" : "medium"
}
}
]
}
},
{
match: {
"category" : categoryId
}
}
]
}
}
}Aggregations of values
It’s very popular to filter products by their price using this type of slider:

But how to obtain maximum and minimum price of products?
Aggregation queries will help you to solve this problem:
{
aggs : {
max_price : { max : { field : "price" } },
min_price : { min : { field : "price" } }
},
query: {
bool: {
should: {
match: {
category: categoryId
}
}
}
}
}This query will return maximum and minimum price in a specific category.
Filtering products by price
Finally, let’s filter our products by price. The final query will look like this:
{
size: size,
from: page * size,
query: {
bool: {
must: [
{
bool: {
should: [
{
match: {
"attributes.color" : "red"
}
},
{
match: {
"attributes.size" : "medium"
}
}
]
}
},
{
match: {
"category" : categoryId
}
},
{
"range" : {
"price" : {
"gte" : min_price,
"lte" : max_price
}
}
}
]
}
}
}Conclusion
So, ElasticSearch Query DSL improves searching abilities of Firebase Realtime Database.
It's a good idea to combine ElasticSearch with Firebase Database, for example, at e-commerce projects.
It provides you more freedom in constructing queries and search performance.
Please feel free to reach out to us if you have any problems with configuring Firebase Flashlight.
Was gibt’s Neues?
Zum Blog ↓Welcome to Nodeartio
August 2026
LesenHow CloudFlare determines TOR visitors?
August 2021
LesenWir stellen ein
Lust auf frische Ideen, berufliches Wachstum und das Erkunden neuer Möglichkeiten.
Klingt nach Ihnen?
Entdecken Sie Karrierechancen bei NodeArt.