我具有以下类型的“契约(Contract)”类型:
{
"contract" : {
"properties" : {
"active" : {
"type" : "boolean"
},
[ ... ]
"device" : {
"properties" : {
[ ... ]
"title" : {
"type" : "string"
},
[ ... ]
}
},
"handset_price" : {
"type" : "double"
},
"id" : {
"type" : "string",
"index" : "not_analyzed"
},
[ ... ]
"monthly_price" : {
"type" : "double"
},
[ ... ]
}
}
}
现在,我需要获取给定设备的最便宜的交易(契约(Contract))。这就是我现在所拥有的:
{
"query": {
"match": {
"device.title": device_name
}
},
"size": 1,
"sort":
[
{"monthly_price": {"order": "asc"}},
{"handset_price": {"order": "asc"}}
]
}
device_name
是字符串(例如Lumia 530
)。当我搜索“三星”时,我会获得最便宜的三星Galaxy s3 Mini。问题在于寻找“Samsung Note”会再次给我Galaxy s3,而不是返回现有的三星笔记交易之一。
关于我在做什么错的任何线索吗?
最佳答案
您的问题是,您按价格对结果进行排序,尽管没有考虑结果的相关性。
当搜索“Samsung”时,按照价格升序排序,您将始终以最便宜的“Samsung Galaxy s3 Mini”为第一结果。
如果要首先使用Samsung Note设备,则必须按分数排序(默认排序)。
如果要按分数排序,然后将每个分数值中最便宜的结果作为第一个结果,请在排序中添加“_score”,如下所示:
{
"query": {
"match": {
"device.title": device_name
}
},
"size": 1,
"sort":
[
{"_score": {"order": "desc"}},
{"monthly_price": {"order": "asc"}},
{"handset_price": {"order": "asc"}}
]
}
不建议使用
min_score
,除非针对documentation中所述的某些高级情况。分数是一个相对值,设置绝对阈值会导致奇怪的结果。
关于sorting - ElasticSearch按名称查找并返回最便宜的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29143761/