简体   繁体   English

对象数组上的Elasticsearch脚本过滤器

[英]Elasticsearch script filter on object array

I've got a following elasticsearch document: 我有以下弹性搜索文档:

{
"json":{
  "freeSlots":[
         {
            "day":"2015-04-21",
            "beginTime":"08:00:00",
            "endTime":"09:00:00"
         },
         {
            "day":"2015-04-21",
            "beginTime":"14:00:00",
            "endTime":"17:00:00"
         }
      ]
   }
}

and I expected that this kind of query would return it: 我希望这种查询将返回它:

{
"query" : {
    "filtered" : {
        "query" : {
            "match_all" : {}
        }
     },
     "filter" : {
         "script": {
             "script" : "doc['json.freeSlots.endTime'].value - doc['json.freeSlots.beginTime'].value > param1",
             "params" : {
                 "param1" : 7200000
             }
          }
     }
 }

Unfortunately it considers only first element of array and its difference between endTime and beginTime is less than 7200000 ms. 不幸的是,它仅考虑数组的第一个元素,并且endTimebeginTime之间的差小于7200000 ms。 How can I change this query so that it returns any document containing an object within freeSlots array which duration (difference between endTime and beginTime ) is greater than 7200000 ms? 如何更改此查询,以便它返回任何文档,其中包含freeSlots数组中包含持续时间( endTimebeginTime之间的beginTime )大于7200000 ms的对象的文档? Date mappings are set. 日期映射已设置。

I'd index each slot as a separate document: { "day":... "beginTime":... "endTime":... } 我将每个插槽都索引为一个单独的文档: { "day":... "beginTime":... "endTime":... }

Then use this query: 然后使用以下查询:

{ "query": { "filter": { "script": { "params": { "delta": 7200000 }, "script": "(doc['endTime'].value - doc['beginTime'].value) > delta" } }, "filtered": { "query": { "match_all": {} } } } }

Also, you may need to configure the script in the server (AFAIK recent versions don't suppport this kind of dynamic script). 另外,您可能需要在服务器中配置脚本(AFAIK最新版本不支持这种动态脚本)。

One way of debugging what's going on is to use a script_field to output some values: 调试正在发生的事情的一种方法是使用script_field输出一些值:

... "script_fields": { "delta": { "file": "debug_script_fields", "lang": "groovy" } } ...

In the server, in config/scripts add a file debug_script_fields.groovy : 在服务器的config/scripts添加一个文件debug_script_fields.groovy

doc['endTime'].value - doc['beginTime'].value

If the value is not what you expect it's unlikely that the filtered query will work. 如果该值不是您期望的值,则过滤后的查询不太可能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM