Wondering how to get Elasticsearch and Firebase’s Firestore to play nice?

Joseph Schultz
Sep 15, 2018 · 5 min read


Roadmap


Setting up Elasticsearch

# Get Elasticsearch running.
$ git clone https://github.com/deviantony/docker-elk
$ cd docker-elk
$ docker-compose up -d

Setting up Firebase

// Root Collection: Users
{
"AWGSLQhnug3dSHq0nECo": {
"firstName": "John",
"lastName": "Doe",
"location": { // <-- This is how a GEOPOINT is stored
"_latitude": 40.7128
"_longitude": -74.0060
}
},
"BASQWERhnug3dS126d1": {
"firstName": "John",
"lastName": "Doe",
"location": {
"_latitude": 37.7749,
"_longitude": -122.4194
}
}
}

Communicating between them using Elasticstore

$ git clone https://github.com/acupajoe/elasticstore
$ cd elasticstore
$ yarn install
$ cp .env.sample .env
# Firebase 
FB_URL={{YOUR FIRESTORE URL}}
FB_ES_COLLECTION="search"
FB_REQ="request"
FB_RES="response"
FB_SERVICE_PATH="service-account.json"
ES_HOST=localhost
ES_PORT=9200
[
....
{
collection: "users",
index: "users",
type: "users",
mappings: {
location: {
// Elasticsearch's 'geo_point' needs to be specified
type: "geo_point"
}
},
// Transform data as we receive it from firestore
transform: (data, parent) => ({
...data,
location:`${doc.location._latitude},${doc.location._longitude}`
})
},
....
]
npm run start

Running Searches leveraging Firestore (Client Side)

const result = await firebase.firestore().collection('search').add({
request: {
index: 'users',
type: 'users',
q: 'John' // Shorthand query syntax
},
response: null
})
result.ref.onSnapshot(doc => {
if (doc.response !== null) {
// Do things
}
})
const result = await firebase.firestore().collection('search').add({
request: {
index: 'users',
type: 'users',
body: {
query: {
match: {
"_all": "John"
}
}
},
response: null
})
result.ref.onSnapshot(doc => {
if (doc.response !== null) {
// Do things
}
})

Caveats


Conclusion

Joseph Schultz

Written by

The key to good development isn’t creating something that works, it’s understanding why.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade