sag*_*r27 9 java spring spring-batch elasticsearch spring-data-elasticsearch
我有一个用例,需要在Elasticsearch中每月创建索引.我们的想法是在每月的基础上创建索引,以便它们易于维护,并且可以在过期时删除.为此我实现了我已经使用了弹簧批次并且每月工作将创建月度基础上的指数对于Elasticsearch -Java集成我使用了Spring-Data Elasticsearch实现.我现在面临的问题是,我无法弄清楚如何使用Entity对象为索引和映射提供动态名称.我的当前实现完成了单一索引.请找到我用于创建索引的以下代码
elasticsearchTemplate.createIndex(SingleChat.class);
elasticsearchTemplate.putMapping(SingleChat.class);
elasticsearchTemplate.refresh(SingleChat.class, true);
Run Code Online (Sandbox Code Playgroud)
而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;
}
Run Code Online (Sandbox Code Playgroud)
但这不符合预期.我也在尝试其他一些东西.但我愿意接受建议.也可以随意评论我目前的方法,是否有效.如果需要更多详细信息,请与我们联系.
Vij*_*pta 10
我在我的项目中所做的是,每当数据库发生更改时,我们都会在数据库中存储索引名称和类型名称。
现在我以这种方式动态获取索引和类型:
1)在配置文件中,创建一个 Bean 返回 someProperty 的值。在这里,我从 DB 或属性文件中注入了带有 @Value 注释的 somePropertyValue :-
@Value("${config.somePropertyValue}")
private String somePropertyValue;
@Bean
public String somePropertyValue(){
return somePropertyValue;
}
Run Code Online (Sandbox Code Playgroud)2)在此之后,可以像这样将 somePropertyValue 注入@Document 注释中:-
@Document(index = "#{@somePropertyValue}")
public class Foobar {
//...
}
Run Code Online (Sandbox Code Playgroud)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;
}
}
Run Code Online (Sandbox Code Playgroud)2)在此之后,可以像这样将 somePropertyValue 注入@Document 注释中:-
@Document(index = "#{config.somePropertyValue}")
public class Foobar {
//...
}
Run Code Online (Sandbox Code Playgroud)我在我的应用程序上所做的是使用 ElasticSearchTemplate 创建我的动态索引名称,然后我将别名指向我创建的新索引,然后删除旧索引。
esTemplate.createIndex(newIndexName, loadfromFromFile(settingsFileName));
esTemplate.putMapping(newIndexName, "MYTYPE", loadfromFromFile(mappingFileName));
Run Code Online (Sandbox Code Playgroud)
我没有使用我班级的映射和设置,因为我需要它是动态的。
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9717 次 |
| 最近记录: |