How to filter an index where a string startsWith() a data property, and return all those docs

Hello Fauna folks. My name is Chris and I’m struggling.

I’m trying to retrieve some documents from an index where the string I’m providing startsWith a data property value. I know that’s kind of awkward but it is what it is am I right.

Here’s the index I created.

CreateIndex({
  name: "test_persist",
  source: Collection("contents"),
  terms: [
    {
      field: ["data", "url"]
    }
  ]
})

So, I figured Filter might be the right approach with the Lamda’ing and all.

Filter(
  Index('test_persist'),
  Lambda(
    'x',
    StartsWith('/test/more', Select(['data', 'url'], Get(Var('x')))),
  ),
)

And right now, you’re chuckling because it looks like I’m just making up stuff, and you are correct, laugh away.

Turns out when you read the docs, filter requires Array, Page or Set as the first argument sooo of course that isn’t going to work. But how do I change the Index into an Array, Set or Page, or reference those within an Index?

This is me pulling the car over and asking for directions. My ex would be so proud.

But how do I change the Index into an Array, Set or Page, or reference those within an Index?

I believe you are looking for Match.

When calling Match through Paginate , the results are returned as an array of Pages. If no matching element is found an empty collection is returned.

But first, have a look at the Stack Overflow answer for How to get documents that contain sub-string in FaunaDB. It walks you through why you may not want to iterate over your entire collection with StartsWith and instead create an index that allows for a more efficient solution.

2 Likes

Thanks, I think one of those approaches is going to set me straight! Never came across that page during my many searches.

1 Like