Collapse results based on the maximum or mininum value

Kenneth Gabriel

I have data in this format.

[
    {
        lead_id: "lead_1",
        value: 34
    },
    {
        lead_id: "lead_1",
        value: 22
    },
    {
        lead_id: "lead_1",
        value: 67
    },
    {
        lead_id: "lead_2",
        value: 12
    },
    {
        lead_id: "lead_2",
        value: 9
    },
    {
        lead_id: "lead_3",
        value: 22
    },
    {
        lead_id: "lead_3",
        value: 5
    }
]

And I want to sort the leads based on the value in desc or asc order. Also, I need to collapse the result. I used a query like this for the sort.

{
    "collapse": {
        "field": "lead_id"
    },
    "sort": {
        "value": { "order": "asc" }
    }
}

But I want to sort base on the maximum value. i.e if I sort in asc I want the results to be:

[
    {
        lead_id: "lead_2",
        value: 12
    },
    {
        lead_id: "lead_3",
        value: 22
    },
    {
        lead_id: "lead_1",
        value: 67
    }
]

and for descending

[
    {
        lead_id: "lead_1",
        value: 67
    },
    {
        lead_id: "lead_3",
        value: 22
    },
    {
        lead_id: "lead_2",
        value: 12
    }
]
jaspreet chahal

collapse_sort as a feature is not available yet. There is an open PR so it might be available in future.

Work around is to use inner_hits

{
  "collapse": {
    "field": "lead_id.keyword",
    "inner_hits": {
      "name": "most_recent",                  
      "size": 1,                              
      "sort": [ { "value": "desc" } ]    
    }
  },
  "sort": {
    "value": {
      "order": "asc"
    }
  }
}

Result

"hits" : [
      {
        "_index" : "index11",
        "_type" : "_doc",
        "_id" : "xAugp4MBfEI_j4sNVw3V",
        "_score" : null,
        "_source" : {
          "lead_id" : "lead_3",
          "value" : 5
        },
        "fields" : {
          "lead_id.keyword" : [
            "lead_3"
          ]
        },
        "sort" : [
          5
        ],
        "inner_hits" : {  --> group sorted values
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "index11",
                  "_type" : "_doc",
                  "_id" : "wwugp4MBfEI_j4sNTw1c",
                  "_score" : null,
                  "_source" : {
                    "lead_id" : "lead_3",
                    "value" : 22
                  },
                  "sort" : [
                    22
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "index11",
        "_type" : "_doc",
        "_id" : "wgugp4MBfEI_j4sNSA00",
        "_score" : null,
        "_source" : {
          "lead_id" : "lead_2",
          "value" : 9
        },
        "fields" : {
          "lead_id.keyword" : [
            "lead_2"
          ]
        },
        "sort" : [
          9
        ],
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "index11",
                  "_type" : "_doc",
                  "_id" : "wQugp4MBfEI_j4sNQg3t",
                  "_score" : null,
                  "_source" : {
                    "lead_id" : "lead_2",
                    "value" : 12
                  },
                  "sort" : [
                    12
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "index11",
        "_type" : "_doc",
        "_id" : "vwugp4MBfEI_j4sNNw0q",
        "_score" : null,
        "_source" : {
          "lead_id" : "lead_1",
          "value" : 22
        },
        "fields" : {
          "lead_id.keyword" : [
            "lead_1"
          ]
        },
        "sort" : [
          22
        ],
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 3,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "index11",
                  "_type" : "_doc",
                  "_id" : "wAugp4MBfEI_j4sNPQ3F",
                  "_score" : null,
                  "_source" : {
                    "lead_id" : "lead_1",
                    "value" : 67
                  },
                  "sort" : [
                    67
                  ]
                }
              ]
            }
          }
        }
      }
    ]

Parent document will still have values sorted as per general sort. Group sort values can be picked from inner hits

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Results + amount blanc rows based on field value

Filter results in angularjs based on select value

Set maximum sliderInput value based on reactive output value

Sum the value of one column based on the maximum value of another column

How to find the maximum value based on another column?

How to select the maximum value of a column and retrieve multiple results?

Numpy get maximum value based on XYZ

Mininum 1 char, Minimum 4 char, Maximum 50 char

Ordering MySQL query results based on an id value

SQLite - Match results based on string value issue

Top n results based on value and ASCII sorting

Get maximum value based on unique values

Getting search results not based on a whole indexed value

Return column value based on results of WHERE clause

filter json results based on value with url

bootstrap toogle between 'collapse' and 'collapse in' based on device

Extract rolling maximum value based on column value

Oracle: prioritizing results based on column’s value

Projection to collapse Sub-Document based on value

SQL displaying results based on a value in column

Changing the value of a cell in excel based on results in a range

Renaming a column entry, when it is the maximum value by group, gives inconsistent results

restricting rows based on first encounter of maximum value

Order descending based on a maximum value of a group, and then remove the maximum column in dplyr

How to replace value in a column based on maximum value in same column in Pyspark?

mininum value of a resample (not 0)

Collapse rows based on another column duplicate value in R

Calculate the value in a column based on JOIN results?

SQL query to limit results based on Priority value