如何编写查询以找到Elasticsearch中的百分比? [英] How to write query to find percentage in elasticsearch?

查看:710
本文介绍了如何编写查询以找到Elasticsearch中的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Elasticsearch中有数据.

这是我的实际文档 https://docs.google.com/document/d/1DKID90I9ulUcut-S8UfrnSjY-3citEwmyfnJJmrIRU8/edit?usp=sharing

doc:
{
  store_id:"abc",
  event_timestamp:"2019-06-05 13:00:05",
  event_type:"heartbeat"   
}

我在输入中有store_id,日期范围和事件类型.在输出中,我需要设备在该小时内在线的时间百分比.

这就是我们在线考虑设备的方式. 如果一个小时内store_id发生event ="heartbeat",则表示商店在线.

示例1.

因此,如果范围是从"2019-05-07"到"2019-05-08",并且有14个文档的小时数不同,则百分比将是(14/(2 * 24))* 100

示例2.

 doc:
{
  store_id:"abc",
  event_timestamp:"2019-06-05 13:00:05",
  event_type:"heartbeat"   
}

doc:
{
  store_id:"abc",
  event_timestamp:"2019-06-05 14:00:05",
  event_type:"heartbeat"   
}

doc:
{
  store_id:"abc",
  event_timestamp:"2019-06-05 14:00:05",
  event_type:"heartbeat"   
}

如果输入是store_id ="abc"和date_range ="2019-06-05"到" 2019-06-05和event_type =" heartbeat,则输出为(2/(1 * 24)),因为该商店的event = heartbeat只有两个不同的小时.

这是我对累计总和的查询.如果有一些如何将最终的累计总和除以日期之间的差异的话.

   GET /internship38/_search
{
  "query": 
  {
   "bool":
    {
      "must":
      [
        {
          "match" :
          {
            "attributes.store_id" : "41b15888-0c2f-48f9-89d0-dc7aad19f52b"
          }
        },
        {
          "match":
          {
            "event_type":"app_sent_heartbeat"
          }
        }
      ]
    }
  },


     "aggs":
  {
    "my_date_histo":{
      "date_histogram":{
        "field":"arrival_timestamp",
        "interval":"day"
      },
      "aggs":
      {
        "distinct_hours": {      
       "cardinality": {
        "script": {
          "lang": "painless",
          "source": "doc[params.date_field].value.hourOfDay;",
          "params": {
            "date_field": "arrival_timestamp"
          }
        }
      }

    },
    "cumulative_hours": {
                    "cumulative_sum": {
                        "buckets_path": "distinct_hours" 
                    }


      }

    }

}
}
} 

可以用Java完成吗?例如 https://www.programcreek.com /java-api-examples/?api=org.elasticsearch.script.Script

解决方案

来自链接:

{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "calendar_interval" : "month"
            },
            "aggs": {
                "total_sales": {
                    "sum": {
                        "field": "price"
                    }
                },
                "t-shirts": {
                  "filter": {
                    "term": {
                      "type": "t-shirt"
                    }
                  },
                  "aggs": {
                    "sales": {
                      "sum": {
                        "field": "price"
                      }
                    }
                  }
                },
                "t-shirt-percentage": {
                    "bucket_script": {
                        "buckets_path": {
                          "tShirtSales": "t-shirts>sales",
                          "totalSales": "total_sales"
                        },
                        "script": "params.tShirtSales / params.totalSales * 100"
                    }
                }
            }
        }
    }
}

I have data in elasticsearch.

this is my actual doc https://docs.google.com/document/d/1DKID90I9ulUcut-S8UfrnSjY-3citEwmyfnJJmrIRU8/edit?usp=sharing

doc:
{
  store_id:"abc",
  event_timestamp:"2019-06-05 13:00:05",
  event_type:"heartbeat"   
}

I have store_id, range of dates and event type in the input.in output, I need the percentage amount of time device was online for that hour.

This is how we consider device online. If there is an event="heartbeat" for a store_id in an hour then we say the store is online.

example 1.

so if the range is of "2019-05-07" to "2019-05-08" and there are 14 docs with different hour then the percentage will be (14/(2*24))*100

example 2.

 doc:
{
  store_id:"abc",
  event_timestamp:"2019-06-05 13:00:05",
  event_type:"heartbeat"   
}

doc:
{
  store_id:"abc",
  event_timestamp:"2019-06-05 14:00:05",
  event_type:"heartbeat"   
}

doc:
{
  store_id:"abc",
  event_timestamp:"2019-06-05 14:00:05",
  event_type:"heartbeat"   
}

if input was store_id="abc" and date_range="2019-06-05" to ""2019-06-05" and event_type="heartbeat" then output would be (2/(1*24)) because there are only two different hour with event=heartbeat of that store.

this is my query for the cumulative sum.If some How I can divide the final cumulative sum with difference between dates.

   GET /internship38/_search
{
  "query": 
  {
   "bool":
    {
      "must":
      [
        {
          "match" :
          {
            "attributes.store_id" : "41b15888-0c2f-48f9-89d0-dc7aad19f52b"
          }
        },
        {
          "match":
          {
            "event_type":"app_sent_heartbeat"
          }
        }
      ]
    }
  },


     "aggs":
  {
    "my_date_histo":{
      "date_histogram":{
        "field":"arrival_timestamp",
        "interval":"day"
      },
      "aggs":
      {
        "distinct_hours": {      
       "cardinality": {
        "script": {
          "lang": "painless",
          "source": "doc[params.date_field].value.hourOfDay;",
          "params": {
            "date_field": "arrival_timestamp"
          }
        }
      }

    },
    "cumulative_hours": {
                    "cumulative_sum": {
                        "buckets_path": "distinct_hours" 
                    }


      }

    }

}
}
} 

Can It be done in java? for example https://www.programcreek.com/java-api-examples/?api=org.elasticsearch.script.Script

解决方案

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html

Above link in the elasticsearch documentation would help if you can reformat your query into "buckets" using the "aggs" functionality.

from link:

{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "calendar_interval" : "month"
            },
            "aggs": {
                "total_sales": {
                    "sum": {
                        "field": "price"
                    }
                },
                "t-shirts": {
                  "filter": {
                    "term": {
                      "type": "t-shirt"
                    }
                  },
                  "aggs": {
                    "sales": {
                      "sum": {
                        "field": "price"
                      }
                    }
                  }
                },
                "t-shirt-percentage": {
                    "bucket_script": {
                        "buckets_path": {
                          "tShirtSales": "t-shirts>sales",
                          "totalSales": "total_sales"
                        },
                        "script": "params.tShirtSales / params.totalSales * 100"
                    }
                }
            }
        }
    }
}

这篇关于如何编写查询以找到Elasticsearch中的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆