让成员加入聚合库Elasticsearch
我是Elasticsearch的新手,
我有一个名为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中),该查询(在Java中)返回每个状态的所有请求列表。请求类看起来像这样:
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)
我该如何实现? 谢谢
I'm pretty new to elasticsearch
I have a class called SubRequest
Mapping:
{
"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"
}
}
}
}
Example:
{
"_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 class:
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;
}
I have to write a query (in Java) that returns a list of all requests, per state. The Request class looks like this:
public class Request {
private String identifier;
private State state;
private String requesterId;
}
The issue is that what I'm saving in my ES database are the sub-requests, I can have any number of sub-requests that are essentially one parent request. What I have to return is a list of the parent requests.
All sub-requests of the same parent will have the same parentId, state, and requesterId.
I wrote a query with an aggregation that groups the sub-requests by parentId, but that just gives me buckets equal to the number of parent requests. I need to somehow access the data that made up those buckets to create one "Request" instance, per bucket, (with the data that is the same for all the sub-requests of that same bucket)
To illustrate with an example, if I have 3 Sub-Requests in my db:
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)
And I am requested to return all the requests for the state of Florida, I would need to return a list of:
Request1 -> (identifier=123, state=FL, requesterId = yuiw_789)
Request2 -> (identifier=345, state=FL, requesterId = kdls_543)
how can i achieve this?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以自己为例,我相信其他3个文档。
两个“ parentRequestid”:“ 674_6767”和一个“ parentRequestid”:“ 674_6768”。
我使用 collapse 。
那是你需要的吗?
I used give yourself as an example and I believe 3 other docs.
Two "parentRequestId" : "674_6767" and one "parentRequestId" : "674_6768".
To group I used collapse.
would that be what you need?