【问题标题】:Elasticsearch painless, how to determine the data type of a fieldElasticsearch无痛,如何判断一个字段的数据类型
【发布时间】:2018-03-01 02:46:59
【问题描述】:

在es5.5中,如何判断一个字段是否为数值?

if (is_numeric(ctx._source.some)) {
    ctx._source.some = ctx._source.some + 2
}

【问题讨论】:

  • 你为什么不得到映射并确定呢? elastic.co/guide/en/elasticsearch/reference/current/…
  • 我相信它可以做到无痛。只是不要让路。
  • 有一个 instanceof 可用于检查引用类型,您可能可以使用它来定义自己的 is_numeric 函数
  • “你为什么不得到映射” - 可能是因为映射因索引而异,并且你想找到每个文档的类型......也许是为了发现一些类型冲突?嗯 - 这就是 目前想做的事情。

标签: elasticsearch elasticsearch-painless


【解决方案1】:

instanceof 操作符可能会有所帮助

if (ctx._source.some instanceof byte ||
    ctx._source.some instanceof short ||
    ctx._source.some instanceof int ||
    ctx._source.some instanceof long ||
    ctx._source.some instanceof float ||
    ctx._source.some instanceof double)
{
    ctx._source.some = ctx._source.some + 2
}

【讨论】:

    【解决方案2】:

    另一种方法是使用Debug.explain,见https://www.elastic.co/guide/en/elasticsearch/painless/6.8/painless-debugging.html

    这将通过painless_explain_error 中止,输出将告诉您涉及哪些类。有了这些信息(从各种 ElasticSearch 版本的各种索引中手动获取),您就可以使用 instanceof 实现 Painless,如 @oleg-grishko 的答案所示。

    POST /hockey/player/1/_explain
    {
      "query": {
        "script": {
           "script": "Debug.explain(doc.goals)"
        }
      }
    }
    
    {
       "error": {
          "type": "script_exception",
          "painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
           ...
    

    【讨论】:

      猜你喜欢
      • 2011-01-04
      • 1970-01-01
      • 2012-06-16
      • 2017-08-30
      • 2012-06-07
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多