Creating and index which filters documents where a field array has a size

Hi,
I have a Collection that has documents where one field is an array. Now I want to create an index containing the documents where the number of elements in this array is larger than zero.

This is how I’m trying to create the index:

CreateIndex({
  name: "eventsWithResults",
  source: {
    collection: Collection("events"),
    fields: {
      has_results: Query(
        Lambda(
          "document",
          GT(Count(Select(["data", "results"], Var("document"), [])), 0)
        )
      )
    }
  },
  terms: [{ binding: "has_results" }],
  values: [
    { field: ["data", "time"], reverse: true },
    { field: ["data", "results", "score"], reverse: true },
    { field: ["ref"] }
  ]
})

However, I’m getting the error message that the binding is not a “pure unary function”.

A document with results (i.e. one that should be part of the index) looks like this:

{
    "ref": Ref(Collection("events"), "340186818667348563"),
    "ts": 1660686283635000,
    "data": {
        "time": "2022-07-24T01:05:19Z",
        "eventnumber": "14485",
        "results": [
            {
                "status": "Pending",
                "score": 0.901
            },
            {
                "status": "Final",
                "score": 1.03
            },
        ]
    }
}

And a document without results (and thus should be excluded from the index) will look like this:

{
    "ref": Ref(Collection("events"), "340186818538373714"),
    "ts": 1660686282052000,
    "data": {
        "time": "2022-07-24T01:05:19Z",
        "eventnumber": "14485",
        "results": []
    }
}

Bindings must not create side effects, such as additional reads or writes. Count is a function that can cause additional reads if passed a set. The logic determining whether side effects might occur only looks at the functions in the binding, not their parameters. In this case, you’re getting the error even though you’re merely trying to count array entries.

As a workaround, you could try storing a resultCount field that you update whenever the results array is modified. Creating a binding that checks the resultCount field should then be easy.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.