ElasticSearch Reindex API和无痛脚本访问日期字段

时间:2017-10-25 14:50:37

标签: date elasticsearch elasticsearch-5 elasticsearch-painless

我尝试熟悉Reindexing API的ElasticSearch以及Painless scripts的使用。

我有以下型号:

"mappings": {
    "customer": {
        "properties": {
            "firstName": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "lastName": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "dateOfBirth": {
                "type": "date"
            }
        }
    }
}

我想重新索引从test-v1test-v2的所有文档,并对它们应用一些转换(例如,提取dateOfBirth的年份部分,将日期值转换为时间戳等)并将结果保存为新字段。但是当我试图访问它时,我遇到了一个问题。

当我拨打以下电话时,我收到了一个错误:

POST /_reindex?pretty=true&human=true&wait_for_completion=true HTTP/1.1
Host: localhost:9200
Content-Type: application/json

{
    "source": {
        "index": "test-v1"
    },
    "dest": {
        "index": "test-v2"
    },
    "script": {
        "lang": "painless",
        "inline": "ctx._source.yearOfBirth = ctx._source.dateOfBirth.getYear();"
    }
}

回复:

{
"error": {
    "root_cause": [
        {
            "type": "script_exception",
            "reason": "runtime error",
            "script_stack": [
                "ctx._source.yearOfBirth = ctx._source.dateOfBirth.getYear();",
                "                                                 ^---- HERE"
            ],
            "script": "ctx._source.yearOfBirth = ctx._source.dateOfBirth.getYear();",
            "lang": "painless"
        }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
        "ctx._source.yearOfBirth = ctx._source.dateOfBirth.getYear();",
        "                                                 ^---- HERE"
    ],
    "script": "ctx._source.yearOfBirth = ctx._source.dateOfBirth.getYear();",
    "lang": "painless",
    "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Unable to find dynamic method [getYear] with [0] arguments for class [java.lang.String]."
    }
},
"status": 500
}

根据this tutorial Date fields are exposed as ReadableDateTime so they support methods like getYear, and getDayOfWeek.确实,Reference提到这些是受支持的方法。

但是,响应仍然提到[java.lang.String]作为dateOfBirth属性的类型。我可以解析它,例如一个OffsetDateTime,但我想知道为什么它是一个字符串。

任何人都有建议我做错了吗?

0 个答案:

没有答案