in order to use spring-data
findBy**
methods, you should have index for queried fields (in your case, index for bin mobileNumber
), otherwise, aerospike will need full scan of all documents in your collection (whether full scan be proceeded or rejected - depends on aerospike configuration). index will not be automatically created.
so please create index (either programmatically or manually). in case programmatically, you could use the following method:
public void createIndex(AerospikeRepository<?, ?> aerospikeRepository,
Class<?> entityClass,
String indexName,
String fieldName,
IndexType indexType) {
var entityName = entityClass.getSimpleName();
log.info("Creating Aerospike index [{}] for field [{}] of entity [{}]", indexName, fieldName, entityName);
try {
aerospikeRepository.createIndex(entityClass, indexName, fieldName, indexType);
log.info("Aerospike index [{}] was successfully created for field [{}] of entity [{}]", indexName, fieldName, entityName);
} catch (IndexAlreadyExistsException ex) {
log.info("Aerospike index [{}] for field [{}] of entity [{}] already exists", indexName, fieldName, entityName);
} catch (AerospikeException e) {
if (e.getResultCode() == ResultCode.INDEX_ALREADY_EXISTS) {
log.info("Aerospike index [{}] for field [{}] of entity [{}] already exists", indexName, fieldName, entityName);
return;
}
throw e;
}
}
you could invoke this method createIndex
in spring-boot
app either in InitializingBean afterPropertiesSet()
or in @PostConstruct
:
createIndex(customerRepository, Customer.class, "customer_mobileNumber_index", "mobileNumber", IndexType.STRING);
still, such index will fetch records only by a single field mobileNumber
, and you need to filter out returned result in java code by customerType
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…