弹性搜索仅根据字段值对某些文档应用提升
Elastic search apply boost only for certain documents based on their field value
以下是我的索引文档:
{
"id": 123,
"details": {
"boostType": "Type1",
"boostFactor": 1.00022
}
}
{
"id": 456,
"details": {
"boostType": "Type2",
"boostFactor": 1.00022
}
}
所以我只想对具有 boostType1
的文档应用提升,提升值将为 1.00022
。下面是我的查询,但它对所有文档应用提升,我只希望它用于某些 boostType
。我试过如下操作:
{
"query": {
"function_score": {
"boost_mode": "sum",
"functions": [
{
"field_value_factor": {
"field": "details.boostFactor",
"factor": 1,
"missing": 1
}
}
]
}
}
}
函数接受额外的 filter
对象,阅读此处:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html,应该适合你。
{
"query": {
"function_score": {
"boost_mode": "sum",
"functions": [
{
"filter": {
"term": {
"details.boostType": "Type1"
}
},
"field_value_factor": {
"field": "details.boostFactor",
"factor": 1,
"missing": 1
}
}
]
}
}
}
请注意,我已将 boostType
映射为 keyword
。
以下是我的索引文档:
{
"id": 123,
"details": {
"boostType": "Type1",
"boostFactor": 1.00022
}
}
{
"id": 456,
"details": {
"boostType": "Type2",
"boostFactor": 1.00022
}
}
所以我只想对具有 boostType1
的文档应用提升,提升值将为 1.00022
。下面是我的查询,但它对所有文档应用提升,我只希望它用于某些 boostType
。我试过如下操作:
{
"query": {
"function_score": {
"boost_mode": "sum",
"functions": [
{
"field_value_factor": {
"field": "details.boostFactor",
"factor": 1,
"missing": 1
}
}
]
}
}
}
函数接受额外的 filter
对象,阅读此处:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html,应该适合你。
{
"query": {
"function_score": {
"boost_mode": "sum",
"functions": [
{
"filter": {
"term": {
"details.boostType": "Type1"
}
},
"field_value_factor": {
"field": "details.boostFactor",
"factor": 1,
"missing": 1
}
}
]
}
}
}
请注意,我已将 boostType
映射为 keyword
。