Search code examples
elasticsearchspring-data-elasticsearch

How to store height information as a float in Elasticsearch


How to store height information as a float in spring-data-elasticsearch. My data model looks like this

Height.java
    int feet, inches;

@Document(indexName = "users", shards = 1, versionType = VersionType.INTERNAL, createIndex = true)
public class User implements Persistable<String> {
    private Height height;
}

Solution

  • FloatConverter seems to work

    @WritingConverter
    public enum ToFloatConverter implements Converter<Height, Float> {
    
        INSTANCE;
    
        @Override
        public Float convert(Height height) {
            String heightStr = height.getFeet() + "." + height.getInches();
            return Float.parseFloat(heightStr);
        }
    }