RSS

Facet dynamic fields with apache solr

21 Jan

By: Nicholas Piasecki

I was in a similar situation when working on an e-commerce platform. Each item had static fields (Price,NameCategory) 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 (BlackWhiteRed, etc.) and Size(SmallMedium, etc.) attributes, whereas a candle in the same store could have a Scent(PumpkinVanilla, 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|BlackSize|Small, andScent|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 inschema.xml so that it can be faceted in the usual way).

Hope this helps someone!

 

Copy from: http://stackoverflow.com/questions/7512392/facet-dynamic-fields-with-apache-solr/14529566#14529566

Advertisements
 
Leave a comment

Posted by on January 21, 2014 in Solr

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: