如何在 Elastic Query 函数的过滤器中添加 should 子句
How to add should clause inside filters in functions in ElasticQuery
我有一个弹性查询,它在函数内部有一个过滤器。我需要添加一个条件,例如它应该与部门过滤器匹配或者它应该与任何 product_ids 匹配,以便召回必须包含通过的 product_ids。下面是我想在elastic recall中看到的product_ids需要在ES查询的函数中添加
{
"terms": {
"product_ids": [
"166168",
"753547",
"156835",
"90112"
]
}
}
下面是实际的ES查询
"track_total_hits": true,
"_source": {},
"query": {
"boosting": {
"positive": {
"function_score": {
"query": {},
"boost_mode": "avg",
"score_mode": "sum",
"functions": [
{
"filter": {
"match": {
"Departments": {
"query": "1/lipstick",
"operator": "OR",
"fuzzy_transpositions": true,
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
"weight": 99
}
//I need to add the product_ids terms here
]
}
},
"negative": {},
"negative_boost": "1.0E-4"
}
},
您可以在 filter
子句中创建 bool
查询。
{
"query": {
"boosting": {
"positive": {
"function_score": {
"query": {
"match_all": {}
},
"boost_mode": "avg",
"score_mode": "sum",
"functions": [
{
"filter": {
"bool": {
"should": [
{
"match": {
"Departments": {
"query": "1/lipstick",
"operator": "OR",
"fuzzy_transpositions": true,
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
{
"terms": {
"product_ids": [
"166168",
"753547",
"156835",
"90112"
]
}
}
]
}
},
"weight": 99
}
]
}
},
"negative": {},
"negative_boost": "1.0E-4"
}
}
}
您还可以添加多个函数,并且只有当文档与给定的过滤查询匹配时,才可以选择应用该函数,如前所述here。
GET /_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"filter": {
"match": {
"Departments": {
"query": "1/lipstick",
"operator": "OR",
"fuzzy_transpositions": true,
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
"weight": 99
},
{
"filter": {
"terms": {
"product_ids": [
"166168",
"753547",
"156835",
"90112"
]
}
},
"weight": 42
}
],
"boost_mode": "avg",
"score_mode": "sum"
}
}
}
我有一个弹性查询,它在函数内部有一个过滤器。我需要添加一个条件,例如它应该与部门过滤器匹配或者它应该与任何 product_ids 匹配,以便召回必须包含通过的 product_ids。下面是我想在elastic recall中看到的product_ids需要在ES查询的函数中添加
{
"terms": {
"product_ids": [
"166168",
"753547",
"156835",
"90112"
]
}
}
下面是实际的ES查询
"track_total_hits": true,
"_source": {},
"query": {
"boosting": {
"positive": {
"function_score": {
"query": {},
"boost_mode": "avg",
"score_mode": "sum",
"functions": [
{
"filter": {
"match": {
"Departments": {
"query": "1/lipstick",
"operator": "OR",
"fuzzy_transpositions": true,
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
"weight": 99
}
//I need to add the product_ids terms here
]
}
},
"negative": {},
"negative_boost": "1.0E-4"
}
},
您可以在 filter
子句中创建 bool
查询。
{
"query": {
"boosting": {
"positive": {
"function_score": {
"query": {
"match_all": {}
},
"boost_mode": "avg",
"score_mode": "sum",
"functions": [
{
"filter": {
"bool": {
"should": [
{
"match": {
"Departments": {
"query": "1/lipstick",
"operator": "OR",
"fuzzy_transpositions": true,
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
{
"terms": {
"product_ids": [
"166168",
"753547",
"156835",
"90112"
]
}
}
]
}
},
"weight": 99
}
]
}
},
"negative": {},
"negative_boost": "1.0E-4"
}
}
}
您还可以添加多个函数,并且只有当文档与给定的过滤查询匹配时,才可以选择应用该函数,如前所述here。
GET /_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"filter": {
"match": {
"Departments": {
"query": "1/lipstick",
"operator": "OR",
"fuzzy_transpositions": true,
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
"weight": 99
},
{
"filter": {
"terms": {
"product_ids": [
"166168",
"753547",
"156835",
"90112"
]
}
},
"weight": 42
}
],
"boost_mode": "avg",
"score_mode": "sum"
}
}
}