如何使用spring-data-elasticsearch存储Java枚举。

huangapple go评论59阅读模式
英文:

How to store java enums using spring-data-elasticsearch

问题

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

如何使用spring-data-elasticsearch存储Java枚举我想存储YesNo并且能够搜索相同的值
英文:
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

得分: 5

你可以通过为枚举提供自定义转换器来实现这一点,以将其从字符串转换为枚举类型,反之亦然。我想你希望在Elasticsearch中将此属性作为关键字存储,而不是进行分析处理。

以下是实现Smoking枚举的代码示例,在其中我已添加了必要的转换器作为嵌套枚举(我更倾向于将枚举用作转换器的单例实现):

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;
        }
    }
}

这些转换器需要注册,可以在配置类中完成(请参阅有关配置客户端的文档,链接:https://docs.spring.io/spring-data/elasticsearch/docs/4.0.4.RELEASE/reference/html/#elasticsearch.clients.rest),通过添加elasticsearchCustomConversions()的自定义实现:

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

然后,在实体中使用你的枚举类:

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

这就是全部内容,枚举值将以所需的形式存储在Elasticsearch中。

英文:

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(&quot;yes&quot;),
    NO(&quot;No&quot;);

    private String elasticsearchName;

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

    @WritingConverter
    public enum SmokingToStringConverter implements Converter&lt;Smoking, String&gt; {

        INSTANCE;

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

    @ReadingConverter
    public enum StringToSmokingConverter implements Converter&lt;String, Smoking&gt; {

        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.

huangapple
  • 本文由 发表于 2020年10月10日 14:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64290524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定