我已在Elasticsearch中索引了一系列歌曲数据,例如
{ID:1, Title:Bailamos, Genre:Pop, Artist:Enrique}, {ID:2, Title:Ring of Fire, Genre:Country, Artist:Johnny Cash}, {ID:3, Title:Free Bird, Genre:Country, Artist:Lynyrd Skynyrd}, {ID:4, Title:Immigrant Song,Genre:Rock, Artist:Led Zeppelin}, {ID:5, Title:Jump, Genre:Rock, Artist:Van Helen}
现在我需要根据他们的特定类型对歌曲进行分组。因此,它将显示为:
Country - 2 songs, Rock - 2 songs, Pop - 1 song
目前,我正在使用聚合来获得此结果。
{ "query": { "bool": { "must": [ { "match_all": {} } ] } }, "aggs": { "SongGroups": { "terms": { "field": "Genre", "size": 10, "order":{ "_score":"asc" } }, "aggs": { "top_Song_hits": { "top_hits": { "sort": [ { "Title": { "order": "desc" } } ], "size": 10 } } } } } }
现在问题是,我需要通过其他字段对类型组进行排序,比如' Title'或者'艺术家'。应根据该类型的第一首匹配歌曲的字段数据对类型组进行排序。
例如,如果按艺术家排序,结果应为:
Pop - 1 song (since first artist is Enrique) Country - 2 songs, (since first artist is Johnny Cash) Rock - 2 songs, (since first artist is Led Zeppelin)
但是,我发现Elasticsearch不支持在聚合中按字段排序。 那么,有什么方法可以通过一次调用Elasticsearch实现这一目标吗?或者是否有任何其他解决办法可以达到预期效果?在此先感谢您提供任何帮助。
PS。如果列表很大,也会应用分页。