如何对具有不同优先级的多个ElasticSearch索引进行排序?

iqjalb3h  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(220)

是否可以按索引对ElasticSearch结果进行排序。我不想按索引更改它们的得分。但我有artistssongs索引,我想先显示艺术家结果,然后再显示歌曲,当保留原始得分时更好。
这可能吗?

  • 谢谢-谢谢
fcg9iug3

fcg9iug31#

你可以在一个查询中按_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

相关问题