List of terms

Problem

You want to search an index using a list of terms but Match only finds exact matches.

Solution

Map over the list of terms and then Union the results, which you can then Paginate over.

Copied!
client.query(
  q.(
    q.(
      q.(
        q.(
          ['air', 'water'],
          q.(
            'element',
            q.(q.('spells_by_element'), q.('element'))
          )
        )
      )
    ),
    q.('ref', q.(q.('ref')))
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  data: [
    {
      ref: (("spells"), "181388642046968320"),
      ts: 1627085181220000,
      data: {
        name: 'Fire Beak',
        element: [ 'air', 'fire' ],
        spellbook: (("spellbooks"), "181388642139243008")
      }
    },
    {
      ref: (("spells"), "181388642071085568"),
      ts: 1627085181220000,
      data: {
        name: "Water Dragon's Claw",
        element: [ 'water', 'fire' ],
        spellbook: (("spellbooks"), "181388642139243008")
      }
    },
    {
      ref: (("spells"), "181388642088911360"),
      ts: 1627085181220000,
      data: { name: "Hippo's Wallow", element: [ 'water', 'earth' ] }
    },
    {
      ref: (("spells"), "181388642581742080"),
      ts: 1627085181220000,
      data: { name: "Mountain's Thunder", element: 'air', cost: 15 }
    }
  ]
}
Query metrics:
  •    bytesIn:   220

  •   bytesOut: 1,125

  • computeOps:     1

  •    readOps:     6

  •   writeOps:     0

  •  readBytes:   643

  • writeBytes:     0

  •  queryTime:  13ms

  •    retries:     0

Discussion

Match returns a Set Reference, so when we Map over it, the result is an Array of set references. In order to Paginate, we need to Union the array back into a single set first.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!