I have a function that returns an array of distinct string values though want to figure out how to drop a null value which is inherently included within the returned array. Can anyone help rewrite the UDF below to assist?
There is a few things going on here, so it will be hard to know how to answer. Can you share the index definition, so we can see the shape of results? Please also provide an example document that would match the index.
Some initial considerations:
You’ll want to also port the index over to v10. Seeing the v4 definition we can see how to do that.
Union here looks like you just need that to flatten results. You can use flatmap or flatten if necessary in v10 for that.
you can drop a value from an array by using .where(x => x != null). But it’s not clear at all what the data looks like to start. Maybe there is something we can do with the index, or leverage a computed field, to simplify things.
What is the allAllergens v4 index definition? It looks like you are only checking if the first value defined in the v4 index is a string. Are you not concerned about the second value? And is it the same order as the values defined in the v10 definition? Just making sure
You might try something like this as a v10 equivalent:
Allergens.allAllergens()
.fold([], (acc, val) => acc.concat(val.allergens).concat(val.potential_allergens))
.where(a => a isa String)
.distinct()
Thanks @ptpaterson! This works exactly as intended. Compute ops significantly reduced by re-writing the UDF as: Allergens.allAllergens() .fold([], (acc, val) => acc.concat(val.allergens).concat(val.potential_allergens)) .distinct() .where(a => a isa String)