Ability to exclude results from index itself

Assume the following statement: Get all pictures that have not been deleted.

It would be great if we can implement this on the index itself, like this:

CreateIndex({
  name: 'pictures',
  source: Collection('pictures'),
  terms: [
    { field: ['data', 'productId'] },
    {
      exclude: Query(
        Lambda(
          'doc',
          ContainsPath(['data', 'deletedAt'], Var('doc'))
        )
      )
    }
  ]
});

Notice the exclude keyword, it’s a function that returns a boolean which tells if this document should be excluded from the result or not, at the moment, this is what I do:

CreateIndex({
  name: 'pictures',
  source: {
    collection: Collection('pictures'),
    fields: {
      isDeleted: Query(
        Lambda(
          'doc',
          ContainsPath(['data', 'deletedAt'], Var('doc'))
        )
      )
    }
  },
  terms: [
    { field: ['data', 'productId'] },
    { binding: 'isDeleted' }
  ]
});

Which when using this, I do:

Match(
  Index('pictures'),
  productId,
  false
);

There’s that unnecessary false.

I’m not sure that your suggestion makes a lot of sense. It specifies an exclusion as a distinct term. The shape of the term(s) in a Match call must match the terms defined in the index, so “something” would have to be specified in the Match call.

There is a solution that does not require any new index capabilities. Recall that index entries are excluded when all of an indexes terms or values evaluate to null. Knowing that, you can do this:

CreateIndex({
  name: 'pictures',
  source: {
    collection: Collection('pictures'),
    fields: {
      prodID: Query(
        Lambda(
          'doc',
          If(
            ContainsPath(['data', 'deletedAt'], Var('doc')),
            null,
            Select(["data", "productID"], Var("doc"))
          )
        )
      )
    }
  },
  terms: [ { binding: "prodID" }]
})

And then:

> Get(Match(Index("pictures"), 123))
{
  ref: Ref(Collection("pictures"), "312443123430064640"),
  ts: 1634227832180000,
  data: { productID: 123, title: 'bear' }
}
> Get(Match(Index("pictures"), 1))
Error: instance not found
{
  errors: [
    {
      position: [],
      code: 'instance not found',
      description: 'Set not found.'
    }
  ]
}
> Update(Ref(Collection("pictures"), "312443156634272256"), { data: { deletedAt: null }})
{
  ref: Ref(Collection("pictures"), "312443156634272256"),
  ts: 1634228899860000,
  data: { productID: 1, title: 'dodo' }
}
> Get(Match(Index("pictures"), 1))
{
  ref: Ref(Collection("pictures"), "312443156634272256"),
  ts: 1634228899860000,
  data: { productID: 1, title: 'dodo' }
}

Basically I want an index that automatically excludes deleted documents using the presence data.deletedAt.

Nothing in Fauna is “automatic”. Valid indexes behave the way that you define them.

Did the example I provided not work for you?

Nothing in Fauna is “automatic”. Valid indexes behave the way that you define them.

That’s what I was suggesting, I was suggesting a way for indexes to have implicit behavior when you create them, like excluding certain data based on a filter function.

Thanks for the clarification.

Can you add a bit more detail about how you’d want to opt-in/out of such behavior? For example, if we implemented an implicit behavior that changed how your existing queries worked, what would you want to see done about that?