我该如何在运行时将SpringData ElasticSearch索引参数化?
例如,数据模型:
@Document(indexName = "myIndex")
public class Asset {
@Id
public String id;
// ...
}
还有代码库:
public interface AssetRepository extends ElasticsearchCrudRepository<Asset, String> {
Asset getAssetById(String assetId);
}
我知道我可以用参数替换myIndex
,但这个参数将在实例化/启动期间解析。我们为多个客户/租户提供相同的资产结构,它们都有自己的索引。我需要的是像这样的东西:
public interface AssetRepository extends ElasticsearchCrudRepository<Asset, String> {
Asset getAssetByIdFromIndex(String assetId, String index);
}
或者这个
repoInstance.forIndex("myOtherIndex").getAssetById("123");
我知道这个功能不能直接使用,但有没有编程上的“hack”方法可以解决呢?