Home > Blockchain >  Get members in aggregation bucket ElasticSearch
Get members in aggregation bucket ElasticSearch

Time:06-24

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

CodePudding user response:

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?

GET teste/_search
{
  "query": {
    "term": {
      "state": {
        "value": "NY"
      }
    }
  },
  "collapse": {
    "field": "parentRequestId"
  }
}
  • Related