elasticsearch version: 7.10.1
field_value_factor 是 Elasticsearch 中用于計算基于字段值的函數(shù)評分的一部分,它允許你根據(jù)文檔中某個字段的值來調(diào)整該文檔的評分。文章來源:http://www.zghlxwxcb.cn/news/detail-849553.html
field_value_factor語法
{
"query": {
"match_all": {}
},
"function_score": {
"functions": [
{
"field_value_factor": {
"field": "popularity",
"factor": 1,
"modifier": "log1p",
"missing": 1
}
}
],
"score_mode": "multiply",
"boost_mode": "sum"
}
}
{
"field_value_factor": {
"field": "your_field_name",
"factor": your_factor_value,
"modifier": "none | log | log1p | log2p | square | sqrt | reciprocal",
"missing": default_value_for_missing_field
}
}
- field:這是你想要根據(jù)其值來調(diào)整評分的字段名。它應(yīng)該是一個數(shù)值字段。
- factor:一個乘數(shù),用于調(diào)整字段值對最終評分的影響。例如,如果你想要將字段值乘以 10 來增加其影響,你可以設(shè)置 factor 為 10。
- modifier:這是一個可選參數(shù),它定義了如何修改字段值以計算其評分貢獻(xiàn)。它可以是以下值之一:
none:不進(jìn)行任何修改,直接使用字段值。
log:對字段值取自然對數(shù)。
log1p:對字段值加 1 后取自然對數(shù),這可以防止對 0 或負(fù)數(shù)取對數(shù)。
log2p:對字段值加 1 后取以 2 為底的對數(shù)。
square:計算字段值的平方。
sqrt:計算字段值的平方根。
reciprocal:計算字段值的倒數(shù)。 - missing:當(dāng)字段的值缺失或不存在時,使用的默認(rèn)值。這確保即使某些文檔缺少該字段,也可以計算一個評分。
field_value_factor案例
場景
我們想要根據(jù)產(chǎn)品的價格來調(diào)整搜索結(jié)果的評分。文章來源地址http://www.zghlxwxcb.cn/news/detail-849553.html
索引創(chuàng)建
PUT /products
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"price": {
"type": "float"
}
}
}
}
文檔插入
POST /products/_doc/1
{
"title": "Product A",
"price": 100.0
}
POST /products/_doc/2
{
"title": "Product B",
"price": 200.0
}
POST /products/_doc/3
{
"title": "Product C",
"price": 50.0
}
POST /products/_doc/4
{
"title": "Product D",
"price": 150.0
}
POST /products/_doc/5
{
"title": "Product E",
"price": 75.0
}
POST /products/_doc/6
{
"title": "Product F",
"price": 300.0
}
POST /products/_doc/7
{
"title": "Product G",
"price": 250.0
}
POST /products/_doc/8
{
"title": "Product H",
"price": 125.0
}
查詢語句
GET /products/_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"field_value_factor": {
"field": "price",
"factor": 1,
"modifier": "none",
"missing": 1
}
}
],
"score_mode": "multiply",
"boost_mode": "replace"
}
}
}
到了這里,關(guān)于Elasticsearch(7) field_value_factor的使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!