Elasticsearch& Groovy脚本嵌套字段聚合匹配

时间:2016-02-17 17:09:07

标签: elasticsearch groovy

我正在尝试使用默认脚本语言Groovy在Elasticsearch中创建自定义脚本聚合。

我在elasticsearch中设置的数据是:

"days_open": [
{
"open": "0800",
"close": "2200",
"dotw": "monday"
}
,
{
"open": "0800",
"close": "2300",
"dotw": "tuesday"
}
,
{
"open": "0800",
"close": "2200",
"dotw": "wednesday"
}
,
{
"open": "0800",
"close": "2300",
"dotw": "thursday"
}
,
{
"open": "0800",
"close": "2300",
"dotw": "friday"
}
,
{
"open": "0800",
"close": "1700",
"dotw": "saturday"
}
]

我想制作一个聚合“立即打开”,它将显示所有带有此嵌套数据的总线“开放”。

"open_now": {
  "filter": {
    "script": {
      "script": {
        "inline": "days_open = _source.days_open; days_open.each {day-> if (day.dotw == 'wednesday' && day.open < 1200 && day.close > 1200) { return true; }}; return false;"
      }
    }
  }
}

但是我收到这样的错误:

"reason": {
    "type": "script_exception",
    "reason": "failed to run inline script [days_open = _source.days_open; days_open.each {day-> if (day.dotw == 'wednesday' && day.open < 1200 && day.close > 1200) { return true; }}; return false;] using lang [groovy]",
    "caused_by": {
        "type": "no_class_def_found_error",
        "reason": "sun/reflect/MethodAccessorImpl",
            "caused_by": {
                 "type": "class_not_found_exception",
                 "reason": "sun.reflect.MethodAccessorImpl"
             }
         }
      }

似乎days_open变量值是这样的:[{dotw=monday, close=2200, open=0800}, {dotw=tuesday, close=2300, open=0800}, {dotw=wednesday, close=2200, open=0800}, {dotw=thursday, close=2300, open=0800}, {dotw=friday, close=2300, open=0800}, {dotw=saturday, close=1700, open=0800}],我可以使用本机查询从Elasticsearch中的_head插件获取,但我不知道如何获得调试信息比那个来自Groovy的界面。

我正在使用https://github.com/ongr-io/ElasticsearchDSL ONGR Elasticsearch捆绑包,它似乎在Nested Aggs中存在一个错误(它包含两次重复的密钥),但是放置原始嵌套aggs并不能真正给我结果我是寻找无论如何。我正在寻找看起来像这样的单一结果:

"open_now": {
   "doc_count": 5
},

哪个会翻译“5个业务目前正在开放(20个)”,没有桶。脚本似乎是最好的选择,指导我,如果我错了或者我错过了什么!

基本上我需要ElasticsearchDSL中的脚本或聚合,它可以给我我想要的结果。如果字段“business.website”为true,我目前还有其他aggs可以在单个字段上运行,例如“has_website”,但由于这个需要一点逻辑来确定“现在打开”,我不知道该怎样去关于它的方式是有效的。

1 个答案:

答案 0 :(得分:1)

有趣的是,你总能在4小时后找到解决方案并寻求帮助。

我设法通过使用Java样式<?xml version="1.0" encoding="UTF-8"?>\ <rs:model-request throttlesize="5"\ xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"\ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\ xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">\ <rs:target-models>\ <rs:models-search>\ <rs:search-criteria\ xmlns="http://www.ca.com/spectrum/restful/schema/filter">\ <filtered-models>\ <equals>\ <attribute id="0x11ee8">\ <value>15</value> <!-- RTR_Cisco -->\ </attribute>\ </equals>\ </filtered-models>\ </rs:search-criteria>\ </rs:models-search>\ </rs:target-models>\ <rs:requested-attribute id="0x1006e" />\ <rs:requested-attribute id="0x10000" />\ <rs:requested-attribute id="0x10032" />\ <rs:requested-attribute id="0x12de2" />\ </rs:model-request>\ 语句使查询正常工作: Groovy查询:

for

ElasticsearchDSL代码:

days_open = _source.days_open; 
for (day in days_open) { 
    if (day.dotw == 'wednesday' && day.open < '1200' && day.close > '1200') {
       return true; 
    }
}; 
return false;