8

I have defined dynamic field in ApacheSolr:

I use it to store products features like: color_feature, diameter_feature, material_feature and so on. Number of those fields are not constant becouse products are changing.

Is it possible to get facet result for all those dynamic fields with the same query or do I need to write always all fields in a query like ... facet.field=color_feature&facet.field=diameter_feature&facet.field=material_feature&facet.field=...

Jayendra
  • 52,349
  • 4
  • 80
  • 90
krinn
  • 882
  • 2
  • 10
  • 16

2 Answers2

6

I was in a similar situation when working on an e-commerce platform. Each item had static fields (Price, Name, Category) that easily mapped to SOLR's schema.xml, but each item could also have a dynamic amount of variations.

For example, a t-shirt in the store could have Color (Black, White, Red, etc.) and Size (Small, Medium, etc.) attributes, whereas a candle in the same store could have a Scent (Pumpkin, Vanilla, etc.) variation. Essentially, this is an entity-attribute-value (EAV) relational database design used to describe some features of the product.

Since the schema.xml file in SOLR is flat from the perspective of faceting, I worked around it by munging the variations into a single multi-valued field ...

<field
  name="variation"
  type="string"
  indexed="true"
  stored="true"
  required="false"
  multiValued="true" />

... shoving data from the database into these fields as Color|Black, Size|Small, and Scent|Pumpkin ...

  <doc>
    <field name="id">ITEM-J-WHITE-M</field>
    <field name="itemgroup.identity">2</field>
    <field name="name">Original Jock</field>
    <field name="type">ITEM</field>
    <field name="variation">Color|White</field>
    <field name="variation">Size|Medium</field>
  </doc>
  <doc>
    <field name="id">ITEM-J-WHITE-L</field>
    <field name="itemgroup.identity">2</field>
    <field name="name">Original Jock</field>
    <field name="type">ITEM</field>
    <field name="variation">Color|White</field>
    <field name="variation">Size|Large</field>
  </doc>
  <doc>
    <field name="id">ITEM-J-WHITE-XL</field>
    <field name="itemgroup.identity">2</field>
    <field name="name">Original Jock</field>
    <field name="type">ITEM</field>
    <field name="variation">Color|White</field>
    <field name="variation">Size|Extra Large</field>
  </doc>

... so that when I tell SOLR to facet, then I get results that look like ...

<lst name="facet_counts">
  <lst name="facet_queries"/>
  <lst name="facet_fields">
    <lst name="variation">
      <int name="Color|White">2</int>
      <int name="Size|Extra Large">2</int>
      <int name="Size|Large">2</int>
      <int name="Size|Medium">2</int>
      <int name="Size|Small">2</int>
      <int name="Color|Black">1</int>
    </lst>
  </lst>
  <lst name="facet_dates"/>
  <lst name="facet_ranges"/>
</lst>

... so that my code that parses these results to display to the user can just split on my | delimiter (assuming that neither my keys nor values will have a | in them) and then group by the keys ...

Color
    White (2)
    Black (1)
Size
    Extra Large (2)
    Large (2)
    Medium (2)
    Small (2)

... which is good enough for government work.

One disadvantage of doing it this way is that you'll lose the ability to do range facets on this EAV data, but in my case, that didn't apply (the Price field applying to all items and thus being defined in schema.xml so that it can be faceted in the usual way).

Hope this helps someone!

Nicholas Piasecki
  • 25,203
  • 5
  • 80
  • 91
  • 1
    Does this have any benefits over dynamic fields? – eadam Aug 22 '13 at 05:04
  • As you mentioned, this solution won't allow range facets. I understand your argument for price. But what about quantity? It can be different for each variant. So, how did you store quantity of each variant? I want to filter search on only available variants. Is there a way to achieve this? – nish Jul 26 '14 at 12:02
  • 1
    Does anyone know if current versions of Solr/Lucene support facet on dynamic fields? – ypriverol Apr 09 '18 at 18:55
6

Solr currently does not support wildcards in the facet.field parameter.
So *_feature won't work for you.

May want to check on this - https://issues.apache.org/jira/browse/SOLR-247

If you don't want to pass parameters, you can easily add these to your request handler defaults.

The qt=requesthandler in request would always include these facets.

Jayendra
  • 52,349
  • 4
  • 80
  • 90
  • Good response, but addind a requestHandler kinda misses the point. If the fields are dynamic the requestHandler would have to be dynamic too. Which is not possible. – wdev Nov 19 '12 at 11:00
  • Yes .. If the fields added are completely dynamic you can't use a Request handler with fixed facets. But you can define a fixed set of list using the append mode and pass the rest in the query. Can reduce the parameters in URL. – Jayendra Nov 19 '12 at 11:55