Show in Kibana a Metric with the output of an arbitrary elasticsearch query

I want to display in Kibana a metric visualization (a.k.a. a number) whose value computed with a pipeline aggregation ES query.

More concretely the query is built based on this one: https://stackoverflow.com/questions/46908360/sql-like-group-by-and-having/46953947#46953947

This is the output of the query (truncated):

 {...,
    "aggregations": {
        "reseller_sale_sum": {
           "doc_count_error_upper_bound": 0,
           "sum_other_doc_count": 0,
           "buckets": [
              ...
           ]
         },
         "max_sales_stats": {
         "value": 1
       }
    }
 }

And what I would like to display the value of max_sales_stats in a Kibana Metric visualization.

Is there a way of achieving this, even outside of the GUI (ie, directly editing a saved object)?

We don't really have a robust query building mechanism in our standard metric visualization.

It's not clear to me exactly what you're trying to build, but it sounds like something you can probably use TSVB to build.

As a last resort, generally speaking, you can run almost any Elasticsearch query in Vega. Vega can be a bit tricky (you've got to understand Vega, Elasticsearch, and the way Kibana uses Vega).

Hi Chris, thanks your answer.

I will describe you the concrete use case: We have applicants for jobs. Some applicants may apply for more than one job within our system, let's call them "recurring applicants" (as "recurring customers"). We would like to display not just the count of unique applicants (as we do using a Unique count), but also the unique count of recurring applicants. I have an ES query that yields this number, but there's no way for me to display it in Kibana (using a Metric visualization).

On the other hand, I imagine that Vega is flexible enough to produce it, but then I have understood that Kibana filters won't apply in the Vega plots.

I have looked at TSVB visualization but I was not obvious for me how to proceed with it. Would you have a more concrete suggestion.

If my understanding is right, this is not yet possible, but because of entirely understandable reasons, there's a plan of allowing something similar in the future:

Quoting from that blog:

We also see saved queries as a building block for some other powerful features. Imagine being able to create filters in the filter bar based on saved queries, giving us an easy way to combine existing queries. In a similar vein, we could develop a syntax for referencing saved queries inside a KQL expression. That would allow for effortless reuse of saved queries in places where a query text input is available without the full search bar, like the filters aggregation in Visualize. Having a way to save a portable set of filters in Kibana will open up all kinds of possibilities.