Indexes

Problem

You need to query an index within the current database.

Solution

The Match function is used to find matching entries in an index.

  1. Indexes created without specifying the terms field return all indexed values on the indexed collection:

    Copied!
    client.query(
      q.(q.(q.('all_people')))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    {
      data: [
        [
          'Alan',
          'Perlis',
          (("People"), "310011992898273792")
        ],
        [
          'Alan',
          'Turing',
          (("People"), "310011992901419520")
        ],
        [
          'Grace',
          'Hopper',
          (("People"), "310011992897225216")
        ],
        [
          'Leslie',
          'Lamport',
          (("People"), "310011992895129088")
        ],
        [
          'Marvin',
          'Minsky',
          (("People"), "310011992895128064")
        ],
        [
          'Stephen',
          'Cook',
          (("People"), "310011992891983360")
        ],
        [ 'Tim', 'Cook', (("People"), "310011992891982336") ]
      ]
    }
    Query metrics:
    •    bytesIn:   56

    •   bytesOut:  979

    • computeOps:    1

    •    readOps:    8

    •   writeOps:    0

    •  readBytes:  602

    • writeBytes:    0

    •  queryTime: 16ms

    •    retries:    0

  2. Results from index matches use a specific sorting precedence. The following example uses an index that sorts by age descending:

    Copied!
    client.query(
      q.(q.(q.('people_by_age_desc')))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    {
      data: [
        [
          119,
          'Grace',
          'Hopper',
          (("People"), "310012262298419712")
        ],
        [
          107,
          'Alan',
          'Turing',
          (("People"), "310012262299469312")
        ],
        [
          97,
          'Alan',
          'Perlis',
          (("People"), "310012262299468288")
        ],
        [
          92,
          'Marvin',
          'Minsky',
          (("People"), "310012262295273984")
        ],
        [
          81,
          'Stephen',
          'Cook',
          (("People"), "310012262292129280")
        ],
        [
          80,
          'Leslie',
          'Lamport',
          (("People"), "310012262296322560")
        ],
        [
          59,
          'Tim',
          'Cook',
          (("People"), "310012262292128256")
        ]
      ]
    }
    Query metrics:
    •    bytesIn:    64

    •   bytesOut: 1,002

    • computeOps:     1

    •    readOps:     8

    •   writeOps:     0

    •  readBytes:   623

    • writeBytes:     0

    •  queryTime: 131ms

    •    retries:     0

  3. For indexes where the terms field was defined, entries are located using the indexed terms:

    Copied!
    client.query(
      q.(q.(q.('people_by_first'), 'Alan'))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    {
      data: [
        [
          'Alan',
          'Perlis',
          (("People"), "310012851960939008")
        ],
        [
          'Alan',
          'Turing',
          (("People"), "310012851957793280")
        ]
      ]
    }
    Query metrics:
    •    bytesIn:   65

    •   bytesOut:  295

    • computeOps:    1

    •    readOps:    1

    •   writeOps:    0

    •  readBytes:  153

    • writeBytes:    0

    •  queryTime: 44ms

    •    retries:    0

  4. For indexes where a binding was defined in the terms field, Match works as if the binding was a normal field:

    Copied!
    client.query(
      q.(
        q.(
          q.('people_by_fullname'),
          'Alan Turing'
        )
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    {
      data: [
        [ 'Alan Turing', (("People"), "310011445570961920") ]
      ]
    }
    Query metrics:
    •    bytesIn:   75

    •   bytesOut:  157

    • computeOps:    1

    •    readOps:    1

    •   writeOps:    0

    •  readBytes:   94

    • writeBytes:    0

    •  queryTime: 99ms

    •    retries:    0

  5. For indexes where there are multiple terms fields defined, the Match values must be expressed as an array:

    Copied!
    client.query(
      q.(
        q.(
          q.('people_by_first_last'),
          ['Alan', 'Turing'],
        )
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    {
      data: [
        [
          'Alan',
          'Turing',
          (("People"), "310005407260082688")
        ]
      ]
    }
    Query metrics:
    •    bytesIn:   81

    •   bytesOut:  159

    • computeOps:    1

    •    readOps:    1

    •   writeOps:    0

    •  readBytes:   94

    • writeBytes:    0

    •  queryTime: 63ms

    •    retries:    0

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!