Index binding Issues - reverse results

Hi Fauna Community,

I am having issues with index bindings returning null values where a value should be found through the reference path. I believe the issue stems from my attempt to reverse the order of binding results (i.e. "reverse": True )

Using the python driver, I create indexes relevant to individual location_hash results corresponding the following command:
client.query(
q.create_index({
“name”: index_name,
“source”: {
“collection”: q.collection(“Recipe”),
“fields”: {
“sort_result”: q.query(
q.lambda_(
“doc”,
q.select([“data”, “algorithm_result” ,
location_hash],
q.var(“doc”))
)
)
}
},
“values”:
[
{ “binding”: “sort_result” ,
“reverse”: True },
{ “field”: [“ref”] }
]
}
)
)

With `location_hash`corresponding to a string like `"irpbgytrlp"` I get the following result:
    {
  data: [
    [null, Ref(Collection("Recipe"), "287118064268345869")],
    [null, Ref(Collection("Recipe"), "287118064279880205")],
    [null, Ref(Collection("Recipe"), "287118064310288909")],
    [null, Ref(Collection("Recipe"), "287118064339649037")],
    [null, Ref(Collection("Recipe"), "287118064361669133")],
    [null, Ref(Collection("Recipe"), "287118064393126413")],
    [null, Ref(Collection("Recipe"), "287118064424583693")],
    [null, Ref(Collection("Recipe"), "287118064448700941")],
    [null, Ref(Collection("Recipe"), "287118064484352525")],
    [null, Ref(Collection("Recipe"), "287118064496935437")],
    [null, Ref(Collection("Recipe"), "287118064509518349")],
    [null, Ref(Collection("Recipe"), "287118064531538445")],
    [null, Ref(Collection("Recipe"), "287118064556704269")],
...]
}

Now if I were to perform a simple get/select on the first result of the above index to simulate the sort_result field I get the following indicating to me that the path is correct:

Select(["data", "algorithm_result" , "irpbgytrlp"], Get(Ref(Collection("Recipe"), "287118064268345869")))

0.377

>> Time elapsed: 178ms

I can’t understand why this binding won’t work as intended. Can anyone point out an issue with my index construction or is this a known issue with bindings?

Attempting to construct the same index without reversing sort_result values as below works as i would have expected:
client.query(
q.create_index({
“name”: index_name,
“source”: {
“collection”: q.collection(“Recipe”),
“fields”: {
“sort_result”: q.query(
q.lambda_(
“doc”,
q.select([“data”, “algorithm_result” ,
location_hash],
q.var(“doc”))
)
)
}
},
“values”:
[
{ “binding”: “sort_result” },
{ “field”: [“ref”] }
]
}
)
)

This index using the same location_hash provides the following result as expected:
{
data: [
[0.0529, Ref(Collection(“Recipe”), “287769033861759501”)],
[0.0607, Ref(Collection(“Recipe”), “287768961888551437”)],
[0.0659, Ref(Collection(“Recipe”), “287768990310203916”)],
[0.074, Ref(Collection(“Recipe”), “289612639276892685”)],
[0.0761, Ref(Collection(“Recipe”), “287769013402993164”)],
[0.0842, Ref(Collection(“Recipe”), “289612641747337741”)],
[0.0849, Ref(Collection(“Recipe”), “287769013018165772”)],
[0.0879, Ref(Collection(“Recipe”), “289614288444719629”)],
[0.089, Ref(Collection(“Recipe”), “289599237629411848”)],
[0.0897, Ref(Collection(“Recipe”), “287768990946689548”)],
[0.0899, Ref(Collection(“Recipe”), “289612672624755213”)],
[0.0906, Ref(Collection(“Recipe”), “289613888261980685”)],
[0.0921, Ref(Collection(“Recipe”), “289599357313876488”)],
[0.093, Ref(Collection(“Recipe”), “287769013318058508”)],
[0.098, Ref(Collection(“Recipe”), “287769023339299341”)],
[0.099, Ref(Collection(“Recipe”), “287769059566551560”)],
[0.0998, Ref(Collection(“Recipe”), “287118081464992269”)],
[0.0998, Ref(Collection(“Recipe”), “287769219663135245”)],
…]
}

Have I attempted to reverse the result incorrectly or is reversing the resulting order simply not possible across a binding value?

Please also accept my apologies for poor formatting, I can’t seem to find the block preformatting button on the new topic GUI.

@Darryl_Naidu Quick suggestion, can you try to use the Reverse Function instead of the reverse Index? I have not yet looked into your solution for any issues.

Why does location_hash change from a string to a float? Is it possible that location_hash is mixed values, and can sometimes be null? See the sort precedence description in the documentation, which might directly explain your results: Data types | Fauna Documentation

@Darryl_Naidu I just tried Index binding with the reverse option, and I see no issues.
Nulls are in the beginning for reverse.

Here are my sample query and data.

Paginate(Match(Index("all_orders_binding_values_shippeddate_rev")))

{
  after: [
    Date("1998-04-20"),
    Ref(Collection("orders"), "285555555990247936"),
    Ref(Collection("orders"), "285555555990247936")
  ],
  data: [
    [null, Ref(Collection("orders"), "285555555969288704")],
    [null, Ref(Collection("orders"), "285555555970346496")],
    [null, Ref(Collection("orders"), "285555555971428864")],
    [null, Ref(Collection("orders"), "285555555971430912")],
    [null, Ref(Collection("orders"), "285555555989212672")],
    [null, Ref(Collection("orders"), "285555555990243840")],
    [null, Ref(Collection("orders"), "285555555990260224")],
    [null, Ref(Collection("orders"), "285555555991290368")],
    [null, Ref(Collection("orders"), "285555555991292416")],
    [null, Ref(Collection("orders"), "285555555991293440")],
    [null, Ref(Collection("orders"), "285555555991295488")],
    [null, Ref(Collection("orders"), "285555555991297536")],
    [null, Ref(Collection("orders"), "285555555991299584")],
    [null, Ref(Collection("orders"), "285555555991301632")],
    [null, Ref(Collection("orders"), "285555555991302656")],
    [null, Ref(Collection("orders"), "285555555991303680")],
    [null, Ref(Collection("orders"), "285555555991305728")],
    [null, Ref(Collection("orders"), "285555555991306752")],
    [null, Ref(Collection("orders"), "285555555991307776")],
    [null, Ref(Collection("orders"), "285555555991308800")],
    [null, Ref(Collection("orders"), "285555555991310848")],
    [Date("1998-05-06"), Ref(Collection("orders"), "285555555969311232")],
    [Date("1998-05-06"), Ref(Collection("orders"), "285555555971429888")],
    [Date("1998-05-06"), Ref(Collection("orders"), "285555555991309824")],
    [Date("1998-05-05"), Ref(Collection("orders"), "285555555971431936")],
    [Date("1998-05-05"), Ref(Collection("orders"), "285555555991291392")],
    [Date("1998-05-04"), Ref(Collection("orders"), "285555555980813824")],
    [Date("1998-05-04"), Ref(Collection("orders"), "285555555990244864")],
    [Date("1998-05-04"), Ref(Collection("orders"), "285555555991296512")],
    [Date("1998-05-04"), Ref(Collection("orders"), "285555555991300608")],
    [Date("1998-05-04"), Ref(Collection("orders"), "285555555991304704")],
    [Date("1998-05-01"), Ref(Collection("orders"), "285555555970348544")],
    [Date("1998-05-01"), Ref(Collection("orders"), "285555555970352640")],
    [Date("1998-05-01"), Ref(Collection("orders"), "285555555980805632")],
    [Date("1998-05-01"), Ref(Collection("orders"), "285555555990261248")],
    [Date("1998-05-01"), Ref(Collection("orders"), "285555555990263296")],
    [Date("1998-05-01"), Ref(Collection("orders"), "285555555991294464")],
    [Date("1998-04-30"), Ref(Collection("orders"), "285555555971433984")],
    [Date("1998-04-30"), Ref(Collection("orders"), "285555555990262272")],
    [Date("1998-04-29"), Ref(Collection("orders"), "285555555970351616")],
    [Date("1998-04-29"), Ref(Collection("orders"), "285555555991298560")],
    [Date("1998-04-28"), Ref(Collection("orders"), "285555555990252032")],
    [Date("1998-04-28"), Ref(Collection("orders"), "285555555990259200")],
    [Date("1998-04-27"), Ref(Collection("orders"), "285555555971438080")],
    [Date("1998-04-27"), Ref(Collection("orders"), "285555555990248960")],
    [Date("1998-04-27"), Ref(Collection("orders"), "285555555990251008")],
    [Date("1998-04-27"), Ref(Collection("orders"), "285555555990257152")],
    [Date("1998-04-24"), Ref(Collection("orders"), "285555555970359808")],
    [Date("1998-04-24"), Ref(Collection("orders"), "285555555981870592")],
    [Date("1998-04-24"), Ref(Collection("orders"), "285555555981871616")],
    [Date("1998-04-24"), Ref(Collection("orders"), "285555555990245888")],
    [Date("1998-04-24"), Ref(Collection("orders"), "285555555990254080")],
    [Date("1998-04-24"), Ref(Collection("orders"), "285555555990256128")],
    [Date("1998-04-23"), Ref(Collection("orders"), "285555555988161024")],
    [Date("1998-04-23"), Ref(Collection("orders"), "285555555990253056")],
    [Date("1998-04-23"), Ref(Collection("orders"), "285555555990255104")],
    [Date("1998-04-22"), Ref(Collection("orders"), "285555555981872640")],
    [Date("1998-04-22"), Ref(Collection("orders"), "285555555990258176")],
    [Date("1998-04-21"), Ref(Collection("orders"), "285555555988167168")],
    [Date("1998-04-21"), Ref(Collection("orders"), "285555555989214720")],
    [Date("1998-04-21"), Ref(Collection("orders"), "285555555990246912")],
    [Date("1998-04-20"), Ref(Collection("orders"), "285555555989207552")],
    [Date("1998-04-20"), Ref(Collection("orders"), "285555555989215744")],
    [Date("1998-04-20"), Ref(Collection("orders"), "285555555990242816")]
  ]
}

Index definition

CreateIndex(
{
name: 'all_orders_binding_values_shippeddate_rev',
source: [{
  collection: Collection('orders'),
  fields: {
    shippedDate: Query(Lambda('orderDoc', ToDate(Select(['data', 'shippedDate'], Var('orderDoc'), null))))
  }
}],
values:[{binding: 'shippedDate', reverse: true}, {field: ['ref']} ]
})

Hi @ewan,
location_hash is a string in all cases and is never null. Certainty that the indexes are all created correctly can be seen in the name of each, location_hash is captured in each relevant index.

The example results that you included in this conversation say otherwise. In one example, you search for the location_hash “irpbgytrlp”, and the results show floating point values. You might want to investigate what data you actually have in your database.

Thanks ewan, just to close out: location_hash, a String, corresponds to the key in the Object/Dictionary titled algorithm_result . The value returned in this case is a float which is expected.

By “close out”, do you mean that your initial question has been resolved? If so, that’s great!