Solr Query to search child and parent documents by join

6k views Asked by At

I need help constructing a Solr query that will not only search child documents and return the parent, but also search on the parent. Please see my example schema below:

Manufacturer

  • Id
  • Name
  • Comments

Products

  • Id
  • ManufacturerId
  • ProductName

All Solr documents have a unique ID field, but Products have a special field "ManufacturerId" that acts as a foreign key.

I would like to search all products that have the name "iPod" and return the parent "Manufacturer" documents. I can accomplish by using the following Join statement.

{!join from=ManufacturerId to=Id}ProductName:iPod

In addition, I would like to pull back Manufacturers that have iPod in Comments. Therefore the result would include all manufacturers that have iPod products and have the word iPod in it's comments field. I've tried the following with no luck.

{!join from=ManufacturerId to=Id}ProductName:iPod OR Comments:iPod

Any help would be greatly appreciated.

Thanks!!

UPDATE:

It seems to work correctly when I use the filter query 'fq' field as opposed to the regular query field 'q'. I'm unsure if this is the best solution. Also, I wonder if relevancy will work the same way. Better solutions appreciated.

Comments:iPod {!join from=RouteId to=Id}ProductName:iPod

or

iPod {!join from=RouteId to=Id}ProductName:iPod
2

There are 2 answers

0
Nathan Hall On

Using query tells Solr that another query should be made that will affect the result list.

q=

_query_:"{!join from=ManufacturerId to=Id}ProductName:iPod" OR Comments:iPod
1
Bereng On

If you are really referring to child (aka nested) documents and parents I suggest you to use block join:

https://cwiki.apache.org/confluence/display/solr/Other+Parsers#OtherParsers-BlockJoinQueryParsers

which has been specifically created with that purpose. Add an 'OR' for any other condition and you should be all ok.