UDFs in index bindings / array generation for search over index

Is there a way to use udfs in index bindings? I tried defining an identity function as Query(Lambda("x", Var("x"))) and use it in an index binding but it fails with “Field binding must be a pure unary function.”

I went down this path because I can’t seem to find any built in functions to generate an array of ints from 0 to n, is there any way to do this with built in functions? I was able to define a recursive function that does this but it seems I can’t use it in an index binding. I’m trying to do this in order to implement trigram search (tokenize into 3 character chunks and search for exact matches). Is there another way to do this? Or any other ways of implementing some kind of text search.

Update: found out about the NGram function, is there a page with all built in functions? I don’t see it listed in the built in function section of the API reference documentation.

https://docs.fauna.com/fauna/current/api/fql/cheat_sheet is the best there is. Our docs are quite well up to date. Ngram is not in there because it is too difficult to use in its current state.

This is indeed a limitation and would be a nice to have. It would be ideal to have. Feel free to open a feature request on the forum for people to vote on if there isn’t one yet. Currently the workarounds:

  • hardcode an array [1,2,3,4,5,…,n] and map over that which is quite static of course and doesn’t work well in bindings. Ideally we would have a [1…n] syntax to Map over n elements
  • recursive functions, which doesn’t work in bindings as you correctly noticed.
  • generate a big query for multiple cases with the host language. You could relatively easily generate a function that handles the first N cases since FQL is quite composable.

I think using the NGram function is your best bet.
There is an extensive stackoverflow answer here that might help you: https://stackoverflow.com/questions/62109035/how-to-get-documents-that-contain-sub-string-in-faunadb

1 Like