2

我尝试熟悉ElasticSearch 的 Reindexing API和 Painless脚本的使用。

我有以下模型:

"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-v1to重新索引所有文档test-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
}

根据本教程 Date fields are exposed as ReadableDateTime so they support methods like getYear, and getDayOfWeek.,实际上,参考文献提到了那些作为支持的方法。

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

有人建议我做错了什么吗?

4

0 回答 0