Concat从Elastic Search查询值,以避免创建新字段



我正在使用Elastic搜索查询一个字段。该字段可以是";"小"中等"平均值"大";。

但我想要";"小";以及";中等;返回";是";并且";平均值";以及";"大";返回";否";

有什么方法可以在查询中做到这一点吗?或者在被动搜索中使用列表组件?

感谢

您可以使用script_field

{
"_source": "*", 
"script_fields": {
"custom_field": {
"script": {
"source":  """
if(doc['description.keyword'].value=='Small' || doc['description.keyword'].value=='Medium')
return 'Yes';
else
return 'No';
"""
}
}
}
}

结果

"hits" : [
{
"_index" : "index68",
"_type" : "_doc",
"_id" : "GyEhPYQBJutE-yZceoEO",
"_score" : 1.0,
"_source" : {
"description" : "Small",
"title" : "A"
},
"fields" : {
"custom_field" : [
"Yes"
]
}
},
{
"_index" : "index68",
"_type" : "_doc",
"_id" : "HCEhPYQBJutE-yZcpoGE",
"_score" : 1.0,
"_source" : {
"description" : "Medium",
"title" : "B"
},
"fields" : {
"custom_field" : [
"Yes"
]
}
},
{
"_index" : "index68",
"_type" : "_doc",
"_id" : "HSEhPYQBJutE-yZctoHu",
"_score" : 1.0,
"_source" : {
"description" : "Average",
"title" : "B"
},
"fields" : {
"custom_field" : [
"No"
]
}
},
{
"_index" : "index68",
"_type" : "_doc",
"_id" : "HiEhPYQBJutE-yZc04Fx",
"_score" : 1.0,
"_source" : {
"description" : "Big",
"title" : "C"
},
"fields" : {
"custom_field" : [
"No"
]
}
}
]

最新更新