彈性搜索 7.13 版
索引已經存在,我想用映射重新索引,該欄位是一個布林值。但是當我嘗試重新索引時,該欄位具有"1"和"0"(字串)。
如何評估是否field = "1"設定true(0 相同,但為假)?
我讀過關于runtime,但無法弄清楚它是如何作業的。
我的映射
{
mappings:{
"OPTIONS": {
"type": "nested",
"properties":{
"COMBINABLE": {
"type": "boolean"
}
}
}
}
}
和檔案
{
"options": [
{
"COMBINABLE": "0"
}
]
}
uj5u.com熱心網友回復:
您可能會考慮使用管道攝取將您的數字轉換為布林值,您可以執行以下操作:
POST _ingest/pipeline/_simulate
{
"pipeline": {
"description": "convert to boolean",
"processors": [
{
"script": {
"source": "def options = ctx.options;def pairs = new ArrayList();for (def pair : options) {def k = false;if (pair[\"COMBINABLE\"] == \"1\" || pair[\"COMBINABLE\"] == 1) {k = true;}pair[\"COMBINABLE\"] = k;}ctx.options = options;"
}
}
]
},
"docs": [
{
"_source": {
"options": [
{
"COMBINABLE": 1
}
]
}
}
]
}
上面的無痛腳本非常簡單:
def options = ctx.options;
def pairs = new ArrayList();
for (def pair : options) {
def k = false;
if (pair["COMBINABLE"] == "1" || pair["COMBINABLE"] == 1) {
k = true;
}
pair["COMBINABLE"] = k;
}
ctx.options = options;
它只是回圈遍歷 下的所有選項options,然后如果COMBINABLE是1或"1",它將轉換為true,否則,它將是false。您可以將管道設定為默認攝取,請參見此處
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/348851.html
