Indexes
Indexes can be ensured using the following annotations. For reference see the indexing documentation and specific aspects that apply to indexes on shards.
Annotation @<IndexType>Indexed
With the @<IndexType>Indexed annotations user defined indexes can be created
at a collection level by annotating single fields of a class.
Possible @<IndexType>Indexed annotations are:
@PersistentIndexed@GeoIndexed@FulltextIndexed(deprecated)@TtlIndexed
The following example creates a persistent index on the field name and a
separate persistent index on the field age:
public class Person {
@PersistentIndexed
private String name;
@PersistentIndexed
private int age;
}
With the @<IndexType>Indexed annotations different indexes can be created on the same field.
The following example creates a TTL index and also a persistent index on the field name:
public class Person {
@TtlIndexed
@PersistentIndexed
private String name;
}
Annotation @<IndexType>Index
If the index should include multiple fields the @<IndexType>Index annotations
can be used on the type instead.
Possible @<IndexType>Index annotations are:
@PersistentIndex@GeoIndex@FulltextIndex(deprecated)@TtlIndex@MDIndex@MDPrefixedIndex
The following example creates a single persistent index on the fields name and
age, note that if a field is renamed in the database with @Field, the new
field name must be used in the index declaration:
@PersistentIndex(fields = {"fullname", "age"})
public class Person {
@Field("fullname")
private String name;
private int age;
}
The @<IndexType>Index annotations can also be used to create an index on a nested field.
The following example creates a single persistent index on the fields name and
address.country:
@PersistentIndex(fields = {"name", "address.country"})
public class Person {
private String name;
private Address address;
}
The @<IndexType>Index annotations and the @<IndexType>Indexed annotations
can be used at the same time in one class.
The following example creates a persistent index on the fields name and age
and a separate persistent index on the field age:
@PersistentIndex(fields = {"name", "age"})
public class Person {
private String name;
@PersistentIndexed
private int age;
}
The @<IndexType>Index annotations can be used multiple times to create more
than one index in this way.
The following example creates a persistent index on the fields name and age
and a separate persistent index on the fields name and gender:
@PersistentIndex(fields = {"name", "age"})
@PersistentIndex(fields = {"name", "gender"})
public class Person {
private String name;
private int age;
private Gender gender;
}
