基于对象数组的弹性搜索过滤器
Elastic search filter based on array of object
下面是我的地图
{
"defaultBoostValue":1.01,
"boostDetails": [
{
"Type": "Type1",
"value": 1.0001
},
{
"Type": "Type2",
"value": 1.002
},
{
"Type": "Type3",
"value": 1.0005
}
]
}
我想根据 type 应用升压类型,所以如果 boostType 是 Type3 那么 boostFactor 应该是 1.0005,如果它没有那个 boostType,它应该应用“defaultBoostValue”作为升压
下面是我试过的查询
{
"query": {
"function_score": {
"boost_mode": "multiply",
"functions": [
{
"filter": {
"match": {
"boostDetails.Type": "Type1"
}
},
"field_value_factor": {
"field": "boostDetails.value",
"factor": 1,
"missing": 1
}
}
]
}
}
}
它没有按预期工作,因为 boostDetails 是对象数组,我们如何在这种情况下应用过滤器
您可以使用 Script Score function_score
query
{
"query": {
"function_score": {
"boost_mode": "multiply",
"functions": [
{
"filter": {
"match": {
"boostDetails.Type": "Type1"
}
},
"script_score": {
"script": {
"source": """
double findBoost(Map params_copy) {
for (def group : params_copy._source.boostDetails) {
if (group['Type'] == params_copy.preferredBoostType ) {
return group['value'];
}
}
return params_copy._source['defaultBoostValue'];
}
return findBoost(params)
""",
"params": {
"preferredBoostType": "Type1"
}
}
}
}
]
}
}
}
但请记住,如果给定文档中不存在此特定提升类型 (Type1
),则 function_score
查询的 filter
将阻止激活此脚本-> defaultBoostValue
根本不会默认为
所以我建议改为使用 match_all
过滤器,当然还要保留 preferredBoostType
:
{
"query": {
"function_score": {
"boost_mode": "multiply",
"functions": [
{
"filter": {
"match_all": {} <---
},
"script_score": {
"script": {
"source": """
...
""",
"params": {
"preferredBoostType": "Type1"
}
}
}
}
]
}
}
}
顺便说一句,如果您的 boostDetails
数组不是 nested
类型,您可能会遇到意想不到的结果,如 and 所述。
下面是我的地图
{
"defaultBoostValue":1.01,
"boostDetails": [
{
"Type": "Type1",
"value": 1.0001
},
{
"Type": "Type2",
"value": 1.002
},
{
"Type": "Type3",
"value": 1.0005
}
]
}
我想根据 type 应用升压类型,所以如果 boostType 是 Type3 那么 boostFactor 应该是 1.0005,如果它没有那个 boostType,它应该应用“defaultBoostValue”作为升压 下面是我试过的查询
{
"query": {
"function_score": {
"boost_mode": "multiply",
"functions": [
{
"filter": {
"match": {
"boostDetails.Type": "Type1"
}
},
"field_value_factor": {
"field": "boostDetails.value",
"factor": 1,
"missing": 1
}
}
]
}
}
}
它没有按预期工作,因为 boostDetails 是对象数组,我们如何在这种情况下应用过滤器
您可以使用 Script Score function_score
query
{
"query": {
"function_score": {
"boost_mode": "multiply",
"functions": [
{
"filter": {
"match": {
"boostDetails.Type": "Type1"
}
},
"script_score": {
"script": {
"source": """
double findBoost(Map params_copy) {
for (def group : params_copy._source.boostDetails) {
if (group['Type'] == params_copy.preferredBoostType ) {
return group['value'];
}
}
return params_copy._source['defaultBoostValue'];
}
return findBoost(params)
""",
"params": {
"preferredBoostType": "Type1"
}
}
}
}
]
}
}
}
但请记住,如果给定文档中不存在此特定提升类型 (Type1
),则 function_score
查询的 filter
将阻止激活此脚本-> defaultBoostValue
根本不会默认为
所以我建议改为使用 match_all
过滤器,当然还要保留 preferredBoostType
:
{
"query": {
"function_score": {
"boost_mode": "multiply",
"functions": [
{
"filter": {
"match_all": {} <---
},
"script_score": {
"script": {
"source": """
...
""",
"params": {
"preferredBoostType": "Type1"
}
}
}
}
]
}
}
}
顺便说一句,如果您的 boostDetails
数组不是 nested
类型,您可能会遇到意想不到的结果,如