I am integrating ElasticSearch to Parse Server for caching and searching capabilities.
I’ll cache data of few specific classes into the ElasticSearch. When a find query is executed on one of those classes then a check is first made to see if the data is cached on the Elastic server. If it is then return it. If not, get it from the parse database.
I want to complete this process by using the beforeFind trigger of a class to check in the ElasticSearch.
For example in Vendor class,
// main.js
const es = require("./elasticsearch.js");
Parse.Cloud.beforeFind(Parse.Vendor, async (request) => {
// Call Elastic function
es.esSearch(request, (res) => {
// if response has data then stops the find query return the data
// If not, continue to find query
});
});
// elasticsearch.js
exports.esSearch = async function (req) {
// returns data
});
So, How to return data from the beforeFind trigger without throwing error and also stop executing of find query?
may I ask how did you implement the ElasticSearch in to your cloud function?
I am learning with Parse Server on Back4App and my guess is I would need to ask them to install some adapter or package to access the functionality GitHub/elasticsearch-js? Or did you simply wrote your own function for HTTP requests?
Error loading your cloud code:
TypeError: elastic is not a constructor
at Object. (/usr/src/app/data/cloud/functions/elastic.js:2:18)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object. (/usr/src/app/data/cloud/triggers/profile.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1153:10)
I also uploaded file package.json to the cloud folder where main.js is sitting with following:
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace',
apiVersion: '7.2', // use the same version of your Elasticsearch instance
});
It seems that missing ; was a problem and then trying to figure out what is going on I changed Client to elastic what is then not defined in the dependency. This is working now: