Pierre Drouillet
Pierre Drouillet

Reputation: 23

How to query a range of values with Fauna and FQL?

Im' having problem with FaunaDB. I'm new to it, went through docs, but no luck. I'll jump imto showing you the code right now but SO complains that my question has too much code in it.

I'm trying to do the following SQL statement with FQL:

SELECT * FROM order
  WHERE cid = '1234'
  AND fulfilled = false
  AND rank >= 0
  AND rank <= 100 

I've tried the following:

q.Paginate(
  q.Intersection(
    q.Match(
      q.Index('order_by_cid'),
      '1234',
    ),
    q.Match(
      q.Index('order_by_status'),
      false,
    ),
    q.Range(
      q.Match('order_by_rank'),
      [0],
      [100]
    ),
  )
)

But this returns { data: [] }

My indexes:

{
  ref: Index("order_by_cid"),
  ts: 1602095185756000,
  active: true,
  serialized: true,
  name: "order_by_cid",
  source: Collection("order"),
  terms: [{ field: ["data", "cid"] }],
  partitions: 1
}

{
  ref: Index("order_by_status"),
  ts: 1602163027885000,
  active: true,
  serialized: true,
  name: "order_by_status",
  source: Collection("order"),
  terms: [{ field: ["data", "fulfilled"] }],
  partitions: 1
}

{
  ref: Index("order_by_rank"),
  ts: 1602611790710000,
  active: true,
  serialized: true,
  name: "order_by_rank",
  source: Collection("order"),
  values: [{ field: ["data", "rank"] }, { field: "ref" }],
  partitions: 8
}

Upvotes: 2

Views: 404

Answers (1)

Luigi Servini
Luigi Servini

Reputation: 176

The index should be:

CreateIndex(
  {
    name:'refByCidFulfilled',
    source:Collection("order"),
    terms:[{field:['data','cid']},{field:['data','fulfilled']}],
    values:[{field:['data','rank']},{field:['ref']}]
  }
)

And you can query

Map(
  Paginate(
    Range(
      Match('refByCidFulfilled',[1234,false]),[1],[100])),
      Lambda(['rank','ref'],Get(Var('ref')
    )
  )
)

Upvotes: 1

Related Questions