英文:
How to store java enums using spring-data-elasticsearch
问题
public enum Smoking {
NO("No"), YES("Yes");
}
如何使用spring-data-elasticsearch存储Java枚举,我想存储Yes、No,并且能够搜索相同的值。
英文:
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("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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论