首页 文章

Elasticsearch:在文档第3页中使用自定义分数字段对影响评分 - 添加衰减

提问于
浏览
0

问题的延续Elasticsearch: Influence scoring with custom score field in document pt.2

这一切都可以正常@Joanna的回答 . 我只想在查询中添加衰减函数:

{
  "query": {
    "function_score": {
        "query": {
            "bool": {
                "should": [{
                    "nested": {
                      "path": "tags",
                      "score_mode": "sum",
                      "query": {
                        "function_score": {
                          "query": {
                            "match": {
                              "tags.tag": "landscape"
                            }
                          },
                          "field_value_factor": {
                            "field": "tags.confidence",
                            "factor": 1,
                            "missing": 0
                          }
                        }
                      }
                    }
                }]
            }
        },
        "field_value_factor": {
            "field": "boost_multiplier",
            "factor": 1,
            "missing": 0
        }
      }
    }
}

基于文档的created_at字段:

{
  "created_at" : "2017-07-31T20:30:14-04:00",
  "description" : null,
  "height" : 3213,
  "id" : "1",
  "tags" : [
    {
      "confidence" : 65.48948436785749,
      "tag" : "beach"
    },
    {
      "confidence" : 57.31950504425406,
      "tag" : "sea"
    },
    {
      "confidence" : 43.58207236617374,
      "tag" : "coast"
    },
    {
      "confidence" : 35.6857910950816,
      "tag" : "sand"
    },
    {
      "confidence" : 33.660057321079655,
      "tag" : "landscape"
    },
    {
      "confidence" : 32.53252312423727,
      "tag" : "sky"
    }
  ],
  "width" : 5712,
  "color" : "#0C0A07",
  "boost_multiplier" : 1
}

我在文档中找到了这个:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-decay

我试图将文档中的示例中显示的高斯函数添加为内部“field_value_factor”的兄弟,并且它给出错误说“未能解析[function_score]查询 . 已找到函数[field_value_factor],现在遇到[gauss] . 如果要定义多个函数,请使用[functions]数组 . “

然后我把“field_value_factor”和“gauss”放在内部“查询”内的函数数组下,这次我收到错误说“无法解析[START_OBJECT] . 格式错误的查询,在解析函数时期望[VALUE_STRING]但得到了[ function_score]而不是“ .

简单地说,我找不到在查询中放置“高斯”函数的位置,以使用基于created_at字段的衰减 .

UPDATE 我也尝试过以下查询:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [{
            "nested": {
              "path": "tags",
              "score_mode": "sum",
              "query": {
                "function_score": {
                  "query": {
                    "match": {
                      "tags.tag": "landscape city"
                    }
                  },
                  "field_value_factor": {
                    "field": "tags.confidence",
                    "factor": 5,
                    "missing": 0
                  }
                }
              }
            }
          }]
        }
      },
      "functions": [
        {
          "decay": {
            "gauss": {
              "created_at": {
                "origin": "2013-09-17",
                "scale": "10d",
                "offset": "5d",
                "decay": 0.5
              }
            }
          }
        },
        {
          "field_value_factor": {
            "field": "boost_multiplier",
            "factor": 1,
            "missing": 0
          }
        }
      ]
    }
  }
}

这次它给出了“没有[查询]注册[衰变]”的错误 .

有帮助吗?

UPDATE-2 以下查询有效:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [{
            "nested": {
              "path": "tags",
              "score_mode": "sum",
              "query": {
                "function_score": {
                  "query": {
                    "match": {
                      "tags.tag": "landscape city"
                    }
                  },
                  "field_value_factor": {
                    "field": "tags.confidence",
                    "factor": 5,
                    "missing": 0
                  }
                }
              }
            }
          }]
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "boost_multiplier",
            "factor": 1,
            "missing": 0
          }
        },
        {
          "gauss": {
            "created_at": {
              "scale": "365d",
              "offset": "5d",
              "decay" : 0.5
            }
          }
        }
      ]
    }
  }
}

作品意味着它没有给出错误,但我没有得到我预期的结果 . 我只是想提升最近的文件而不是旧文件 . 有什么帮助怎么实现呢?

1 回答

  • 0

    此查询有效:

    {
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "should": [{
                "nested": {
                  "path": "tags",
                  "score_mode": "sum",
                  "query": {
                    "function_score": {
                      "query": {
                        "match": {
                          "tags.tag": "landscape city"
                        }
                      },
                      "field_value_factor": {
                        "field": "tags.confidence",
                        "factor": 5,
                        "missing": 0
                      }
                    }
                  }
                }
              }]
            }
          },
          "functions": [
            {
              "field_value_factor": {
                "field": "boost_multiplier",
                "factor": 1,
                "missing": 0
              }
            },
            {
              "gauss": {
                "created_at": {
                  "scale": "365d",
                  "offset": "5d",
                  "decay" : 0.5
                }
              }
            }
          ]
        }
      }
    }
    

    问题是文档最近有created_at值,因此它们在偏移量下降,因此没有计算出衰减 .

相关问题