Solr facet相当于group by?

时间:2018-01-05 14:48:07

标签: sorting solr

如果我有这样的数据:

{"field1":"x", "field2":".."}
{"field1":"x", "field2":".."}
{"field1":"y", "field2":".."}
{"field1":"y", "field2":".."}
{"field1":"y", "field2":".."}

使用简单的group=true&group.field=field1&group.limit=0我得到的结果如下:

{
  "responseHeader":{..}
  "grouped":{
        "field1": {
            "matches": 5,
            "groups": [

                {"groupValue": "x", "doclist":{"numFound": 2, ...}}
                {"groupValue": "y", "doclist":{"numFound": 3, ...}}

            ]
        }

  }
}

使用此功能,我知道为每个groupValuenumFound)找到的文档数量。问题是我需要按降序对结果组进行排序,这对于任何一种排序都是不可能的(简单sort=numFound会导致异常,说字段numFound不存在和组。 sort会对每个组内的文档进行排序。)

是否有相当于使用facets的方法,我可以按计数对结果进行排序?

1 个答案:

答案 0 :(得分:1)

您可以尝试:

http://localhost:8983/solr/your_core/select?facet.field=field1&facet.sort=count&facet.limit=-1&facet=on&indent=on&q=*:*&rows=0&start=0&wt=json

结果如下:

{
  "responseHeader":{
    "status":0,
    "QTime":17,
    "params":{
      "q":"*:*",
      "facet.field":"field1",
      "indent":"on",
      "start":"0",
      "rows":"0",
      "facet":"on",
      "wt":"json"}},
  "response":{"numFound":225364,"start":0,"docs":[]
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "field1":[
        "x",113550,
        "y",111814]},
    "facet_ranges":{},
    "facet_intervals":{},
    "facet_heatmaps":{}
  }
}

刚刚使用Solr 6.3.0测试。

有关详情,请查看Solr documentation中的相关部分。

如果要同时计算可用构面的数量,可以使用Solr stats组件(因为字段的类型为数字,字符串或日期)。
但请记住,可能会出现服务器性能和内存开销问题。

运行如下的查询:

http://localhost:8983/solr/your_core/select?facet.field=field1&facet.sort=count&facet.limit=10&facet=true&indent=on&q=*:*&rows=0&start=0&wt=json&stats=true&stats.field={!cardinality=true}field1

响应类似于:

{
  "responseHeader":{
    "status":0,
    "QTime":614,
    "params":{
      "facet.limit":"10",
      "q":"*:*",
      "facet.field":"field1",
      "indent":"on",
      "stats":"true",
      "start":"0",
      "rows":"0",
      "facet":"true",
      "wt":"json",
      "facet.sort":"count",
      "stats.field":"{!cardinality=true}field1"}},
  "response":{"numFound":2336315,"start":0,"docs":[]
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "field1":[
        "Value1",708116,
        "Value2",607088,
        "Value3",493949,
        "Value4",314433,
        "Value5",104478,
        "Value6",41099,
        "Value7",28879,
        "Value8",18767,
        "Value9",9308,
        "Value10",4545]},
    "facet_ranges":{},
    "facet_intervals":{},
    "facet_heatmaps":{}},
  "stats":{
    "stats_fields":{
      "field1":{
        "cardinality":27}}}}

有关stats的详情,请查看here