Ulf
Ulf

Reputation: 37

Using hops and join against datasets, how to get the highest value of a certain property. DTL

I'm running hops against a dataset in sesam and my output is as the example below, my problem is that need a join where i only end up with the property with the highest priority.

[{
    "id": "AAA",
    "priority": 10
  }, {
    "id": "AAB",
    "priority": 7
  }]

Upvotes: 1

Views: 60

Answers (2)

Geir Ove Grønmo
Geir Ove Grønmo

Reputation: 61

You can also solve this with the "max" function:

{
  "_id": "sort-by-prop",
  "type": "pipe",
  "source": {
    "type": "embedded",
    "entities": [{
      "_id": "foo",
      "bar": [{
        "id": "AAA",
        "priority": 10
      }, {
        "id": "AAB",
        "priority": 5
      }]
    }]
  },
  "transform": {
    "type": "dtl",
    "rules": {
      "default": [
        ["copy", "_id"],
        ["add", "foo",
          ["path", "id",
            ["max", "_.priority", "_S.bar"]
          ]
        ]
      ]
    }
  }
}

The result will be:

[
  {
    "_id": "foo",
    "foo": "AAA"
  }
]

Upvotes: 2

Baard
Baard

Reputation: 375

You can solve this using "sorted", "reverse", "first" and "path":

{
  "_id": "sort-by-prop",
  "type": "pipe",
  "source": {
    "type": "embedded",
    "entities": [{
      "_id": "foo",
      "bar": [{
        "id": "AAA",
        "priority": 10
      }, {
        "id": "AAB",
        "priority": 5
      }]
    }]
  },
  "transform": {
    "type": "dtl",
    "rules": {
      "default": [
        ["copy", "_id"],
        ["add", "foo",
          ["path", "id",
            ["first",
              ["reversed",
                ["sorted", "_.priority", "_S.bar"]
              ]
            ]
          ]
        ]
      ]
    }
  }
}

The result will be:

[
  {
    "_id": "foo",
    "foo": "AAA"
  }
]

Upvotes: 1

Related Questions