Querying the database
To query, create a filter
object, where each each property on the object maps to a registered Index key.
In the following example, we are looking for movies where:
genre
is "Action" or "Comedy"title
includes "day"score
is greater or equal to 6
// Each property on the filter should match a registered index key
let filter = {
genre: ["Action", "Comedy"],
title: "day*",
score: { min: 6 },
};
// Pass the filter to the `query(criteria)` method
let { items, refiners } = await refinerDB.query({ filter });
Exact Equals
// Find Action movies
let filter = { genre: "Action" };
Pass multiple values and they will be OR
'd together.
// Find movies with a genre of Action or Comedy
let filter = { genre: ["Action", "Comedy"] };
If you add multiple indexes to the filter, they will be AND
'd together.
// Find all Action or Horror movies rated PG-13
let filter = { genre: ["Action", "Horror"], mpaa: "PG-13" };
Number Indexes work the same way
// Find anything with a score of exactly 7.1
let filter = { score: 7.1 };
See below for the min/max filter that may be more useful for Number indexes
String contains
You can query IndexType.String
indexes using a 'contains' by including an *
(asterisk) in your filter value.
// Put an asterisk at the end to do a "contains" query
// Find any movies with 'day' anywhere in the title
let filter = { title: "day*" };
// Find any movies with 'day' or 'night' anywhere in the title
let filter = { title: ["day*", "night*"] };
Min Max
You can query IndexType.Number
and IndexType.Date
indexes using a min/max range.
// Find all movies with a score greater or equal to 7
let filter = {
score: { min: 7 },
};
// Find all movies released in 2012 with score less than 5
let filter = {
released: {
min: new Date("1/1/2012"),
max: new Date("12/31/2012"),
},
score: { max: 5 },
};
Sorting
TODO: describe sort
and sortDir
Paging
TODO: describe limit
and skip