How to configure the percolator to return when the aggregation value reaches a certain threshold?

As an example, take the following aggregation request:

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "groupBy": {
      "terms": {
        "field": "CustomerName"
      },
      "aggs": {
        "points_sum": {
          "stats": {
            "field": "TransactionAmount"
          }
        }
      }
    }
  },
  "size": 0
}

I am interested to know when any CustomerName has an average TransactionAmount (stats.avg) that is above some threshold for all purchases of this customer, as soon as I index a document that would have my average above this threshold. It seems that the percolator is intended to match documents with rules, more or less, but I cannot find good examples of using percolators to comply with rules based on aggregation results.

Is it possible? Is this a better percolator? Is there any other / better solution? thanks in advance

+4
source share
1 answer

Watcher :

PUT _watcher/watch/transaction_alert
{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "search": {
      "request": {
        "indices": "transactions",
        "types": "transaction",
        "body": {
          "query": {
            "match_all": {}
          },
          "size": 0,
          "aggs": {
            "groupBy": {
              "terms": {
                "field": "CustomerName"
              },
              "aggs": {
                "points_sum": {
                  "stats": {
                    "field": "TransactionAmount"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "inline": "return ctx.payload.aggregations.groupBy.buckets.findAll{ cust -> cust.points_sum.avg >= 200}"
    }
  },
  "actions": {
    "send_email": { 
      "email": {
        "to": "<username>@<domainname>", 
        "subject": "Customer Notification - Transaction > 200",
        "body": "The attached customers have a transaction average above $200"
        "attachments" : {
           "data.yml" : {
              "data" : {
                 "format" : "yaml" 
              }
           }
        }
      }
    }
  }
}

UPDATE

:

  • Watcher - .
  • ElastAlert () , .

, Logstash. , elasticsearch , http_poller, Elasticsearch. , , , , , , - , email .

( , URL ES source=...). , , points_sum.avg (desc)

input {
  http_poller {
    urls => {
      test1 => 'http://localhost:9200/your-index/_search?source=%7B%22query%22%3A%7B%22match_all%22%3A%7B%7D%7D%2C%22aggs%22%3A%7B%22groupBy%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22CustomerName%22%2C%22order%22%3A%7B%22points_sum.avg%22%3A%22desc%22%7D%7D%2C%22aggs%22%3A%7B%22points_sum%22%3A%7B%22stats%22%3A%7B%22field%22%3A%22TransactionAmount%22%7D%7D%7D%7D%7D%2C%22size%22%3A0%7D'
   }
   # checking every 10 seconds
   interval => 10
   codec => "json"
  }
}
filter {
  split {
    field => "[aggregations][groupBy][buckets]" 
  }
}
output {
  if [aggregations][groupBy][buckets][points_sum][avg] > 200 {
    email {
      to => "<username>@<domainname>"
      subject => "Customer Notification - Transaction > 200",
      body => "The customer %{[aggregations][groupBy][buckets][key]} has a transaction average above $200"
    }
  }
}

, , , , , Logstash - ; -)

2

node.js elasticwatch.

+1

Source: https://habr.com/ru/post/1655713/


All Articles