我在尝试让function_score为价格字段工作时遇到了麻烦。最初我尝试使用scaled_float字段。然而它并不喜欢这样。因此,我将我的价格字段更改为带有小数位填充的长整数。所以我的字段为“15000”,价格为150.00美元。
这是我对/ products_v7 / product / _search
的查询 {
"explain":true,
"query":{
"function_score":{
"query":{
"bool":{
"should":[
{
"match_phrase":{
"title":{
"query":"test",
"slop":10
}
}
}
]
}
},
"functions":[
{
"gauss":{
"price":{
"origin":"15000",
"scale":"2000"
}
},
"weight":2
}
]
}
}
}
以下是回复
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "unknown field [price]",
"line": 1,
"col": 0
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "products_v7",
"node": "cd3yjjoSSxKxaJ-vCB8SgQ",
"reason": {
"type": "parsing_exception",
"reason": "unknown field [price]",
"line": 1,
"col": 0
}
}
]
},
"status": 400
}
映射/ products_v7 / product / _mapping
{
"products_v7": {
"mappings": {
"product": {
"properties": {
"price:": {
"type": "long"
},
"sku": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
这是我推送的数据
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "products_v7",
"_type": "product",
"_id": "cSULBGMBog6d8NyO0gRH",
"_score": 1,
"_source": {
"sku": "126",
"title": "test 4",
"price:": 15000
}
},
{
"_index": "products_v7",
"_type": "product",
"_id": "fl0FBGMBog0jN_eMK89-",
"_score": 1,
"_source": {
"sku": "125",
"title": "test 3",
"price:": 13000
}
}
]
}
}
答案 0 :(得分:2)
问题是在你的文档中,字段price
被称为price:
(即你的名字中有一个冒号。这些细节很重要。
{
"sku": "126",
"title": "test 4",
"price:": 15000
^
|
see here
}