如何比较Elasticsearch的小时数

时间:2018-11-06 11:17:40

标签: elasticsearch elasticsearch-painless

我试图在我的Elasticsearch查询中使用无痛语言比较小时。我想查询类似的内容:

{  
   "script":"doc['schedule.from_time'] >= doc['schedule.to_time']"
}

但是我有错误:

  

无法对[org.elasticsearch.index.fielddata.ScriptDocValues.Dates]类型应用[>]操作

嵌套文档的方案是:

{
    "settings": {
        "index.mapping.total_fields.limit": 10000
    },
    "mappings": {
        "_doc": {
           "dynamic_templates": [{
                "integers": {
                    "match_mapping_type": "long",
                    "mapping": {
                        "type": "long",
                        "index": false
                    }
                }
            }],
            "properties": {
                "enabled_services": {
                    "type": "nested",
                    "properties": {
                        "service_id": {
                           "type": "text",
                           "analyzer": "whitespace",
                           "search_analyzer": "whitespace"
                        },
                        "available_day_of_week": {
                            "type": "long"
                        },
                        "available_from_time": {
                            "type": "date",
                            "format": "hour_minute"
                        },
                        "available_to_time": {
                            "type": "date",
                            "format": "hour_minute"
                        }
                    }
                }
            }
        }
    }
}

(这些值的格式类似于“ 2:00”或“ 18:00”)。

我尝试使用.date.value,但由于我的变量仅包含小时而不是日期时间,因此它不起作用。

有人可以帮我吗:)

2 个答案:

答案 0 :(得分:0)

我认为您正在寻找:

doc['enabled_services.available_from_time'].value.isAfter(doc['enabled_services.available_to_time'].value)

您还需要从映射中删除"type": "nested"。我认为您无需这样做。

工作代码如下:

映射

PUT /painless-dates
{
  "settings": {
    "index.mapping.total_fields.limit": 10000
  },
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {
          "integers": {
            "match_mapping_type": "long",
            "mapping": {
              "type": "long",
              "index": false
            }
          }
        }
      ],
      "properties": {
        "enabled_services": {
          "properties": {
            "service_id": {
              "type": "text",
              "analyzer": "whitespace",
              "search_analyzer": "whitespace"
            },
            "available_day_of_week": {
              "type": "long"
            },
            "available_from_time": {
              "type": "date",
              "format": "hour_minute"
            },
            "available_to_time": {
              "type": "date",
              "format": "hour_minute"
            }
          }
        }
      }
    }
  }
}

添加两个元素

POST /painless-dates/_doc
{
  "enabled_services": {
    "available_from_time": "02:00",
    "available_to_time": "18:00"
  }
}

POST /painless-dates/_doc
{
  "enabled_services": {
    "available_from_time": "04:00",
    "available_to_time": "03:00"
  }
}

查询

GET /painless-dates/_search
{
  "query": {
    "bool": {
      "must": {
        "script": {
          "script": {
            "source": "doc['enabled_services.available_from_time'].value.isAfter(doc['enabled_services.available_to_time'].value)",
            "lang": "painless"
          }
        }
      }
    }
  }
}

答案

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "painless-dates",
        "_type": "_doc",
        "_id": "0wFw7mYBueYINcTmJsMG",
        "_score": 1,
        "_source": {
          "enabled_services": {
            "available_from_time": "04:00",
            "available_to_time": "03:00"
          }
        }
      }
    ]
  }
}

答案 1 :(得分:0)

好,我找到了答案:

{
    "script": {
        "script": "doc['enabled_services.available_from_time'].date.isBefore(doc['enabled_services.available_to_time'].date)"
    }
}

谢谢大家!