Search code examples
spring-data-elasticsearch

spring data Elasticsearch dynamic mapping


Given using annotation on both Class and fields levels to set my mappings settings and use settings file like this

@Setting(settingPath = "settings.json")

to define custom analyzers, how would I set mappings "dynamic": false ?


Solution

  • Setting the dynamic mapping type on document and objects was introduced in Spring Data Elasticsearch version 4.0.0. It can be defined like this (code from the tests):

    @Document(indexName = "test-index-configure-dynamic-mapping")
    @DynamicMapping(DynamicMappingValue.False)
    class ConfigureDynamicMappingEntity {
    
        @Nullable 
        @DynamicMapping(DynamicMappingValue.Strict) 
        @Field(type = FieldType.Object) 
        private Author author;
    
        @Nullable
        public Author getAuthor() {
            return author;
        }
    
        public void setAuthor(Author author) {
            this.author = author;
        }
    }