Nested Property Index
An index can be defined on a nested property to improve performance of nested queries - this is highly recommended.
Nested properties indexing uses an additional @SpaceIndex
attribute - path()
.
The SpaceIndex.path() Attribute
The path()
attribute represents the path of the property within the nested object.
Below is an example of defining an index on a nested property:
@SpaceClass
public class Person {
private int id;
private Info personalInfo;
private String description;
//getter and setter methods
...
// this defines and ORDERED index on the personalInfo.socialSecurity property
@SpaceIndex(path = "socialSecurity", type = SpaceIndexType.ORDERED)
public Info getPersonalInfo() {
return personalInfo;
}
}
public static class Info implements Serializable {
private String name;
private Address address;
private Date birthday;
private long socialSecurity;
private int _id;
//getter and setter methods
}
public static class Address implements Serializable {
private int zipCode;
private String street;
//getter and setter methods
}
Multiple annotations to a field can be written using repeatable annotations instead of compound annotations, as show below.
With compound annotations:
@SpaceIndexes( { @SpaceIndex(path = "socialSecurity", type = SpaceIndexType.ORDERED), @SpaceIndex(path = "address.zipCode", type = SpaceIndexType.EQUAL)})
With repeatable annotations:
@SpaceIndex(path = "socialSecurity", type = SpaceIndexType.ORDERED)
@SpaceIndex(path = "address.zipCode", type = SpaceIndexType.EQUAL)
@SpaceClass
public static class Person {
private int id;
private Info personalInfo;
private String description;
private HashMap<String, String> map;
//getter and setter methods
...
// this defines several indexes on the same personalInfo property
@SpaceIndexes( { @SpaceIndex(path = "socialSecurity", type = SpaceIndexType.ORDERED),
@SpaceIndex(path = "address.zipCode", type = SpaceIndexType.EQUAL)})
public Info getPersonalInfo() {
return personalInfo;
}
// this defines indexes on map keys
@SpaceIndexes( {@SpaceIndex(path="key1" , type = SpaceIndexType.EQUAL),
@SpaceIndex(path="key2" , type = SpaceIndexType.EQUAL)})
public HashMap<String, String> getMap() {
return map;
}
public void setMap(HashMap<String, String> map) {
this.map = map;
}
}
public static class Info implements Serializable {
private String name;
private Address address;
private Date birthday;
private long socialSecurity;
private int _id;
//getter and setter methods
}
public static class Address implements Serializable {
private int zipCode;
private String street;
//getter and setter methods
}
<gigaspaces-mapping>
<class name="com.gigaspaces.examples.Person" >
<property name="personalInfo">
<index path="socialSecurity" type = "ordered"/>
<index path="address.zipCode" type = "equal"/>
</property>
</class>
</gigaspaces-mapping>
The following is an example of query code that automatically triggers this index:
SQLQuery<Person> query = new SQLQuery<Person>(Person.class,
"personalInfo.socialSecurity<10000050L and personalInfo.socialSecurity>=10000010L");
For more information, see the SQL Query page.
The same indexing techniques above are also applicable to Map-based nested properties, which means that in the example above the Info
and Address
classes could be replaced with a java.util.Map<String,Object>
, with the map keys representing the property names.