2015-11-18 37 views
0

我需要对数据的子串执行avg()。在ES中可能吗? 这里是我的脚本ElasticSearch数据子串上的聚合

{  
    "size": 0 
    ,"query": { 
     "bool": { 
     "must": [ 
      { "term": {"app": "att"} } 
     ] 
     } 
    } 
    ,"aggs": { 
    "clients": { 
     "terms": {"field": "client"} 
     ,"aggs" : { 
       "_avg_" : { "avg" : { "field" : "ms" } } 
      }  
     }  
    } 
} 

的问题是场“_MS”的模样:

It took: 100 ms ...... 
It took: 104 ms ...... 
It took: 102 ms ...... 

所以,我不得不拉100,104,102等之前,我做“ avg“

+1

你有大量数据的?我强烈建议重新编制索引并妥善保存。 –

+0

我没有大量的数据,但很确定我没有重新索引过程中的说法。应用程序以这种方式转储数据。 – epipko

+0

然后修复您的应用程序并妥善保存。你只是通过这种方式使事情更复杂。另一种方法是创建一个单独的字段,最初将存储相同的值,但应用char过滤器,因此只有数字保留。然后你可以做你的聚合。 –

回答

0

如果您无法重新为您的数据建立索引并绝对”必须“处理该数据,则可以在avg聚合中使用script。由于您没有太多数据,因此性能不会受到太大影响。

您可以使用的Groovy脚本将只是doc['ms'].value as Integer,它会将字符串强制为一个整数并使用整数值来执行平均值。

{ 
    "size": 0, 
    "query": { 
     "bool": { 
     "must": [ 
      { 
       "term": { 
        "app": "att" 
       } 
      } 
     ] 
     } 
    }, 
    "aggs": { 
     "clients": { 
     "terms": { 
      "field": "client" 
     }, 
     "aggs": { 
      "_avg_": { 
       "avg": { 
        "script": "doc['ms'].value as Integer" 
       } 
      } 
     } 
     } 
    } 
} 

注意,为了执行这个查询,你需要enable dynamic scripting

+0

感谢您的回复,但我发现字符串将以这种方式进行格式化:“花费:100毫秒;范围:0到1秒;”。刚刚尝试过你的解决方案,但它没有奏效。是否因为存在多个整数? – epipko