Here's a quick overview of my project and the bug:

I have a CCK content type of "Themes".
I have a Taxonomy vocabulary of "Theme Colors".
In Theme Colors there are terms such as "Red, Blue, Green, etc..."
I use CCK's "Content Taxonomy Fields" to add "Theme Colors" as a field for "Themes".

For examples sake:

"Theme A" has colors "Red" & "Blue"
"Theme B" has colors "Green", "Yellow" & "Blue"

The Faceted Search shows the following values in the Guided Search block:

Blue (2)
Green (1)
Red (1)
Yellow (1)

When I click "Blue", I get 2 results. When I click any of the other colors, I get one result. Perfect!

Now the problem comes when I edit a node. In this example, I edit "Theme B". I change "Theme B" so that the colors are Green & Yellow and remove Blue.

Now my Guide Search block shows this:

Blue (1)
Green (1)
Red (1)
Yellow (1)

But when I click on "Blue", I get BOTH "Theme A" and "Theme B" in the results. I've repeated this type of testing over and over again and I'm 99% sure this erroneous result only occurs when a node's taxonomy values have been edited. The Faceted Search works perfectly until you change a node's taxonomy field and then it fails.

So I used Firebug for Drupal to take a look at the queries generated and I think I have found out what is going on... well, I'm 80% sure on this:

In the term_node table, which is the table that Faceted Search uses when creating the TEMP table, I see the following:

nid		vid		tid
++++++++++++++++++++
37		85		93
39		87		93

And here is what the nid and tid values represent:

37 = "Theme A"
39 = "Theme B"
93 = "Blue"

So, I've removed "Blue" from "Theme B" but it's still showing the relationship. This is because of the vid value which refers to the revision value in the node_revisions table.

When I take a look at the node_revisions table I find the following rows:

nid		vid		title
++++++++++++++++++++++++
37		85		Theme A
39		87		Theme B
39		88		Theme B

So basically, from what I can tell, it's accessing the first record but that is the OLD record. It needs to access the node with the highest value for vid, which is the most recent revision.

So, to over simplify the CREATE TABLE query, it currently looks like this:

SELECT n.nid AS nid
FROM node AS n 
INNER JOIN term_node AS term_node_93 
	ON n.nid = term_node_93.nid 
WHERE ((n.status = 1) AND (n.type IN ('theme')) AND (term_node_93.tid = 93)) 
GROUP BY n.nid ASC 

This query returns:

nid
++++
37
39

But I think it should look like this:

SELECT n.nid AS nid
FROM node AS n 
INNER JOIN term_node AS term_node_93 
	ON n.nid = term_node_93.nid 
WHERE ((n.status = 1) AND (n.type IN ('theme')) AND (term_node_93.tid = 93) AND (n.vid=term_node_93.vid) ) 
GROUP BY n.nid ASC 

This query returns:

nid
++++
37

Which is correct because 37 is "Theme A" and that is the only node with a taxonomy value of "Blue".

So, I think I fixed this bug by applying the following modification below. BUT I WOULD LOVE IF AN EXPERT COULD DOUBLE CHECK MY MODIFICATION. I have no idea if it's written properly but it seems to work (after very minimal testing).

I edited the "taxonomy_facets.module" and change this:

  function build_results_query(&$query) {
    // Since multiple terms might be used and cause multiple joins of
    // taxonomy_facets_term_node, we add the tid into the table alias to ensure
    // a unique alias.
    $query->add_table('term_node', 'nid', 'n', 'nid', "term_node_{$this->_tid}");
    $query->add_where("term_node_{$this->_tid}.tid = %d", $this->_tid);
  }

To this:

  function build_results_query(&$query) {
    // Since multiple terms might be used and cause multiple joins of
    // taxonomy_facets_term_node, we add the tid into the table alias to ensure
    // a unique alias.
    $query->add_table('term_node', 'nid', 'n', 'nid', "term_node_{$this->_tid}");
    $query->add_where("term_node_{$this->_tid}.tid = %d", $this->_tid);
    $query->add_where("term_node_{$this->_tid}.vid = n.vid");
  }

OK, so I hope the Faceted Search developer or someone with FS expertise can take a look at this modification.

Thanks again for one of the best modules for the Drupal project!!!

CommentFileSizeAuthor
#2 taxonomy_facets.patch776 bytesKristi Wachter
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

vlad.k’s picture

Hi, I'm no expert, but I had the same issue and the described solution solved my issue. I think that this should be commited.

Kristi Wachter’s picture

Status: Active » Needs review
FileSize
776 bytes

Here's a patch for this fix.

Note that I've added the new line to both build_results_query() functions.

Testing is definitely needed.

YK85’s picture

subscribing

David ROUSSE’s picture

Hi.

Just for information, the patch provided by Kristi Wachter (post #2 above) does not work for version 6.x-1.x-dev. For this version, I only added the following line in the first function build_results_query of taxonomy_facets.module and it worked :-)

$query->add_where("term_node_{$this->_tid}.vid = n.vid");

Hope this help.
David.

David Lesieur’s picture

Status: Needs review » Closed (duplicate)

Although this issue was there first, #1034006: Results from previous node revisions being returned has now been committed to fix the same problem, so I'm marking this one as duplicate.