我對彈性搜索很陌生
我有一個名為 SubRequest 的類
映射:
{
"sub-request" : {
"aliases" : { },
"mappings" : {
"properties" : {
"_class" : {
"type" : "keyword",
"index" : false,
"doc_values" : false
},
"createdAt" : {
"type" : "date",
"format" : "date_hour_minute_second_millis"
},
"parentRequestId" : {
"type" : "keyword"
},
"platform" : {
"type" : "keyword"
},
"requesterId" : {
"type" : "keyword"
},
"state" : {
"type" : "keyword"
},
"subRequestId" : {
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1655214424891",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "X15112-5QTw",
"version" : {
"created" : "7100299"
},
"provided_name" : "sub-request"
}
}
}
}
例子:
{
"_index" : "sub-request",
"_type" : "_doc",
"_id" : "13",
"_score" : 0.0,
"_source" : {
"_class" : "********.model.SubRequest",
"subRequestId" : "13",
"parentRequestId" : "674_6767",
"state" : "NY",
"requesterId" : "5489_uiyd",
"platform" : "GCX",
"createdAt" : "2022-06-14T11:27:32.092"
}
}
Java類:
public class SubRequest implements Persistable<String> {
public static final String INDEX = "sub-request";
@Id
@EqualsAndHashCode.Include
@Field(type = FieldType.Keyword)
private String id;
@Field(type = FieldType.Keyword)
private String parentId;
@Field(type = FieldType.Keyword)
private State state;
@Field(type = FieldType.Keyword)
private String requesterId;
}
我必須撰寫一個查詢(在 Java 中),它回傳每個狀態的所有請求的串列。Request 類如下所示:
public class Request {
private String identifier;
private State state;
private String requesterId;
}
問題是我保存在我的 ES 資料庫中的是子請求,我可以有任意數量的子請求,這些子請求本質上是一個父請求。我必須回傳的是父請求串列。
同一父節點的所有子請求將具有相同的 parentId、state 和 requesterId。
我撰寫了一個聚合查詢,該聚合按 parentId 對子請求進行分組,但這只是給了我等于父請求數量的存盤桶。我需要以某種方式訪問??構成這些存盤桶的資料,以便為每個存盤桶創建一個“請求”實體(對于同一個存盤桶的所有子請求,資料都是相同的)
舉個例子,如果我的資料庫中有 3 個子請求:
SR1-> (id=1, parentId=123, state=FL, requesterId = yuiw_789)
SR2-> (id=2, parentId=123, state=FL, requesterId = yuiw_789)
SR3-> (id=3, parentId=345, state=FL, requesterId = kdls_543)
我被要求回傳佛羅里達州的所有請求,我需要回傳一個串列:
Request1 -> (identifier=123, state=FL, requesterId = yuiw_789)
Request2 -> (identifier=345, state=FL, requesterId = kdls_543)
我怎樣才能做到這一點?謝謝
uj5u.com熱心網友回復:
我以自己為例,我相信其他 3 個檔案。兩個“parentRequestId”:“674_6767”和一個“parentRequestId”:“674_6768”。分組我使用了 collapse。
那是你需要的嗎?
GET teste/_search
{
"query": {
"term": {
"state": {
"value": "NY"
}
}
},
"collapse": {
"field": "parentRequestId"
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/495382.html
