Tip
|
For more details on secondary indexes please visit Aerospike Secondary Index official documentation. |
Out of the box Spring Data Aerospike supports creating secondary indexes in Aerospike.
There are two ways to accomplish this task:
-
Using
@Indexed
annotation placed over the indexed field in your entity; -
Using
createIndex
method ofAerospikeTemplate
.
Let’s dive into more details.
Place @Indexed
annotation over the necessary fields in your entity and specify required types of the index.
This will make spring-data-aerospike
auto-create specified secondary indexes in Aerospike
on startup of your application (it can be configured by create-indexes-on-startup
property,
by default it is true
).
Note
|
|
@Value
@Document
public class IndexedDocument {
@Id
String key;
@Indexed(type = STRING, collectionType = DEFAULT)
String author;
@Indexed(type = NUMERIC, collectionType = DEFAULT)
int likes;
@Indexed(type = NUMERIC, collectionType = LIST)
List<Integer> options;
}
In this example we will create an index at startup of the application manually.
@Slf4j
@Configuration
public class AerospikeIndexConfiguration {
private static final String INDEX_NAME = "movie-rating-index";
@Bean
@ConditionalOnProperty(
value = "aerospike." + INDEX_NAME + ".create-on-startup",
havingValue = "true",
matchIfMissing = true)
public boolean createAerospikeIndex(AerospikeTemplate aerospikeTemplate) {
aerospikeTemplate.createIndex(MovieDocument.class, INDEX_NAME, "rating", IndexType.NUMERIC);
log.info("Index {} was successfully created", INDEX_NAME);
return true;
}
}
Verify indexes were created using the following tests:
public class IndexTests extends SecondaryIndexAerospikeDemoApplicationTest {
@Value("${spring.data.aerospike.namespace}")
private String namespace;
@Autowired
AerospikeClient client;
@Test
void verifyCustomIndexCreated() {
List<String> existingIndexes = getIndexes(client, namespace);
assertThat(existingIndexes).contains("movie-rating-index");
}
@Test
void verifyAnnotationBasedIndexesCreated() {
List<String> existingIndexes = getIndexes(client, namespace);
assertThat(existingIndexes)
.contains(
"IndexedDocument_author_string_default",
"IndexedDocument_likes_numeric_default",
"IndexedDocument_options_numeric_list");
}
// DO NOT USE THIS CODE IN PRODUCTION
private static List<String> getIndexes(AerospikeClient client, String namespace) {
Node node = client.getNodes()[0];
String response = Info.request(node, "sindex/" + namespace);
return Arrays.stream(response.split(";"))
.map(info -> {
Map<String, String> keyValue = Arrays.stream(info.split(":"))
.map(part -> {
String[] kvParts = part.split("=");
return Map.entry(kvParts[0], kvParts[1]);
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return keyValue.get("indexname");
})
.collect(Collectors.toList());
}
}
Tip
|
If you are not familiar with how to set up embedded Aerospike server for your tests please see Getting Started: Testing. |
To see demo application go to Creating Secondary Index Demo.