Skip to content

Latest commit

 

History

History
135 lines (104 loc) · 4.24 KB

creating-secondary-index.adoc

File metadata and controls

135 lines (104 loc) · 4.24 KB

Creating secondary index with Spring Data Aerospike

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:

  1. Using @Indexed annotation placed over the indexed field in your entity;

  2. Using createIndex method of AerospikeTemplate.

Let’s dive into more details.

Creating index via @Indexed annotation

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

@Indexed annotation is not supported for the fields annotated with @Id (not needed for primary key), @Expiration or @Version (metadata).

IndexedDocument.java
@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;
}

Creating index via AerospikeTemplate

In this example we will create an index at startup of the application manually.

AerospikeIndexConfiguration.java
@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;
    }
}

Testing

Verify indexes were created using the following tests:

IndexTests.java
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.

Demo application

To see demo application go to Creating Secondary Index Demo.