Issue:
The index pipeline JavaScript stage fails with the below JS Error :
com.lucidworks.cloud.error.FusionException: Script in stage 'xxx' doesn't end with expression.
Environment:
Fusion 5.8 and above
Cause:
JavaScript:
function doWork(doc) {
// do some work ...
return doc;
}
However, the following works fine
function (doc) {
// do some work ...
return doc;
}
However, in lower Fusion versions (< 5.8.0) both JS are working fine as expected.
Resolution:
The function syntax or the advanced syntax is better for both index and query pipelines in terms of clarity and safe usage.
Explanation:
Previously in custom JavaScript stages, we could name the main function to be used like this:
function doWork(doc) {
// do some work ...
return doc;
}
But with this commit starting from 5.8.0 https://github.com/lucidworks/fusion-indexing/commit/6c6c0a396ba2c8f5b5b1a867ae2433b7158fe8f9 there is now a check for correct syntax.
LN 117 where a new check was added with a linked stack overflow page:
It seems like in the transition to Java 11, there were some more checks to prevent loss of functionality.
Nashorn in Java 11 behaves differently from Java 8 when evaluating named functions
eval = compiledScript.eval(bindings);
if (eval == null) {
//NOTE: https://stackoverflow.com/questions/62500957/nashorn-in-java-11-behaves-differently-from-java-8-when-evaluating-named-functio
eval = new IndexStageException("Script in stage '" + stageName + "' doesn't end with expression");
}
inline = false;
Same thing for query stages JavaScript as well.
It seems like this is only if the function is declared without the full structure. The recommended structure is still working like this where we wrap the code. Then we can still have named functions.
Now named functions in general still work like this:
/* globals Java, logger*/
(function () {
"use strict";
function testingStuff(id){
return id;
}
return function main(doc , ctx, collection, solrServer, solrServerFactory) {
// do some work ...
// add your logic here
testingStuff("dslfk");
return doc;
};
})();
SYNTAX VARIANTS
JavaScript stages can be written using legacy syntax or function syntax. The key difference between these syntax variants is how the "global variables" are used. While using legacy syntax, these variables are used as global variables. With function syntax, however, these variables are passed as function parameters
LEGACY SYNTAX
//do some work...
return doc;
FUNCTION SYNTAX
function(request,response) {
request.addParam("foo", "bar");
}
ADVANCED SYNTAX
/* globals Java, logger*/
(function () {
"use strict";
return function main(doc , ctx, collection, solrServer, solrServerFactory) {
// do some work ...
return doc;
};
})();
Note: Support for legacy syntax was removed in Fusion 5.8 and above.
Comments
0 comments
Article is closed for comments.