Merge document by condition

Hi,
I have document :
{"match_id": xxx, userid: yyy, "bonus": zzz}

I want to result like:
{"match_id" : xxx, users: [{"userid": user1, bonos: bonus1}, {"userid": user2, bonos: bonus2}]}

Please help me.
Thank you .

anyone please help me.

Welcome!

Please be patient in waiting for responses to your question and refrain from pinging multiple times asking for a response or opening multiple topics for the same question. This is a community forum, it may take time for someone to reply to your question. For more information please refer to the Community Code of Conduct specifically the section "Be patient". Also, please refrain from pinging folks directly, this is a forum and anyone that participates might be able to assist you.

If you are in need of a service with an SLA that covers response times for questions then you may want to consider talking to us about a subscription.

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

It's hard to say without a concrete example of the documents you have.

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

But anyway, may be top hits is what you want to have?

Hi,
thank you for fast reply.

My document:

"hits" : [
      {
        "_index" : "test",
        "_type" : "match",
        "_id" : "43UAXngB2RLYKVaHmQ9G",
        "_score" : 1.0,
        "_source" : {
          "match_id" : 1,
          "user_id" : 1,
          "score" : 10
        }
      },
      {
        "_index" : "test",
        "_type" : "match",
        "_id" : "TXUAXngB2RLYKVaHxxTD",
        "_score" : 1.0,
        "_source" : {
          "match_id" : 1,
          "user_id" : 2,
          "score" : 20
        }
      },
      {
        "_index" : "test",
        "_type" : "match",
        "_id" : "03UAXngB2RLYKVaH_Rml",
        "_score" : 1.0,
        "_source" : {
          "match_id" : 2,
          "user_id" : 1,
          "score" : 100
        }
      },
      {
        "_index" : "test",
        "_type" : "match",
        "_id" : "vnUBXngB2RLYKVaHHBz4",
        "_score" : 1.0,
        "_source" : {
          "match_id" : 2,
          "user_id" : 2,
          "score" : 200
        }
      },
  {
    "_index" : "test",
    "_type" : "match",
    "_id" : "B3cYXngB2RLYKVaHR1X3",
    "_score" : 1.0,
    "_source" : {
      "match_id" : 3,
      "user_id" : 2,
      "score" : 1000
    }
  }
    ]

Business logic is find the match which a special user played .
The match data must contain all user data

Request using top_hits is as below :

GET /test/match/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "matchs": {
      "terms": {
        "field": "match_id"
      },
      "aggs": {
        "match_userid": {
          "terms": {
            "field": "user_id"
          },
          "aggs": {
            "same_match": {
              "top_hits": {
                "size": 100
              }
            }
          }
        }
      }
    }
  }
}

Response is:

   "matchs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 2,
          "match_userid" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : 1,
                "doc_count" : 1,
                "same_match" : {
                  "hits" : {
                    "total" : 1,
                    "max_score" : 1.0,
                    "hits" : [
                      {
                        "_index" : "test",
                        "_type" : "match",
                        "_id" : "43UAXngB2RLYKVaHmQ9G",
                        "_score" : 1.0,
                        "_source" : {
                          "match_id" : 1,
                          "user_id" : 1,
                          "score" : 10
                        }
                      }
                    ]
                  }
                }
              },
              {
                "key" : 2,
                "doc_count" : 1,
                "same_match" : {
                  "hits" : {
                    "total" : 1,
                    "max_score" : 1.0,
                    "hits" : [
                      {
                        "_index" : "test",
                        "_type" : "match",
                        "_id" : "TXUAXngB2RLYKVaHxxTD",
                        "_score" : 1.0,
                        "_source" : {
                          "match_id" : 1,
                          "user_id" : 2,
                          "score" : 20
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        },
        {
          "key" : 2,
          "doc_count" : 2,
          "match_userid" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : 1,
                "doc_count" : 1,
                "same_match" : {
                  "hits" : {
                    "total" : 1,
                    "max_score" : 1.0,
                    "hits" : [
                      {
                        "_index" : "test",
                        "_type" : "match",
                        "_id" : "03UAXngB2RLYKVaH_Rml",
                        "_score" : 1.0,
                        "_source" : {
                          "match_id" : 2,
                          "user_id" : 1,
                          "score" : 100
                        }
                      }
                    ]
                  }
                }
              },
              {
                "key" : 2,
                "doc_count" : 1,
                "same_match" : {
                  "hits" : {
                    "total" : 1,
                    "max_score" : 1.0,
                    "hits" : [
                      {
                        "_index" : "test",
                        "_type" : "match",
                        "_id" : "vnUBXngB2RLYKVaHHBz4",
                        "_score" : 1.0,
                        "_source" : {
                          "match_id" : 2,
                          "user_id" : 2,
                          "score" : 200
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        },

Expected

"hits" : [
{
    "match_id": 1
    "_source" : {
        "data": [{ "user_id" : 1,"score" : 10}, { "user_id" : 2,"score" : 20}]
    },
{
        "match_id": 2
        "_source" : {
            "data": [{ "user_id" : 1,"score" : 100}, { "user_id" : 2,"score" : 200}]
        }
    ]

The result of the aggregation sounds super close at least in term of data to what you are looking for. I guess you just have to transform the json then in your application to extract the meaningful data, right?

You can use filter_path if you want to filter a bit the json but I'd do that in the application instead. See Common options | Elasticsearch Reference [7.11] | Elastic

Thanks for the suggestion.