Solr Faceting on Multiple Concatenated Fields

2019-05-27 15:08发布

问题:

I need a way to get facets on two combined field names. To show you what I mean, take a look at the query as it is now:

{
  "responseHeader":{
    "status":0,
    "QTime":16,
    "params":{
      "facet":"true",
      "indent":"true",
      "q":"productId:(1 OR 2 OR 3 OR 4)",
      "facet.field":["productMetaType",
        "productId"],
      "rows":"10"}},
  "response":{"numFound":4,"start":0,"docs":[
      {
        "productId":1,
        "productMetaType":"PRIMARY_PHOTO",
        "url":"1_PRIM.JPG"},
      {
        "productId":1,
        "productMetaType":"OTHER_PHOTO",
        "url":"1_1.JPG"},
      {
        "productId":1,
        "productMetaType":"OTHER_PHOTO",
        "url":"1_2.JPG"},
      {
        "productId":2,
        "productMetaType":"OTHER_PHOTO",
        "url":"2_1.JPG"}]
  },
  "facet_counts":{
    "facet_queries":{},
    "facet_fields":{
      "productMetaType":[
        "PRIMARY_PHOTO",1,
        "OTHER_PHOTO",3],
      "productId":[
        "1",3,
        "2",1]},
    "facet_dates":{},
    "facet_ranges":{}
    }
}

I get two facet fields, productMetaType and productId. What I need to do is somehow combine those fields so I get data back something like this:

1_PRIMARY_PHOTO, 1,
1_OTHER_PHOTO, 2, 
2_PRIMARY_PHOTO, 0,
2_OTHER_PHOTO, 1

Does the pivot functionality do this? Unfortunately, we're running Solr 3.1, so pivot isn't available, but if that is the only way to do this, I might have some ammo for upgrading.

The only other thing I could think of was some how concatenating the field names. I am new to Solr and don't know what is possible. Any advice or assistance is appreciated. Thank you for your time.

回答1:

Yes, Pivot would work do the trick, but as you observed, this feature is only available in Solr trunk.

Your idea to combine both fields would work too. Actually, if your fields have a limited number of values, the easiest and most flexible way to do this would be to use facet queries:

  • productId:1 AND productMetaType:PRIMARY_PHOTO
  • productId:2 AND productMetaType:OTHER_PHOTO
  • productId:1 AND productMetaType:OTHER_PHOTO
  • productId:2 AND productMetaType:PRIMARY_PHOTO

Otherwise, just create a new field in your Solr schema.xml with string type, recreate your index by adding your documents as previously, but with this new field (that you can generate as you wish, using '_' as a separator between the two field values would work perfectly).



标签: solr