0

I have product information stored in my solr database. A product can be a part of multiple categories.

Now, I want to store information about those categories inside the product which belongs in those categories. (Is there any other way?)

So, say a product A belongs to category C1 and C2 with ids I1 and I2. Now how do i store this mapping of I1 to C1 in my product A? What should be the schema to do so?

But, if a simply store a list of ids, names and some other data(say urls), then the mapping of each id to name or url will be lost. Like this:

<field name="category_ids" type="tints" indexed="true" stored="true"/>
<field name="category_names" type="strings" indexed="true" stored="true"/>

So how should I store documents?

Nikhil Sahu
  • 2,463
  • 2
  • 32
  • 48
  • Actually, a better question is how you are going to **search** those documents and then design index for that. – Alexandre Rafalovitch Oct 20 '15 at 23:09
  • the search can still happen on the list of ids/name that will be maintained alongwith the mapping. But now, any new field need not be added since the order of addition in a multivalued field will be preserved. That will automatically map ids to information in respective fields. – Nikhil Sahu Oct 21 '15 at 11:52

1 Answers1

1

The way you've described works - Solr will keep the sequence between fields the same, so you can assume that the first value in the field category_ids corresponds to the first value in the field category_names. We use this to index more complex objects in several multi value fields.

A second solution is to use the category id to look up the actual category information in your middleware, querying the database for the related information. This will avoid having to reindex all documents for a category if the name changes (except if you use the name for querying, which will require you to do a re-index regardless of solution selected).

A third solution would be to have a field containing both id, name in a serialized form, such as 3;Laptops or as JSON, and just store the field while not indexing it (and use an indexed, non-stored field for actual searching).

You can also use child documents for something like this, but my personal opinion is that it'll give you quite a bit of unnecessary complexity.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • Is the order always guaranteed? Can you point me to some solr documentation that states this? Yeah, I though of using child documents but dropped it because it will make the system unnecessarily complex. If is has to be done this way, then it could be better handled at the middleware. – Nikhil Sahu Oct 21 '15 at 06:48
  • Yes, the order is guaranteed. We've been doing this in production since 2008 IIRC. The best I could find is [this mail thread](https://www.mail-archive.com/solr-user@lucene.apache.org/msg74780.html) about the issue. – MatsLindh Oct 21 '15 at 11:25
  • Cool. That will simplify things a lot. Thanks :) – Nikhil Sahu Oct 21 '15 at 11:47