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

时间:2017-08-03 07:16:48

标签: elasticsearch

继续提问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],现在遇到[如果你想定义几个函数,请使用[functions]数组。“

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

我无法在查询中找到将“高斯”函数放在何处使用基于created_at字段的衰减。

更新 我也尝试了以下查询:

{
  "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 :(得分: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值,因此它们的偏移量下降,因此没有计算出衰减。