Comments (12)
Ah, one more modification then! You'll have to await
the calls in FirestoreHandler.ts
So anywhere there is:
body = this.reference.transform.call(/*...*/)
it should be changed to:
body = await this.reference.transform.call(/* .... */)
I think that oughtta do it
from elasticstore.
Hey @butaminas - I haven't tried it myself, but you should be able to do this via the transform
property.
Something like:
{
// ...
transform: async (data, parent) => {
var user = await parent.ref.firestore.collection("users").doc(data.user_id).get()
if (user.exists) {
return { ...data, ...user.data() }
} else {
return { ...data }
}
}
// ...
}
Be aware that this will require a db lookup for every comment that's inserted.
Anyway, that should work? At least, it should give you an idea of where to go with it.
from elasticstore.
@acupofjose this does make sense and I think it should work.
However, parent
is undefined in my case. Not really sure why, just started with this library.
from elasticstore.
Ah whoops, parent
will only be set from a subcollection query. You'll have to import firestore.
Something like:
import firebase from "firebase/app"
import "firebase/firestore"
{
// ...
transform: async (data, parent) => {
var user = await firestore.collection("users").doc(data.user_id).get()
if (user.exists) {
return { ...data, ...user.data() }
} else {
return { ...data }
}
}
// ...
}
from elasticstore.
@acupofjose This almost works. Only had to import firebase-admin
like this import * as admin from "firebase-admin"
and then access firestore like this admin.firestore()
.
However, whenever I try to run transform
with async I always get empty hits no matter what.
Even if I use transform like this:
transform: async (data, parent) => {
return { ...data }
}
from elasticstore.
@acupofjose that was exactly it! Thanks!
from elasticstore.
@acupofjose I just realized that my index won't be updated whenever user data is updated since I include user data via transform.
Is there any way I could link this data together so that the index would stay in sync even when the data from linked collection is updated?
from elasticstore.
Yeah just add another item into references
for your user collection, make sure it points to your existing index, and be sure that you transform the data to match the structure in elasticsearch
from elasticstore.
@acupofjose how do I point the user_id
field of comments
index to users
index?
from elasticstore.
@butaminas I don't know what you're asking - could you clarify? Thanks!
from elasticstore.
@acupofjose I understand now how to transform the data when it is being indexed.
What I don't fully understand is how do I do the linking so that transformed data would be updated whenever users table is updated.
From your previous comment I understand that this is how I should link users
to comments
:
{
collection: "comments",
index: "comments",
include: ["text", "user_id"],
transform: async (data, parent) => {
// Transformation from Firestore happens here
}
}
{
collection: "users",
index: "comments",
include: ["name"]
}
But it is not linking to the transformed data in comments
index.
What I would like to happen is when the users
collection is updated so that the data in comments
-> user_id
index would also be updated.
from elasticstore.
Sorry for the delay on this!
It seems like a better idea would be to use the onItemUpserted
property in references and the add an update_by_query from elasticsearch. (You'll have to pull the latest commit on the repo to have access to that)
Something resembling (I'm not sure what your actual structure is):
{
collection: "comments",
index: "comments",
include: ["text", "user_id"],
transform: async (data, parent) => {
// Transformation from Firestore happens here
},
onItemUpserted: async (data, parent, esClient) => {
await esClient.updateByQuery({
index: 'comments',
refresh: true,
body: {
script: {
lang: 'painless',
source: 'ctx._source["user_name"] = data.user.name
},
query: {
match: {
user_id: data.user_id
}
}
}
})
}
}
from elasticstore.
Related Issues (20)
- Error in `FS_ADDED` handler [doc@h3XhjEJQJ4EZ6KXLSgIE]: illegal_argument_exception HOT 4
- Add failed then all update failed as well HOT 3
- How do you monitor multiple collections? HOT 1
- Mapping Date and excluding fields inside objects. HOT 2
- this.client.indices.exists returns a response object not a boolean HOT 2
- Mapping include_type_name should be removed at this point HOT 3
- [feature request] Add and allow option mapping dynamic mapping variable
- [feature request] parent-join, would need routing, parent/child, joins HOT 3
- Poor error handling when pushing data to same index, will throw already exists error. HOT 1
- What is onItemUpserted? HOT 1
- Update does not work HOT 1
- The datastore operation timed out, or the data was temporarily unavailable HOT 14
- index can't be a function
- Clusters HOT 3
- TypeError: Cannot read property 'request' of undefined at FirestoreCollectionHandler.existsApi HOT 11
- TypeError: Cannot read property 'onSnapshot' of undefined HOT 14
- Add @nareddyt to contributors
- How to get the search text in builder? HOT 1
- How to use completion suggester HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elasticstore.