[英]Can we avoid mapping all the fields to entity class in springdata for elasticsearch, as I have more than 100 field in the json document?
我正在使用 springdata API 为 elasticsearch (es) 操作实现 spring-boot 微服务。 我在 es 中索引的文档很大,有多个字段(超过 100 个)。
有没有办法避免在java中为elasticsearch对象定义/硬编码实体类中的所有字段?
我的示例患者 JSON 可能是这样的:
{
"key_1":"value_1",
"key_2":"value_2",
"key_3":"value_3",
.
.
.
"key_n":"value_n"
}
如果您可以使用稍微不同的形式将数据存储在 Elastcisearch 中,则可以使用以下内容:
如下定义您的实体(为了简洁,我省略了 getter/setter):
@Document(indexName = "innerdata")
public class InnerDataEntity {
@Id
private String id;
@Field(type = FieldType.Object)
private List<InnerData> innerData;
static class InnerData {
@Field(type = FieldType.Text)
private String key;
private String value;
}
}
将此与 Spring Data Elasticsearch Repository 一起使用将创建具有以下映射的索引:
{
"innerdata": {
"mappings": {
"properties": {
"id": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"innerData": {
"properties": {
"key": {
"type": "text"
}
}
}
}
}
}
}
value
属性是自动映射的,如果您还需要搜索它们,请添加FieldType.Text
。
然后,您可以通过定义这样的存储库来使用键值进行搜索:
public interface InnerDataRepository extends ElasticsearchRepository<InnerDataEntity, String> {
SearchHits<InnerDataEntity> searchByInnerData_Key(String key);
}
并在例如控制器中使用它:
@GetMapping("/{key}")
public SearchHits<InnerDataEntity> allByKey(@PathVariable String key) {
return repository.searchByInnerData_Key(key);
}
Elasticsearch 中存储的数据如下所示:
{
"hits": {
"hits": [
{
"_id": "1",
"_index": "innerdata",
"_score": 1.0,
"_source": {
"id": "1",
"innerData": [
{
"key": "key-1",
"value": "value-1"
},
{
"key": "key-2",
"value": "value-2"
}
]
},
"_type": "_doc"
},
{
"_id": "2",
"_index": "innerdata",
"_score": 1.0,
"_source": {
"id": "2",
"innerData": [
{
"key": "key-1",
"value": "value-1"
},
{
"key": "key-3",
"value": "value-3"
}
]
},
"_type": "_doc"
}
]
}
}
}
感谢“@PJMeisch”,为我指明了正确的方向!
我通过执行文档 (json) 重组解决了这个问题,这将在 elasticsearch 中编入索引。 这是通过将匿名患者 ID 保留在根级别并将其余患者详细信息保留在子 json 中来完成的。 下面是我的模型类。
@Component
@Document(indexName = "manual_patients") // name must be lower case
@Getter
@Setter
@ToString(callSuper=true, includeFieldNames=true)
// @NoArgsConstructor
public class ManualPatient {
@Id
private String _id = UUID.randomUUID().toString();
@ApiModelProperty(position = 0)
private String patientId;
private Map patientDetails;
public ManualPatient(){}
public ManualPatient( String patientId, Map patientDetails) {
this.patientId = patientId;
this.patientDetails = patientDetails;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.