扩展 Spring 数据的 @Document 注释

Extending Spring data's @Document annotation

我正在尝试将 org.springframework.data.elasticsearch.annotations.Document 包装到自定义注释 MyDocument 中。 @MyDocument 将继承父级@Document 所拥有的所有内容,并且 spring 数据库也应该按照父级 @Document 的方式处理 MyDocument 注释。

这可能吗?

我试过下面的代码,但我无法为@Document 设置所需的 'indexName' 参数。

@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 :我实际上需要通过这个@MyDocument

传递所有@Document 参数

EDIT#2 : 添加@Document注释class以供参考

您只是缺少 @AliasFor 注释的 value 参数:

@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....


}

注意 这仅适用于 spring-data-elasticsearch 版本 4.2.x(及更高版本)。