是否可以按索引对ElasticSearch结果进行排序。我不想按指数改变他们的分数。但是我有artists和songs索引,我想先显示艺术家的结果,然后再显示歌曲,当保持原始分数时更好。
这有可能吗?
谢谢。
发布于 2022-11-15 00:28:00
您可以在查询中按_index进行排序(如以下所示);
POST artist/_doc/1
{
"user_id" : 1234,
"acc_id" : 4321,
"type_of_event" : "event",
"event_label" : "example activity",
"time_stamp" : 1582720371,
"event_info" : "example info"
}
POST songs/_doc/2
{
"user_id" : 1234,
"acc_id" : 4321,
"type_of_event" : "event",
"event_label" : "example activity",
"time_stamp" : 1582760371,
"event_info" : "example info"
}
POST artist/_doc/3
{
"user_id" : 12345,
"acc_id" : 4321,
"type_of_event" : "event",
"event_label" : "example activity",
"time_stamp" : 1582720371,
"event_info" : "example info"
}
POST songs/_doc/4
{
"user_id" : 1235,
"acc_id" : 4321,
"type_of_event" : "event",
"event_label" : "example activity",
"time_stamp" : 1592720371,
"event_info" : "example info"
}
POST /artist,songs/_search
{
"sort" : [ { "_index" : {"order" : "desc" }} ]
}输出为;
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "songs",
"_id": "2",
"_score": null,
"_source": {
"user_id": 1234,
"acc_id": 4321,
"type_of_event": "event",
"event_label": "example activity",
"time_stamp": 1582760371,
"event_info": "example info"
},
"sort": [
"songs"
]
},
{
"_index": "songs",
"_id": "4",
"_score": null,
"_source": {
"user_id": 1235,
"acc_id": 4321,
"type_of_event": "event",
"event_label": "example activity",
"time_stamp": 1592720371,
"event_info": "example info"
},
"sort": [
"songs"
]
},
{
"_index": "artist",
"_id": "1",
"_score": null,
"_source": {
"user_id": 1234,
"acc_id": 4321,
"type_of_event": "event",
"event_label": "example activity",
"time_stamp": 1582720371,
"event_info": "example info"
},
"sort": [
"artist"
]
},
{
"_index": "artist",
"_id": "3",
"_score": null,
"_source": {
"user_id": 12345,
"acc_id": 4321,
"type_of_event": "event",
"event_label": "example activity",
"time_stamp": 1582720371,
"event_info": "example info"
},
"sort": [
"artist"
]
}
]
}
}如果愿意,可以将desc更改为asc
https://stackoverflow.com/questions/74374729
复制相似问题