Sets

Sets are sorted groups of tuples. An index derives sets from documents within the collections in its source. As documents are created, modified, and deleted, sets are updated to reflect their documents' current state.

Indexes are groups of sets, each of which has a natural key; a tuple of zero or more terms. The Match query function constructs a set ref to identify a set for a given tuple of terms within an index:

curl https://db.fauna.com/ \
    -u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
    -d '{ "match": { "index": "spells_by_element" }, "terms": "water" }'
client.Query(Match(Index("spells_by_element"), "water"));
System.out.println(
   client.query(
      Match(Index(Value("spells_by_element")), Value("water"))
).get());
result, _ := client.Query(f.MatchTerm(f.Index("spells_by_element"), "water"))

fmt.Println(result)
client.query(Match(Index("spells_by_element"), "water"))
client.query(q.match(q.index("spells_by_element"), "water"))
$client.query do
  match index('spells_by_element'), 'water'
end
client.query(
    Match(index: Index("spells_by_element"), terms: "water")
)
client.query(q.Match(q.Index('spells_by_element'), 'water'))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
  "resource": {
    "@set": {
      "match": { "@ref": "indexes/spells_by_element" },
      "terms": "water"
    }
  }
}
{
  "@set": {
    "match": { "@ref": "indexes/spells_by_element" },
    "terms": "water"
  }
}
{
  @set = {
    match: ref(id = "spells_by_element", collection = ref(id = "indexes")),
    terms: "water"
  }
}
{map[match:{spells_by_element 0xc4202b6040 <nil>} terms:water]}
{
  "@set": {
    "match": { "@ref": "indexes/spells_by_element" },
    "terms": "water"
  }
}
{
  "@set": {
    "match": { "@ref": "indexes/spells_by_element" },
    "terms": "water"
  }
}
{
  "@set": {
    "match": { "@ref": "indexes/spells_by_element" },
    "terms": "water"
  }
}
{
  "@set": {
    "match": { "@ref": "indexes/spells_by_element" },
    "terms": "water"
  }
}
SetRef({"match":{"@ref":{"id":"spells_by_element","class":{"@ref":{"id":"indexes"}}}},"terms":"water"})

Set refs are unique according to their structure: Two set refs with the same structure refer to the same set within a database. Query functions such as Union, Intersection, and Join allow the construction of more complex logical set identifiers:

curl https://db.fauna.com/ \
    -u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
    -d '{
          "intersection": [
            {
              "match": { "index": "spells_by_element" },
              "terms": "water"
            },
            {
              "match": { "index": "spells_by_element" },
              "terms": "fire"
            }
          ]
        }'
client.Query(
  Intersection(
    Match(Index("spells_by_element"), "water"),
    Match(Index("spells_by_element"), "fire")));
System.out.println(
    client.query(
       Intersection(
          Match(Index(Value("spells_by_element")), Value("fire")),
          Match(Index(Value("spells_by_element")), Value("water")))
    ).get());
result, _ := client.Query(
    f.Intersection(
        f.MatchTerm(f.Index("spells_by_element"), "water"),
        f.MatchTerm(f.Index("spells_by_element"), "fire"),
    ),
)

fmt.Println(result)
client.query(
  Intersection(
    Match(Index("spells_by_element"), "water"),
    Match(Index("spells_by_element"), "fire")))
client.query(
  q.intersection(
    q.match(q.index("spells_by_element"), "water"),
    q.match(q.index("spells_by_element"), "fire")
  ))
$client.query do
  intersection match(index('spells_by_element'), 'water'),
               match(index('spells_by_element'), 'fire')
end
client.query(
    Intersection(
        Match(
            index: Index("spells_by_element"),
            terms: "water"
        ),
        Match(
            index: Index("spells_by_element"),
            terms: "fire"
        )
    )
)
client.query(
  q.Intersection(
    q.Match(q.Index('spells_by_element'), 'water'),
    q.Match(q.Index('spells_by_element'), 'fire'),
  )
)
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
  "resource": {
    "@set": {
      "intersection": [
        {
          "@set": {
            "match": { "@ref": "indexes/spells_by_element" },
            "terms": "water"
          }
        },
        {
          "@set": {
            "match": { "@ref": "indexes/spells_by_element" },
            "terms": "fire"
          }
        }
      ]
    }
  }
}
{
  "@set": {
    "intersection": [
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "water"
        }
      },
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "fire"
        }
      }
    ]
  }
}
{
  @set = {
    intersection: [
    {
      @set = {
        match: ref(id = "spells_by_element", collection = ref(id = "indexes")),
        terms: "fire"
      },
      {
        @set = {
          match: ref(id = "spells_by_element", collection = ref(id = "indexes")),
          terms: "water"
        }
      }
    ]
  }
}
{map[intersection:[{map[match:{spells_by_element 0xc42026c760 <nil>} terms:water]} {map[match:{spells_by_element 0xc42026c920 <nil>} terms:fire]}]]}
{
  "@set": {
    "intersection": [
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "water"
        }
      },
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "fire"
        }
      }
    ]
  }
}
{
  "@set": {
    "intersection": [
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "water"
        }
      },
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "fire"
        }
      }
    ]
  }
}
{
  "@set": {
    "intersection": [
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "water"
        }
      },
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "fire"
        }
      }
    ]
  }
}
{
  "@set": {
    "intersection": [
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "water"
        }
      },
      {
        "@set": {
          "match": { "@ref": "indexes/spells_by_element" },
          "terms": "fire"
        }
      }
    ]
  }
}
SetRef({"intersection":[{"@set":{"match":{"@ref":{"id":"spells_by_element","class":{"@ref":{"id":"indexes"}}}},"terms":"water"}},{"@set":{"match":{"@ref":{"id":"spells_by_element","class":{"@ref":{"id":"indexes"}}}},"terms":"fire"}}]})

The Paginate function is used to retrieve the tuples of a set. The Page object returned by Paginate contains an array of tuples and cursors for moving forward or backward within the set.

Field Type Definition and Requirements

data

Array

The elements in the page.

after

Cursor

The cursor for the next page, inclusive. Optional.

before

Cursor

The cursor for the previous page, exclusive. Optional.

Pages can be further manipulated using collection-oriented functions such as Map and Filter, or individual elements can be extracted using select.

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!