If I'm correct, there was a change to the search keyword behavior somewhere around v4.6 such that text keywords are now AND'ed by default by the native search engine. This matches generally expected search behavior, as modeled by Google, and seems like the right thing to do.
However, when we mix text terms and and taxonomy (category) terms, the categories are always boolean OR'ed in the resultant SQL query (see line # 653 in node.module):
if ($category = search_query_extract($keys, 'category')) {
$categories = array();
foreach (explode(',', $category) as $c) {
$categories[] = "tn.tid = %d";
$arguments1[] = $c;
}
$conditions1 .= ' AND ('. implode(' OR ', $categories) .')';
$join1 .= ' INNER JOIN {term_node} tn ON n.nid = tn.nid';
$keys = search_query_insert($keys, 'category');
}
This is unfortunate in my opinion and seems contrary to the default AND behavior of the text search.
There does not seem to be an easy way to switch this to an AND search, since the INNER JOIN that is part of this search query limits the term ID values to a single column in the query results -- therefore only an OR comparison can be made. It seems that either a new approach or a sub-query is required to access all nodes with a range of Term ID associations, in a boolean AND manner.
Has this problem been solved already? Are we missing something obvious?
Thanks in advance for any guidance or suggestions.
Comments
Difficult query
The right way to do it would be to do a grouping query on the nid where you use e.g. HAVING to select only those aggregated rows that have each of the desired terms. This is tricky to implement within the frame of search queries... but not impossible.
Still, the current behaviour makes a lot of sense if you use e.g. forums. In that case, people will want to search for results in a subset of all forums, i.e. all posts that appear in forum A or B. So I'm not sure how this should be addressed. Both systems have merit.
--
If you have a problem, please search before posting a question.
A Lack of Choice
I guess the real problem is that we do not have a choice of OR vs. AND when it comes to categories. This is surprising since any advanced search form would typically have AND or OR as an option and just in general Google and the like have trained the world to expect AND searches as default behavior.
Does anyone out there have a hack or similar that would allow us to implement an AND search with categories and keywords?
This would be greatly appreciated.
Same requirments here!
I need to implement something similar on drupal 6. Could anyone can help with this?