elasticsearch 如何计算Elasticsearch中匹配文档的字段?
这是我的示例文档:elasticsearch 如何计算Elasticsearch中匹配文档的字段?,elasticsearch,elasticsearch,这是我的示例文档: { "jobID": "ace4c888-1907-4021-a808-4a816e99aa2e", "startTime": 1415255164835, "endTime": 1415255164898, "moduleCode": "STARTING_MODULE" } 我有成千上万的文件 我有一对作业ID相同的文档,模块代码是开始模块和结束模块 我的公式是ENDING_MODULE endTime减去STARTING_MODULE
{
"jobID": "ace4c888-1907-4021-a808-4a816e99aa2e",
"startTime": 1415255164835,
"endTime": 1415255164898,
"moduleCode": "STARTING_MODULE"
}
- 我有成千上万的文件
- 我有一对作业ID相同的文档,模块代码是开始模块和结束模块
- 我的公式是ENDING_MODULE endTime减去STARTING_MODULE startTime等于模块处理所用的时间
请让我知道这是否需要更多的澄清。谢谢 试试下面的方法,可能不太理想,但它会返回一个jobID和经过的时间。首先,我假设
jobID
和moduleCode
未分析:
{
"mappings": {
"jobs": {
"properties": {
"jobID":{
"type": "string",
"index": "not_analyzed"
},
"startTime":{
"type": "date"
},
"endTime":{
"type": "date"
},
"moduleCode":{
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
在ES 1.4.0中可用以计算这两个值之间的差异。我还没有研究如何为“小于2880000”添加过滤,但我希望可以使用该脚本来限制这一点:
{
"query": {
"match_all": {}
},
"aggs": {
"jobIds": {
"terms": {
"field": "jobID"
},
"aggs": {
"executionTimes": {
"scripted_metric": {
"init_script": "_agg['time'] = 0L",
"map_script": "if (doc['moduleCode'].value == \"STARTING_MODULE\") { _agg['time']=-1*doc['startTime'].value } else { _agg['time']=doc['endTime'].value}",
"combine_script": "execution = 0; for (t in _agg.time) { execution += t };return execution",
"reduce_script": "execution = 0; for (a in _aggs) { execution += a }; return execution"
}
}
}
}
}
}
结果应该是这样的:
"aggregations": {
"jobIds": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "ace4c888-1907-4021-a808-4a816e99aa1e",
"doc_count": 2,
"executionTimes": {
"value": 1
}
},
{
"key": "ace4c888-1907-4021-a808-4a816e99aa2e",
"doc_count": 2,
"executionTimes": {
"value": 1000201063
}
},
{
"key": "ace4c888-1907-4021-a808-4a816e99aa3e",
"doc_count": 2,
"executionTimes": {
"value": 10000
}
}
]
}
}