FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, review the migration guide. Contact support@fauna.com with any questions.

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!
    Paginate(Match(Index('all_people')))
    {
      data: [
        [
          'Alan',
          'Perlis',
          Ref(Collection("People"), "309934841578653184")
        ],
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "309934841578652160")
        ],
        [
          'Grace',
          'Hopper',
          Ref(Collection("People"), "309934841576555008")
        ],
        [
          'Leslie',
          'Lamport',
          Ref(Collection("People"), "309934841575507456")
        ],
        [
          'Marvin',
          'Minsky',
          Ref(Collection("People"), "309934841575506432")
        ],
        [
          'Stephen',
          'Cook',
          Ref(Collection("People"), "309934841572360704")
        ],
        [ 'Tim', 'Cook', Ref(Collection("People"), "309934841572361728") ]
      ]
    }
    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!
    Paginate(Match(Index('people_by_age_desc')))
    {
      data: [
        [
          119,
          'Grace',
          'Hopper',
          Ref(Collection("People"), "309995028361511424")
        ],
        [
          107,
          'Alan',
          'Turing',
          Ref(Collection("People"), "309995028357317120")
        ],
        [
          97,
          'Alan',
          'Perlis',
          Ref(Collection("People"), "309995028360462848")
        ],
        [
          92,
          'Marvin',
          'Minsky',
          Ref(Collection("People"), "309995028352074240")
        ],
        [
          81,
          'Stephen',
          'Cook',
          Ref(Collection("People"), "309995028351026688")
        ],
        [
          80,
          'Leslie',
          'Lamport',
          Ref(Collection("People"), "309995028356268544")
        ],
        [
          59,
          'Tim',
          'Cook',
          Ref(Collection("People"), "309995028351025664")
        ]
      ]
    }
    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!
    Paginate(Match(Index('people_by_first'), 'Alan'))
    {
      data: [
        [
          'Alan',
          'Perlis',
          Ref(Collection("People"), "309935512027660800")
        ],
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "309935512023466496")
        ]
      ]
    }
    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!
    Paginate(
      Match(
        Index('people_by_fullname'),
        'Alan Turing'
      )
    )
    {
      data: [
        [ 'Alan Turing', Ref(Collection("People"), "313624171151098368") ]
      ]
    }
    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!
    Paginate(
      Match(
        Index("people_by_first_last"),
        ["Alan", "Turing"]
      )
    )
    {
      data: [
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "309995228827222528")
        ]
      ]
    }
    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!