solr控制数据的返回的排序只能使用已有的字段(字段必须索引)。
使用facet只能返回分组的次数,如:
{
"responseHeader":{
"status":0,
"QTime":9,
"params":{
"q":"post_content:\"nike\"",
"facet.field":"uid",
"indent":"on",
"rows":"0",
"facet":"on",
"wt":"json",
"_":"1567574307269"}},
"response":{"numFound":33679,"start":0,"docs":[]
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"uid":[
"56616",2272,
"59212",1393,
"71466",1289,
"820",1060,
"58190",693,
"344",675,
"58559",524,
"95849",439,
"97810",412,
"56635",405,
"70449",356,
"61959",269,
"63930",250,
"57823",245,
"60380",238,
"84618",235,
"102601",225]},
"facet_ranges":{},
"facet_intervals":{},
"facet_heatmaps":{}}}
使用group可以返回数据,但是不能以numFound排序,如:
{
"responseHeader":{
"status":0,
"QTime":54,
"params":{
"q":"post_content:\"nike\"",
"indent":"on",
"fl":"id,post_time",
"group.limit":"3",
"group.ngroups":"true",
"rows":"3",
"wt":"json",
"group.field":"uid",
"_":"1567574501345",
"group":"true"}},
"grouped":{
"uid":{
"matches":33679,
"ngroups":5238,
"groups":[{
"groupValue":"412",
"doclist":{"numFound":5,"start":0,"docs":[
{
"id":"15874598",
"post_time":"20190226"},
{
"id":"21279196",
"post_time":"20190712"},
{
"id":"19830647",
"post_time":"20190606"}]
}},
{
"groupValue":"70134",
"doclist":{"numFound":10,"start":0,"docs":[
{
"id":"17550386",
"post_time":"20190418"},
{
"id":"17678823",
"post_time":"20190422"},
{
"id":"20580963",
"post_time":"20190624"}]
}},
{
"groupValue":"89899",
"doclist":{"numFound":92,"start":0,"docs":[
{
"id":"18440190",
"post_time":"20190509"},
{
"id":"13451713",
"post_time":"20181205"},
{
"id":"19047413",
"post_time":"20190521"}]
}}]}}}
如果需要以分组numFound排序返回数据,可将两者结合起来。
1、先使用facet查询提到次数最多的10个分组,如:facet.field=uid&facet.limit=10&facet=on&indent=on&q=post_content:"nike"&rows=0&wt=json
返回:
{
"responseHeader":{
"status":0,
"QTime":3,
"params":{
"facet.limit":"10",
"q":"post_content:\"nike\"",
"facet.field":"uid",
"indent":"on",
"rows":"0",
"facet":"on",
"wt":"json",
"_":"1567574501345"}},
"response":{"numFound":33679,"start":0,"docs":[]
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"uid":[
"56616",2272,
"59212",1393,
"71466",1289,
"820",1060,
"58190",693,
"344",675,
"58559",524,
"95849",439,
"97810",412,
"56635",405]},
"facet_ranges":{},
"facet_intervals":{},
"facet_heatmaps":{}}}
2、把查到的10个分组带到group中的条件中,如:
group.field=uid&group.limit=2&group.ngroups=true&group=true&indent=on&q=post_content:"nike"%20AND%20uid:("56616","59212","71466","820","58190","344","58559","95849","97810","56635")&wt=json
返回:
{
"responseHeader":{
"status":0,
"QTime":10,
"params":{
"q":"post_content:\"nike\" AND uid:(\"56616\",\"59212\",\"71466\",\"820\",\"58190\",\"344\",\"58559\",\"95849\",\"97810\",\"56635\")",
"indent":"on",
"fl":"id,uid,post_time",
"group.limit":"2",
"group.ngroups":"true",
"wt":"json",
"group.field":"uid",
"_":"1567574501345",
"group":"true"}},
"grouped":{
"uid":{
"matches":9162,
"ngroups":10,
"groups":[{
"groupValue":"95849",
"doclist":{"numFound":439,"start":0,"docs":[
{
"uid":"95849",
"id":"12472434",
"post_time":"20181029"},
{
"uid":"95849",
"id":"13150231",
"post_time":"20181123"}]
}},
{
"groupValue":"97810",
"doclist":{"numFound":412,"start":0,"docs":[
{
"uid":"97810",
"id":"17011576",
"post_time":"20190401"},
{
"uid":"97810",
"id":"20836190",
"post_time":"20190702"}]
}},
{
"groupValue":"71466",
"doclist":{"numFound":1289,"start":0,"docs":[
{
"uid":"71466",
"id":"9568951",
"post_time":"20180711"},
{
"uid":"71466",
"id":"12310677",
"post_time":"20181023"}]
}},
{
"groupValue":"344",
"doclist":{"numFound":675,"start":0,"docs":[
{
"uid":"344",
"id":"16181520",
"post_time":"20190307"},
{
"uid":"344",
"id":"337442",
"post_time":"20151204"}]
}},
{
"groupValue":"59212",
"doclist":{"numFound":1393,"start":0,"docs":[
{
"uid":"59212",
"id":"17192718",
"post_time":"20190408"},
{
"uid":"59212",
"id":"7620333",
"post_time":"20180316"}]
}},
{
"groupValue":"820",
"doclist":{"numFound":1060,"start":0,"docs":[
{
"uid":"820",
"id":"15442810",
"post_time":"20190213"},
{
"uid":"820",
"id":"10022689",
"post_time":"20180731"}]
}},
{
"groupValue":"58559",
"doclist":{"numFound":524,"start":0,"docs":[
{
"uid":"58559",
"id":"9235985",
"post_time":"20180627"},
{
"uid":"58559",
"id":"14815380",
"post_time":"20190121"}]
}},
{
"groupValue":"56635",
"doclist":{"numFound":405,"start":0,"docs":[
{
"uid":"56635",
"id":"3023676",
"post_time":"20160415"},
{
"uid":"56635",
"id":"7508607",
"post_time":"20180309"}]
}},
{
"groupValue":"58190",
"doclist":{"numFound":693,"start":0,"docs":[
{
"uid":"58190",
"id":"6323225",
"post_time":"20171114"},
{
"uid":"58190",
"id":"5452944",
"post_time":"20170804"}]
}},
{
"groupValue":"56616",
"doclist":{"numFound":2272,"start":0,"docs":[
{
"uid":"56616",
"id":"10634218",
"post_time":"20180820"},
{
"uid":"56616",
"id":"10634219",
"post_time":"20180820"}]
}}]}}}
使用代码根据numFound排序(逆序),得到正确数据。