3
public enum Smoking {
    NO("No"),YES("Yes");
}

How to store java enums using spring-data-elasticsearch, I want to store Yes, No and search for the same

1 Answer 1

5

You can do so by providing custom converters for your Enum to convert it from and to a String. I suppose you want to have this property as a keyword in Elasticsearch and not analyzed.

Here is an implementation of the Smoking enum where I have added the necessary converters as nested enums (I prefer to use the enum as singleton implementation for converters):

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;

public enum Smoking {
    YES("yes"),
    NO("No");

    private String elasticsearchName;

    Smoking(String elasticsearchName) {
        this.elasticsearchName = elasticsearchName;
    }

    @WritingConverter
    public enum SmokingToStringConverter implements Converter<Smoking, String> {

        INSTANCE;

        @Override
        public String convert(Smoking source) {
            return source.elasticsearchName;
        }
    }

    @ReadingConverter
    public enum StringToSmokingConverter implements Converter<String, Smoking> {

        INSTANCE;

        @Override
        public Smoking convert(String source) {
            for (Smoking smoking : Smoking.values()) {
                if (smoking.elasticsearchName.equals(source)) {
                    return smoking;
                }
            }
            return null;
        }
    }
}

The converters need to be registered, this can be done in the configuration class (see the documentation about configuring the client at https://docs.spring.io/spring-data/elasticsearch/docs/4.0.4.RELEASE/reference/html/#elasticsearch.clients.rest) by adding a custom implementation of elasticsearchCustomConversions():

@Override
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
    return new ElasticsearchCustomConversions(Arrays.asList(
        Smoking.SmokingToStringConverter.INSTANCE,
        Smoking.StringToSmokingConverter.INSTANCE)
    );
}

You then would use your enum class in your entity:

@Field(type = FieldType.Keyword)
private Smoking smoking;

That's all, the enum values are stored in Elasticsearch in the desired form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.