-
Notifications
You must be signed in to change notification settings - Fork 7
Basic Indexes
An index allows you to quickly find a document or a range of documents based on the value of an attribute of the document at the cost of slower write speeds and increased disk use.
In order to create and use an index, you need a query. Currently, the query system used is msgpack's. The query syntax is intuitive extremely simple to use. Take this struct for example:
type Person struct {
Name string
Age int
}
If you want to index the Person document by age, your query is just "Age"
. Let's take a look at a slightly more advanced example with nested values.
type Person struct {
Name string
Company struct {
Name string
FoundingYear int
}
}
To refer to a nested value, you simply just use a .
. It's just like nested objects in JavaScript.
So for example in order to index the document by the company's name, your query would be "Company.Name"
.
You can create an index like so:
err := db.Table("table_name").NewIndex("your query")
So using the Person example from above:
err := db.Table("people").NewIndex("Age")
That's all there is to it! Note that index creation might take a while if you already have a lot of documents in the table, as it needs to index the documents already in the table.
You can get the first document result of an index lookup using One
:
// Find who is exactly 17.
var result Person
key, counter, err := db.Table("people").Index("Age").One(16, &result)
If you want to get all of the results of an index (since a single index value can point to multiple documents) you can use GetAll
:
// Find everyone who is exactly 17
r, err := db.Table("people").Index("Age").GetAll(17)
for {
var result Person
key, counter, err := r.Next(&result)
if err != nil {
break
}
// Do something with the result
}
If you want the results of an index on a range of values, you can use Between
. Since all values in Cete are stored in-order, your results will also be sorted in ascending order:
// Find everyone who is between 15 and 18 (inclusive).
r, err := db.Table("people").Index("Age").Between(15, 18)
for {
var result Person
key, counter, err := r.Next(&result)
if err != nil {
break
}
// Do something with the result
}
You can reverse this result if you want descending order by specifying an additional parameter:
// The true at the end will reverse the results to be in descending order.
r, err := db.Table("people").Index("Age").Between(15, 18, true)
You can also use cete.MinValue
and cete.MaxValue
to refer to absolute minimum and maximum bounds. For example if you want to find everyone who is older or equal to 18 with no upper bound:
r, err := db.Table("people").Index("Age").Between(18, cete.MaxValue)
If you wish to delete an index, you can use the Drop
method:
err := db.Table("people").Index("Age").Drop()
If your index becomes corrupt and needs to be regenerated, you need to Drop
it first, then call NewIndex
.
Cete supports more complex indexing features, such as compound indexes (indexes based on multiple document attributes) and multi-indexes (multiple index values refer to the same document, like "tags" on a blog post). You can find examples on using these features in Advanced Indexes.