# Math operation on different documents/queries

**URL:** <https://discuss.elastic.co/t/math-operation-on-different-documents-queries/320875>\
**Category:** Kibana\
**Tags:** lens\
**Created:** [December 9, 2022, 11:15am UTC](https://discuss.elastic.co/t/math-operation-on-different-documents-queries/320875 "2022-12-09T11:15:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Frankiewicz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_frankiewicz/32/97650_2.png) [@Marcin\_Frankiewicz](https://discuss.elastic.co/u/Marcin_Frankiewicz)\
**Post date:** [December 9, 2022, 11:15am UTC](https://discuss.elastic.co/t/math-operation-on-different-documents-queries/320875/1 "2022-12-09T11:15:31Z")

</div>

Hi,

I'm trying to create metric in Kibana dashboard(Lens), where i will see difference between two different fields (events\_passed **-** events\_all) (subtraction)

That fields are available in two different documents

Can i make some aggregation? to calculate difference?

I'm able to get required fields by two queries

events\_all :

```auto
{
  "_source": ["events_all"], 
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "format": "strict_date_optional_time",
              "gte": "now-15m",
              "lte": "now"
            }
          }
        },
        {
          "match_phrase": {
            "app_name": "app1"
          }
        },
        {
          "match_phrase": {
            "imsi_prefix": "00002"
          }
        }
      ]
    }
  }
}

```

events\_passed

```auto
{
  "size": 1, 
  "_source": ["events_passed"], 
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "format": "strict_date_optional_time",
              "gte": "now-15m",
              "lte": "now"
            }
          }
        },
        {
          "match_phrase": {
            "app_name": "app1"
          }
        },
        {
          "exists": {
            "field": "events_passed"
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}

```

Sample docs

```auto
{
  "_index": "app1_log-001745",
  "_type": "_doc",
  "_id": "KKha9oQBIhZIlNHKrDlX",
  "_version": 1,
  "_score": 1,
  "_source": {
    "app_name": "app1",
    "events_all": "14928046",
    "@version": "1",
    "executed_time": 7.331166666666666,
    "events_rejected": "5869684",
    "events_passed": "9058362", - Field1
    "log_level": "INFO"
  }
}

{
  "_index": "app_log-001745",
  "_type": "_doc",
  "_id": "7C6R9oQBIhZIlNHK1xU7",
  "_version": 1,
  "_score": 1,
  "_source": {
    "imsi_prefix": "00002",
    "log_level": "DEBUG",
    "app_name": "app1",
    "events_all": "8252891", - Field2
    "@version": "1",
  }
}

```

Expected result : 9058362(Field1) - 8252891(Field2) = 805471

How can I deal with it ?  
Elasticsearch/Kibana 7.16

Regards

---

<div class="post-metadata">

**Author:** ![Marcin\_Frankiewicz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_frankiewicz/32/97650_2.png) [@Marcin\_Frankiewicz](https://discuss.elastic.co/u/Marcin_Frankiewicz)\
**Post date:** [December 14, 2022, 12:25pm UTC](https://discuss.elastic.co/t/math-operation-on-different-documents-queries/320875/2 "2022-12-14T12:25:31Z")

</div>

Anyone can help?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 11, 2023, 12:25pm UTC](https://discuss.elastic.co/t/math-operation-on-different-documents-queries/320875/3 "2023-01-11T12:25:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
