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:

    try
    {
        Value result = await client.Query(
            Paginate(Match(Index("all_people")))
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    ObjectV(data: Arr(Arr(StringV(Alan), StringV(Perlis), RefV(id = "310141133713834496", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(StringV(Alan), StringV(Turing), RefV(id = "310141133716980224", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(StringV(Grace), StringV(Hopper), RefV(id = "310141133720125952", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(StringV(Leslie), StringV(Lamport), RefV(id = "310141133724320256", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(StringV(Marvin), StringV(Minsky), RefV(id = "310141133728514560", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(StringV(Stephen), StringV(Cook), RefV(id = "310141133731660288", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(StringV(Tim), StringV(Cook), RefV(id = "310141133734806016", collection = RefV(id = "People", collection = RefV(id = "collections"))))))
    result, err := client.Query(
    	f.Paginate(f.Match(f.Index("all_people"))))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[data:[[Alan Perlis {310014288737600000 0xc000180570 0xc000180570 <nil>}] [Alan Turing {310014288735502848 0xc000180720 0xc000180720 <nil>}] [Grace Hopper {310014288734454272 0xc0001808d0 0xc0001808d0 <nil>}] [Leslie Lamport {310014288732357120 0xc000180a80 0xc000180a80 <nil>}] [Marvin Minsky {310014288731308544 0xc000180c30 0xc000180c30 <nil>}] [Stephen Cook {310014288729211392 0xc000180de0 0xc000180de0 <nil>}] [Tim Cook {310014288729212416 0xc000180f90 0xc000180f90 <nil>}]]]
    System.out.println(
        client.query(
            Paginate(Match(Index("all_people")))
        ).get());
    {data: [["Alan", "Perlis", ref(id = "310136466189582848", collection = ref(id = "People", collection = ref(id = "collections")))], ["Alan", "Turing", ref(id = "310136466193777152", collection = ref(id = "People", collection = ref(id = "collections")))], ["Grace", "Hopper", ref(id = "310136466196922880", collection = ref(id = "People", collection = ref(id = "collections")))], ["Leslie", "Lamport", ref(id = "310136466201117184", collection = ref(id = "People", collection = ref(id = "collections")))], ["Marvin", "Minsky", ref(id = "310136466205311488", collection = ref(id = "People", collection = ref(id = "collections")))], ["Stephen", "Cook", ref(id = "310136466210554368", collection = ref(id = "People", collection = ref(id = "collections")))], ["Tim", "Cook", ref(id = "310136466214748672", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    client.query(
      q.Paginate(q.Match(q.Index('all_people')))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    {
      data: [
        [
          'Alan',
          'Perlis',
          Ref(Collection("People"), "310011992898273792")
        ],
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "310011992901419520")
        ],
        [
          'Grace',
          'Hopper',
          Ref(Collection("People"), "310011992897225216")
        ],
        [
          'Leslie',
          'Lamport',
          Ref(Collection("People"), "310011992895129088")
        ],
        [
          'Marvin',
          'Minsky',
          Ref(Collection("People"), "310011992895128064")
        ],
        [
          'Stephen',
          'Cook',
          Ref(Collection("People"), "310011992891983360")
        ],
        [ 'Tim', 'Cook', Ref(Collection("People"), "310011992891982336") ]
      ]
    }
    result = client.query(
      q.paginate(q.match(q.index("all_people")))
    )
    print(result)
    {'data': [['Alan', 'Perlis', Ref(id=310013802959603200, collection=Ref(id=People, collection=Ref(id=collections)))], ['Alan', 'Turing', Ref(id=310013802959602176, collection=Ref(id=People, collection=Ref(id=collections)))], ['Grace', 'Hopper', Ref(id=310013802957505024, collection=Ref(id=People, collection=Ref(id=collections)))], ['Leslie', 'Lamport', Ref(id=310013802956457472, collection=Ref(id=People, collection=Ref(id=collections)))], ['Marvin', 'Minsky', Ref(id=310013802956456448, collection=Ref(id=People, collection=Ref(id=collections)))], ['Stephen', 'Cook', Ref(id=310013802953310720, collection=Ref(id=People, collection=Ref(id=collections)))], ['Tim', 'Cook', Ref(id=310013802953311744, collection=Ref(id=People, collection=Ref(id=collections)))]]}
    try {
      println(Await.result(
        client.query(
          Paginate(Match(Index("all_people")))
        ),
        5.seconds
      ))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    {data: [["Cook", "Stephen", ref(id = "310139172759273984", collection = ref(id = "People", collection = ref(id = "collections")))], ["Cook", "Tim", ref(id = "310139172763468288", collection = ref(id = "People", collection = ref(id = "collections")))], ["Hopper", "Grace", ref(id = "310139172730962432", collection = ref(id = "People", collection = ref(id = "collections")))], ["Lamport", "Leslie", ref(id = "310139172749836800", collection = ref(id = "People", collection = ref(id = "collections")))], ["Minsky", "Marvin", ref(id = "310139172755079680", collection = ref(id = "People", collection = ref(id = "collections")))], ["Perlis", "Alan", ref(id = "310139172723622400", collection = ref(id = "People", collection = ref(id = "collections")))], ["Turing", "Alan", ref(id = "310139172727816704", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    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") ]
      ]
    }
  2. Results from index matches use a specific sorting precedence. The following example uses an index that sorts by age descending:

    try
    {
        Value result = await client.Query(
            Paginate(Match(Index("people_by_age_desc")))
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    ObjectV(data: Arr(Arr(LongV(119), NullV, StringV(Hopper), RefV(id = "310141133720125952", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(LongV(107), NullV, StringV(Turing), RefV(id = "310141133716980224", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(LongV(97), NullV, StringV(Perlis), RefV(id = "310141133713834496", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(LongV(92), NullV, StringV(Minsky), RefV(id = "310141133728514560", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(LongV(81), NullV, StringV(Cook), RefV(id = "310141133731660288", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(LongV(80), NullV, StringV(Lamport), RefV(id = "310141133724320256", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(LongV(59), NullV, StringV(Cook), RefV(id = "310141133734806016", collection = RefV(id = "People", collection = RefV(id = "collections"))))))
    result, err := client.Query(
    	f.Paginate(f.Match(f.Index("people_by_age_desc"))))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[data:[[119 Grace Hopper {310014288734454272 0xc0001099b0 0xc0001099b0 <nil>}] [107 Alan Turing {310014288735502848 0xc000109b60 0xc000109b60 <nil>}] [97 Alan Perlis {310014288737600000 0xc000109d10 0xc000109d10 <nil>}] [92 Marvin Minsky {310014288731308544 0xc000109ec0 0xc000109ec0 <nil>}] [81 Stephen Cook {310014288729211392 0xc00016e090 0xc00016e090 <nil>}] [80 Leslie Lamport {310014288732357120 0xc00016e240 0xc00016e240 <nil>}] [59 Tim Cook {310014288729212416 0xc00016e3f0 0xc00016e3f0 <nil>}]]]
    System.out.println(
        client.query(
            Paginate(Match(Index("people_by_age_desc")))
        ).get());
    {data: [[119, "Grace", "Hopper", ref(id = "310136723542639104", collection = ref(id = "People", collection = ref(id = "collections")))], [107, "Alan", "Turing", ref(id = "310136723538444800", collection = ref(id = "People", collection = ref(id = "collections")))], [97, "Alan", "Perlis", ref(id = "310136723535299072", collection = ref(id = "People", collection = ref(id = "collections")))], [92, "Marvin", "Minsky", ref(id = "310136723566756352", collection = ref(id = "People", collection = ref(id = "collections")))], [81, "Stephen", "Cook", ref(id = "310136723571999232", collection = ref(id = "People", collection = ref(id = "collections")))], [80, "Leslie", "Lamport", ref(id = "310136723546833408", collection = ref(id = "People", collection = ref(id = "collections")))], [59, "Tim", "Cook", ref(id = "310136723577242112", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    client.query(
      q.Paginate(q.Match(q.Index('people_by_age_desc')))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    {
      data: [
        [
          119,
          'Grace',
          'Hopper',
          Ref(Collection("People"), "310012262298419712")
        ],
        [
          107,
          'Alan',
          'Turing',
          Ref(Collection("People"), "310012262299469312")
        ],
        [
          97,
          'Alan',
          'Perlis',
          Ref(Collection("People"), "310012262299468288")
        ],
        [
          92,
          'Marvin',
          'Minsky',
          Ref(Collection("People"), "310012262295273984")
        ],
        [
          81,
          'Stephen',
          'Cook',
          Ref(Collection("People"), "310012262292129280")
        ],
        [
          80,
          'Leslie',
          'Lamport',
          Ref(Collection("People"), "310012262296322560")
        ],
        [
          59,
          'Tim',
          'Cook',
          Ref(Collection("People"), "310012262292128256")
        ]
      ]
    }
    result = client.query(
      q.paginate(q.match(q.index("people_by_age_desc")))
    )
    print(result)
    {'data': [[119, 'Grace', 'Hopper', Ref(id=310013488491660800, collection=Ref(id=People, collection=Ref(id=collections)))], [107, 'Alan', 'Turing', Ref(id=310013488494805504, collection=Ref(id=People, collection=Ref(id=collections)))], [97, 'Alan', 'Perlis', Ref(id=310013488495854080, collection=Ref(id=People, collection=Ref(id=collections)))], [92, 'Marvin', 'Minsky', Ref(id=310013488490611200, collection=Ref(id=People, collection=Ref(id=collections)))], [81, 'Stephen', 'Cook', Ref(id=310013488487466496, collection=Ref(id=People, collection=Ref(id=collections)))], [80, 'Leslie', 'Lamport', Ref(id=310013488491659776, collection=Ref(id=People, collection=Ref(id=collections)))], [59, 'Tim', 'Cook', Ref(id=310013488487465472, collection=Ref(id=People, collection=Ref(id=collections)))]]}
    try {
      println(Await.result(
        client.query(
          Paginate(Match(Index("people_by_age_desc")))
        ),
        5.seconds
      ))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    {data: [[119, "Grace", "Hopper", ref(id = "310138747409662464", collection = ref(id = "People", collection = ref(id = "collections")))], [107, "Alan", "Turing", ref(id = "310138747404419584", collection = ref(id = "People", collection = ref(id = "collections")))], [97, "Alan", "Perlis", ref(id = "310138747400225280", collection = ref(id = "People", collection = ref(id = "collections")))], [92, "Marvin", "Minsky", ref(id = "310138747420148224", collection = ref(id = "People", collection = ref(id = "collections")))], [81, "Stephen", "Cook", ref(id = "310138747424342528", collection = ref(id = "People", collection = ref(id = "collections")))], [80, "Leslie", "Lamport", ref(id = "310138747413856768", collection = ref(id = "People", collection = ref(id = "collections")))], [59, "Tim", "Cook", ref(id = "310138747428536832", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    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")
        ]
      ]
    }
  3. For indexes where the terms field was defined, entries are located using the indexed terms:

    try
    {
        Value result = await client.Query(
            Paginate(Match(Index("people_by_first"), "Alan"))
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    ObjectV(data: Arr(Arr(StringV(Alan), StringV(Perlis), RefV(id = "310141133713834496", collection = RefV(id = "People", collection = RefV(id = "collections")))), Arr(StringV(Alan), StringV(Turing), RefV(id = "310141133716980224", collection = RefV(id = "People", collection = RefV(id = "collections"))))))
    result, err := client.Query(
    	f.Paginate(f.MatchTerm(f.Index("people_by_first"), "Alan")))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[data:[[Alan Perlis {310014511710994944 0xc000184570 0xc000184570 <nil>}] [Alan Turing {310014511706800640 0xc000184720 0xc000184720 <nil>}]]]
    System.out.println(
        client.query(
            Paginate(
                Match(
                    Index("people_by_first"),
                    Value("Alan")
                )
            )
        ).get());
    {data: [[null, null, ref(id = "310136723535299072", collection = ref(id = "People", collection = ref(id = "collections")))], [null, null, ref(id = "310136723538444800", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    client.query(
      q.Paginate(q.Match(q.Index('people_by_first'), 'Alan'))
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    {
      data: [
        [
          'Alan',
          'Perlis',
          Ref(Collection("People"), "310012851960939008")
        ],
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "310012851957793280")
        ]
      ]
    }
    result = client.query(
      q.paginate(q.match(q.index("people_by_first"), "Alan"))
    )
    print(result)
    {'data': [['Alan', 'Perlis', Ref(id=310014237971841536, collection=Ref(id=People, collection=Ref(id=collections)))], ['Alan', 'Turing', Ref(id=310014237970793984, collection=Ref(id=People, collection=Ref(id=collections)))]]}
    try {
      println(Await.result(
        client.query(
          Paginate(Match(Index("people_by_first"), "Alan"))
        ),
        5.seconds
      ))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    {data: [["Alan", "Perlis", ref(id = "310138747400225280", collection = ref(id = "People", collection = ref(id = "collections")))], ["Alan", "Turing", ref(id = "310138747404419584", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    Paginate(Match(Index('people_by_first'), 'Alan'))
    {
      data: [
        [
          'Alan',
          'Perlis',
          Ref(Collection("People"), "309935512027660800")
        ],
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "309935512023466496")
        ]
      ]
    }
  4. For indexes where a binding was defined in the terms field, Match works as if the binding was a normal field:

    try
    {
        Value result = await client.Query(
            Paginate(
                Match(
                    Index("people_by_fullname"),
                    "Alan Turing"
                )
            )
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    ObjectV(data: Arr(Arr(StringV(Alan Turing), RefV(id = "310142131057459712", collection = RefV(id = "People", collection = RefV(id = "collections"))))))
    result, err := client.Query(
    	f.Paginate(
    		f.MatchTerm(
    			f.Index("people_by_fullname"),
    			"Alan Turing",
    		),
    	),
    )
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[data:[[Alan Turing {310014847404212736 0xc000092570 0xc000092570 <nil>}]]]
    System.out.println(
        client.query(
            Paginate(
                Match(
                    Index("people_by_fullname"),
                    Value("Alan Turing")
                )
            )
        ).get());
    {data: [["Alan Turing", ref(id = "310136466193777152", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    client.query(
      q.Paginate(
        q.Match(
          q.Index('people_by_fullname'),
          'Alan Turing'
        )
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    {
      data: [
        [ 'Alan Turing', Ref(Collection("People"), "310011445570961920") ]
      ]
    }
    result = client.query(
      q.paginate(
        q.match(
          q.index("people_by_fullname"),
          "Alan Turing"
        )
      )
    )
    print(result)
    {'data': [['Alan Turing', Ref(id=310013344420463104, collection=Ref(id=People, collection=Ref(id=collections)))]]}
    try {
      println(Await.result(
        client.query(
          Paginate(
            Match(
              Index("people_by_fullname"),
              "Alan Turing"
            )
          )
        ),
        5.seconds
      ))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    {data: [["Alan Turing", ref(id = "310138747404419584", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    Paginate(
      Match(
        Index('people_by_fullname'),
        'Alan Turing'
      )
    )
    {
      data: [
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "309995028357317120")
        ]
      ]
    }
  5. For indexes where there are multiple terms fields defined, the Match values must be expressed as an array:

    try
    {
        Value result = await client.Query(
            Paginate(
                Match(
                    Index("people_by_first_last"),
                    Arr("Alan", "Turing")
                )
            )
        );
        Console.WriteLine(result);
    }
    catch (Exception e)
    {
        Console.WriteLine($"ERROR: {e.Message}");
    }
    ObjectV(data: Arr(Arr(StringV(Alan), StringV(Turing), RefV(id = "310141133716980224", collection = RefV(id = "People", collection = RefV(id = "collections"))))))
    result, err := client.Query(
    	f.Paginate(
    		f.MatchTerm(
    			f.Index("people_by_first_last"),
    			f.Arr{"Alan", "Turing"},
    		),
    	))
    
    if err != nil {
    	fmt.Fprintln(os.Stderr, err)
    } else {
    	fmt.Println(result)
    }
    map[data:[[Alan Turing {310014511706800640 0xc000092570 0xc000092570 <nil>}]]]
    System.out.println(
        client.query(
            Paginate(
                Match(
                    Index("people_by_first_last"),
                    Arr(Value("Alan"), Value("Turing"))
                )
            )
        ).get());
    {data: [["Alan", "Turing", ref(id = "310137132021711360", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    client.query(
      q.Paginate(
        q.Match(
          q.Index('people_by_first_last'),
          ['Alan', 'Turing'],
        )
      )
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error('Error: %s', err))
    {
      data: [
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "310005407260082688")
        ]
      ]
    }
    result = client.query(
      q.paginate(
        q.match(
          q.index("people_by_first_last"),
          ["Alan", "Turing"]
        )
      )
    )
    print(result)
    {'data': [['Alan', 'Turing', Ref(id=310013947139850752, collection=Ref(id=People, collection=Ref(id=collections)))]]}
    try {
      println(Await.result(
        client.query(
          Paginate(
            Match(
              Index("people_by_first_last"),
              Arr("Alan", "Turing")
            )
          )
        ),
        5.seconds
      ))
    } catch {
      case unknown: Throwable => println("Error: " + unknown.getMessage())
    }
    {data: [["Alan", "Turing", ref(id = "310138747404419584", collection = ref(id = "People", collection = ref(id = "collections")))]]}
    Paginate(
      Match(
        Index("people_by_first_last"),
        ["Alan", "Turing"]
      )
    )
    {
      data: [
        [
          'Alan',
          'Turing',
          Ref(Collection("People"), "309995228827222528")
        ]
      ]
    }

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!