Question

I am implemeting Lucene for an ecommerce system and need to index the products along with their attributes. However, I'm not sure how to approach this as every product will have a totally unique attribute list.

Most examples online show a lucene access layer with a product structure such as Name / Title / Description, sometimes even with a custom field which just gets added for every product.

An example of two products that would need to be indexed are shown below. As you can see in this case (although not always) there are similar attributes, but also unique ones.

ID - 1
Product - Electric Shower
Name - Triton t80Z
Description - Details about shower here...
Attributes
    Color - Black
    Power Rating - 7.5Kw
    Temperature Control - Manual
    Water Entry - Top Left

ID - 2
Product - Digital Shower
Name - Grohe Grotherm
Description - Details about shower here...
Attributes
    Color - Chrome
    Temperature Control - Thermostatic
    Water Entry - Top Left
    Flow Limit - 8 Litre/min
    LCD Display - True
    Control Panel - Wireless
    Control Panel Range - 10m

Given this situation, how would I index the above products and create a query in lucence to find any products across the site that contain a Temperature Control attribute which is Thermostatic?

Était-ce utile?

La solution

Unlike with databases, Lucene has no schema (at least in the classical DB sense) so you are free to add any attributes (they're called fields) at any given time. Just create a new Field, with the relevant name/value, add it to the Document and that's it.

Q> how would I <..> create a query in lucence to find any products across the site that contain a Temperature Control attribute which is Thermostatic?

A> something along the lines of the following should just work, providing you will use the same analyzer which is used for indexing the document:

Query q = new AnalyzingQueryParser(<params>).parse("temperature_control:Thermostatic");

Going deeper into details, it depends if values for Temperature Control come from a pre-defined list, and how you want them to be searchable (exact match VS separate words, etc.). These will define settings for your analyzer.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top