Search code examples
javaspringspring-dataspring-data-elasticsearch

Extending Spring data's @Document annotation


I am trying to wrap org.springframework.data.elasticsearch.annotations.Document into a custom annotation , MyDocument. @MyDocument will inherit everything that the parent @Document has, and the spring data library should also process MyDocument annotation that way it would do it for parent @Document.

Is this possible at all?

I tried below code, but I can't set the required 'indexName' param for @Document.

@Document() // HOW TO PUT indexName of @Mydocument into this?
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface MyDocument {
     @AliasFor(annotation = Document.class, attribute = "indexName")
    String indexName();

    @AliasFor(annotation = Document.class, attribute = "useServerConfiguration")
    boolean useServerConfiguration() default false;

    @AliasFor(annotation = Document.class, attribute = "shards")
    short shards() default 1;

    @AliasFor(annotation = Document.class, attribute = "replicas")
    short replicas() default 1;

    @AliasFor(annotation = Document.class, attribute = "refreshInterval")
    String refreshInterval() default "1s";

    @AliasFor(annotation = Document.class, attribute = "indexStoreType")
    String indexStoreType() default "fs";

    @AliasFor(annotation = Document.class, attribute = "createIndex")
    boolean createIndex() default true;

    @AliasFor(annotation = Document.class, attribute = "versionType")
    VersionType versionType() default VersionType.EXTERNAL;

    // My document specific props
    String myCustomProp() default "myDefault";

    // more properties....


}

REFERENCE for @Document Annotation by spring data elasticsearch

@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Document {

    String indexName();
    @Deprecated
    String type() default "";
    boolean useServerConfiguration() default false;
    
    short shards() default 1;

    short replicas() default 1;

    String refreshInterval() default "1s";

    String indexStoreType() default "fs";

    boolean createIndex() default true;

    VersionType versionType() default VersionType.EXTERNAL;
}


EDITED : I actually needed to pass all the @Document params via this @MyDocument

EDIT#2 : Added @Document annotation class for reference


Solution

  • You are just missing the value argument of the @AliasFor annotation:

    @Document() // HOW TO PUT indexName of @Mydocument into this?
    @Persistent
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    public @interface MyDocument {
    
        @AliasFor(value = "indexName", annotation = Document.class)
        String indexName();
    
        boolean useServerConfiguration() default false;
    
        short shards() default 1;
    
        short replicas() default 1;
    
        String refreshInterval() default "1s";
    
        String indexStoreType() default "fs";
    
        boolean createIndex() default true;
    
        VersionType versionType() default VersionType.EXTERNAL;
    
        // My document specific props
        String myCustomProp() default "myDefault";
    
        // more properties....
    
    
    }
    

    Note that this only works in spring-data-elasticsearch version 4.2.x (and above).