views:

317

answers:

2

Hi all,

I need to create a somewhat advanced search functionality for my Drupal 6 site. I have a one-to-many relationship between two content types and need to search them, respecting that relationship.

To make things more clear...

I have content types TypeX and TypeY. TypeY has a node reference CCK field that relates it to a single node of TypeX. So, many nodes of TypeY reference the same node of TypeX.

I want to use Views 2 to create a search page for these nodes. I want each search result to be a node of TypeX, along with all the nodes of TypeY that reference it. I know I could just theme the individual results and use a view to add the nodes of TypeY to the single node of TypeX... but that won't allow users to actually search TypeY... it would only search TypeX and merely display some nodes of TypeY along with it.

Is there anyway to get the search to account for content in nodes of both content types, but merge the TypeY results into the "parent" node of TypeX?

In database terms, it seems like I need to do a join, then filter by the search terms. But I can't figure out how to do this in Views.

Thanks for any help i can get!!!

'** EDIT '**

To make sure this is clear, here's a visual example of what I'm trying to do...

_____________________________________________________________________________________
|Type X Node                           |Type Y Node                                  |
|                                      |                                             |
| Content in node of TypeX that gets   |Has a node reference that points to the node |
| searched by the view...              |of TypeX. This content gets searched too!    |
|                                      |_____________________________________________|
|                                      |Type Y Node                                  |
|                                      |                                             |
|                                      |Another TypeY node with a node reference to  |
|                                      |to the TypeX node. This gets searched too!   |
--------------------------------------------------------------------------------------

This would be a single result from the search View. They content in all three nodes would be considered by the Search : Terms filter in the view. So if I searched for "This gets searched too!", I would get the above result. Or, if I searched "Content in node of TypeX", I'd get the same search result above.

I know I could search for TypeX and load up the TypeY nodes in the result display using another view, but it wouldn't search the content in the TypeY nodes.

Any ideas... short of rolling my own search functionality (not something I'd like to do at the moment)?

A: 

Provided you can get your view to a point where it is searching both content types, I think this actually does become a theme issue. You could override the various theme templates for the view and organize the returned array of nodes in a particular way. You could also simply omit content type Y from the list during output and theme the content type X node to list the nodes from the CCK node reference field (this would be similar to what you mentioned, but you'd still leave content type Y in the view query and omit it during output). There might be some performance implications depending on the amount of content type Y nodes per content type X nodes there are.

I think no matter what you end up doing this will be a theme issue albeit a complex one.

CalebD
So basically your saying, let it search both content types and return the results as it normally would (Y nodes and X nodes being separate results). Then in my theme, handle the functionality of merging the various results as I have displayed above?
Chaulky
I'm not entirely familiar with "Search : Terms" filter, so maybe that throws a wrench in it, but my thought was having the view have two filters: "Search : Terms" and "Content Type Is One Of Content Type X or Content Type Y". When submitted it would return nodes of both content type x and content type y and you could use some PHP foo on the theme / preprocess side to output what you need (e.g. output only content type x and then customize the theme template for content type x to display content type y, similar to what you proposed, or hack at the node list from the view and rearrange it).
CalebD
Well, I was really hoping for a more elegant solution using Views functionality to merge the results. But it doesn't seem like its going to happen. I think I will have to just let it return both content types (the filter Content Type Is One of...), because then the Search : Terms filter should apply to both types. And in the template for the view results, I'll have to manually find the TypeY nodes that belong to each TypeX node returned and merge them. It is rather hackish, but it just might work.If anyone knows how to do it in Views... PLEASE let me know.
Chaulky
A: 

Wow, just wow. I finally found an elegant solution to this problem. I can't believe how simple it turns out to be!!!

I won't take credit for the solution... I stumbled upon a great blog post that answered this question completely (wasn't even looking anymore!). The article is by a guy name Davy, and he's my new hero.

The article is here: http://www.drupalcoder.com/story/667-improving-search-results-when-working-with-node-references-in-drupal

He also wrote a follow up article that simplifies the process even more!! http://www.drupalcoder.com/story/696-a-better-alternative-for-improving-search-results-when-working-with-node-references-in-dru

It turns out the good folks that wrote CCK had already thought of indexing a referenced node's content. In the Display Fields section of editing a content type, click on the Search option! It lets you determine how the referenced node should be indexed when the parent node is indexed... and you can set it to full node. This will index the full contents of the referenced node as part of indexing the parent node. The only thing left to do (which Davy explains) is to make sure the parent node gets reindexed whenever a referenced node's content changes.

Chaulky