java 使用 Spring-Data Elasticsearch 在 Elasticsearch 中动态创建索引名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33894618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Indices name Dynamically in Elasticsearch using Spring-Data Elasticsearch
提问by sagar27
I have a use case where in need to create the indices per month in Elasticsearch. The idea is to create indices on the monthly bases so that they are easy to maintain and can be deleted when expired.For this to achieve i have used the spring-batch and have a monthly job which will create the indices on monthly bases For Elasticsearch-Java integration I have used the Spring-Data Elasticsearch implementation. The problem which i am facing now is, I am not able to figure out how to provide the dynamic-name to the index and mapping using the Entity object. My Current implementation is done keeping the Single Index in mind. Please find the below code which i am using to create indices
我有一个用例,需要在 Elasticsearch 中每月创建索引。这个想法是在每月的基础上创建索引,以便它们易于维护并且可以在过期时删除。为此,我使用了 spring-batch 并有一个每月的工作,它将为 Elasticsearch 创建每月的索引-Java 集成我使用了 Spring-Data Elasticsearch 实现。我现在面临的问题是,我无法弄清楚如何使用 Entity 对象为索引和映射提供动态名称。我当前的实现是在记住单一索引的情况下完成的。请找到我用来创建索引的以下代码
elasticsearchTemplate.createIndex(SingleChat.class);
elasticsearchTemplate.putMapping(SingleChat.class);
elasticsearchTemplate.refresh(SingleChat.class, true);
And SingleChat is my Entity Class
SingleChat 是我的实体类
@Document(indexName="singlemsgtemp_#{jobParameters['MONTH']}",type="singlechat")
public class SingleChat {
@org.springframework.data.annotation.Id
String Id;
@Field(type = FieldType.String)
String conservationId;
@Field(type = FieldType.String)
String from;
@Field(type = FieldType.String)
String to;
@Field(type = FieldType.String)
String msgContent;
@Field(type = FieldType.String)
String sessionId;
@Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store = true, format = DateFormat.date_hour_minute_second_millis)
Date postedDate;
@Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store = true, format = DateFormat.date_hour_minute_second_millis)
Date expireDate;
}
But this is not working as expected. I am trying out some other things also. But i am open for suggestion. Also feel free to comment on my current approach, will it work or not. Please let me know if any more details are required.
但这并没有按预期工作。我也在尝试其他一些东西。但我愿意接受建议。也可以随意评论我目前的方法,它是否有效。如果需要更多详细信息,请告诉我。
采纳答案by Thiago
What I am doing on my application is that I use ElasticSearchTemplate to create my dynamic index name and then I point an alias to the new index I have created and then drop the old one.
我在我的应用程序上所做的是使用 ElasticSearchTemplate 创建我的动态索引名称,然后我将别名指向我创建的新索引,然后删除旧索引。
esTemplate.createIndex(newIndexName, loadfromFromFile(settingsFileName));
esTemplate.putMapping(newIndexName, "MYTYPE", loadfromFromFile(mappingFileName));
I'm not using the mapping and settings from my class since I need it to be dynamic.
我没有使用我班级的映射和设置,因为我需要它是动态的。
protected String loadFromFile(String fileName) throws IllegalStateException {
StringBuilder buffer = new StringBuilder(2048);
try {
InputStream is = getClass().getResourceAsStream(fileName);
LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));
while (reader.ready()) {
buffer.append(reader.readLine());
buffer.append(' ');
}
} catch (Exception e) {
throw new IllegalStateException("couldn't load file " + fileName, e);
}
return buffer.toString();
}
回答by Opster Elasticsearch Pro-Vijay
What I am doing in my project , we are storing index name and type name in DB whenever it gets changed .
我在我的项目中所做的,我们在数据库中存储索引名称和类型名称,只要它发生变化。
Now i m getting index and type Dynamically in this manner:
现在我以这种方式动态获取索引和类型:
First Solution
第一个解决方案
1)In configuration file ,create a Bean returning a value for someProperty . Here I injected the somePropertyValue with @Value annotation from DB or property file :-
@Value("${config.somePropertyValue}") private String somePropertyValue; @Bean public String somePropertyValue(){ return somePropertyValue; }
2)After this , it is possible to inject the somePropertyValue into the @Document annotation like this :-
@Document(index = "#{@somePropertyValue}") public class Foobar { //... }
1)在配置文件中,创建一个 Bean 返回 someProperty 的值。在这里,我从数据库或属性文件中注入了带有 @Value 注释的 somePropertyValue :-
@Value("${config.somePropertyValue}") private String somePropertyValue; @Bean public String somePropertyValue(){ return somePropertyValue; }
2)在此之后,可以像这样将 somePropertyValue 注入@Document 注释中:-
@Document(index = "#{@somePropertyValue}") public class Foobar { //... }
Second Solution
第二种解决方案
1) create getter setter in bean :-
@Component public class config{ @Value("${config.somePropertyValue}") private String somePropertyValue; public String getSomePropertyValue() { return somePropertyValue; } public void setSomePropertyValue(String somePropertyValue) { this.somePropertyValue = somePropertyValue; } }
2)After this , it is possible to inject the somePropertyValue into the @Document annotation like this :-
@Document(index = "#{config.somePropertyValue}") public class Foobar { //... }
1)在bean中创建getter setter:-
@Component public class config{ @Value("${config.somePropertyValue}") private String somePropertyValue; public String getSomePropertyValue() { return somePropertyValue; } public void setSomePropertyValue(String somePropertyValue) { this.somePropertyValue = somePropertyValue; } }
2)在此之后,可以像这样将 somePropertyValue 注入@Document 注释中:-
@Document(index = "#{config.somePropertyValue}") public class Foobar { //... }