如何根据字段值提升结果
How to boost results on the basis of fields values
我一直在为这个问题创建查询。该查询将进行关键字搜索以及产品评分。
我有这些产品列表
[
{
name: 'pro',
clickCount: 110,
bookingCount: 57,
isPromoted: 0,
},
{
name: 'prod',
clickCount: 13,
bookingCount: 77,
isPromoted: 0,
},
{
name: 'produ',
clickCount: 43,
bookingCount: 10,
isPromoted: 0,
},
{
name: 'produc',
clickCount: 5,
bookingCount: 17,
isPromoted: 0,
},
{
name: 'product',
clickCount: 89,
bookingCount: 67,
isPromoted: 0,
},
{
name: 'products',
clickCount: 1,
bookingCount: 2,
isPromoted: 1,
},
{
name: 'products2',
clickCount: 3,
bookingCount: 4,
isPromoted: 1,
},
]
我需要按以下顺序对这些进行排序:
- isPromoted=1 的产品将排在最前面(如果有多个产品具有相同的值,则具有 MAX(clickCount+bookingCount) 的产品将优先。
- 具有 MAX(clickCount+bookingCount) 的产品将在其后出现。
关键字搜索 pro 的最终排序应该是这样的:
[
{
name: 'products2',
clickCount: 3,
bookingCount: 4,
isPromoted: 1,
},
{
name: 'products',
clickCount: 1,
bookingCount: 2,
isPromoted: 1,
},
{
name: 'pro',
clickCount: 110,
bookingCount: 57,
isPromoted: 0,
},
{
name: 'product',
clickCount: 89,
bookingCount: 67,
isPromoted: 0,
},
{
name: 'prod',
clickCount: 13,
bookingCount: 77,
isPromoted: 0,
},
{
name: 'produ',
clickCount: 43,
bookingCount: 10,
isPromoted: 0,
},
{
name: 'produc',
clickCount: 5,
bookingCount: 17,
isPromoted: 0,
},
]
我正在编写此查询,但没有给出所需的结果。
GET /products/_search
{
"query": {
"function_score": {
"query": {
"wildcard": {
"name": "*pro*"
}
},
"functions": [
{
"field_value_factor": {
"field": "clickCount",
"factor": 1.2,
"modifier": "sqrt"
}
},
{
"field_value_factor": {
"field": "clickCount",
"factor": 1,
"modifier": "sqrt"
}
},
{
"field_value_factor": {
"field": "isPromoted",
"factor": 1.5,
"modifier": "sqrt"
},
"weight": 20
}
],
"boost_mode": "sum"
}
}
}
对于您想要的,我认为您可以在运行时计算一个 totalCount
字段,其中 returns 计数的总和。那么您只需先使用 isPromoted
字段进行排序,然后再使用 totalCount
字段进行排序:
映射:
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"clickCount": {
"type": "integer"
},
"bookingCount": {
"type": "integer"
},
"isPromoted": {
"type": "integer"
}
}
}
}
搜索查询:
{
"runtime_mappings": {
"totalCount": {
"type": "long",
"script": "emit(doc['clickCount'].value + doc['bookingCount'].value);"
}
},
"query": {
"match_all": {}
},
"sort": [
{"isPromoted": "desc"},
{"totalCount": "desc"}
]
}
我一直在为这个问题创建查询。该查询将进行关键字搜索以及产品评分。 我有这些产品列表
[
{
name: 'pro',
clickCount: 110,
bookingCount: 57,
isPromoted: 0,
},
{
name: 'prod',
clickCount: 13,
bookingCount: 77,
isPromoted: 0,
},
{
name: 'produ',
clickCount: 43,
bookingCount: 10,
isPromoted: 0,
},
{
name: 'produc',
clickCount: 5,
bookingCount: 17,
isPromoted: 0,
},
{
name: 'product',
clickCount: 89,
bookingCount: 67,
isPromoted: 0,
},
{
name: 'products',
clickCount: 1,
bookingCount: 2,
isPromoted: 1,
},
{
name: 'products2',
clickCount: 3,
bookingCount: 4,
isPromoted: 1,
},
]
我需要按以下顺序对这些进行排序:
- isPromoted=1 的产品将排在最前面(如果有多个产品具有相同的值,则具有 MAX(clickCount+bookingCount) 的产品将优先。
- 具有 MAX(clickCount+bookingCount) 的产品将在其后出现。
关键字搜索 pro 的最终排序应该是这样的:
[
{
name: 'products2',
clickCount: 3,
bookingCount: 4,
isPromoted: 1,
},
{
name: 'products',
clickCount: 1,
bookingCount: 2,
isPromoted: 1,
},
{
name: 'pro',
clickCount: 110,
bookingCount: 57,
isPromoted: 0,
},
{
name: 'product',
clickCount: 89,
bookingCount: 67,
isPromoted: 0,
},
{
name: 'prod',
clickCount: 13,
bookingCount: 77,
isPromoted: 0,
},
{
name: 'produ',
clickCount: 43,
bookingCount: 10,
isPromoted: 0,
},
{
name: 'produc',
clickCount: 5,
bookingCount: 17,
isPromoted: 0,
},
]
我正在编写此查询,但没有给出所需的结果。
GET /products/_search
{
"query": {
"function_score": {
"query": {
"wildcard": {
"name": "*pro*"
}
},
"functions": [
{
"field_value_factor": {
"field": "clickCount",
"factor": 1.2,
"modifier": "sqrt"
}
},
{
"field_value_factor": {
"field": "clickCount",
"factor": 1,
"modifier": "sqrt"
}
},
{
"field_value_factor": {
"field": "isPromoted",
"factor": 1.5,
"modifier": "sqrt"
},
"weight": 20
}
],
"boost_mode": "sum"
}
}
}
对于您想要的,我认为您可以在运行时计算一个 totalCount
字段,其中 returns 计数的总和。那么您只需先使用 isPromoted
字段进行排序,然后再使用 totalCount
字段进行排序:
映射:
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"clickCount": {
"type": "integer"
},
"bookingCount": {
"type": "integer"
},
"isPromoted": {
"type": "integer"
}
}
}
}
搜索查询:
{
"runtime_mappings": {
"totalCount": {
"type": "long",
"script": "emit(doc['clickCount'].value + doc['bookingCount'].value);"
}
},
"query": {
"match_all": {}
},
"sort": [
{"isPromoted": "desc"},
{"totalCount": "desc"}
]
}