我对Drupal非常陌生,尝试构建一个模块,该模块允许管理员使用关键字标记节点以将节点提升到搜索结果顶部。在Drupal 7中更改数据集的搜索查询条件
我有一个单独的数据库表的关键字和各自的节点ID。此表是联合在一起通过hook_query_alter的search_index表...
function mos_search_result_forcer_query_alter(QueryAlterableInterface &$query) {
if (get_class($query) !== 'PagerDefault') { //<< check this because this function will mod all queries elsewise
return;
}
// create unioned search index result set...
$index = db_select('search_index', 's');
$index->addField('s', 'sid');
$index->addField('s', 'word');
$index->addField('s', 'score');
$index->addField('s', 'type');
$msrfi = db_select('mos_search_result_forcer', 'm');
$msrfi->addField('m', 'nid', 'sid');
$msrfi->addField('m', 'keyword', 'word');
$msrfi->addExpression('(SELECT MAX(score) + m.id/(SELECT MAX(id) FROM {mos_search_result_forcer}) FROM {search_index})', 'score');
$msrfi->addExpression(':type', 'type', array(':type' => 'node'));
$index->union($msrfi);
$tables =& $query->getTables();
$tables['i']['table'] = $index;
return $query;
}
的Drupal然后生成几乎正确的查询......
SELECT
i.type AS type, i.sid AS sid, SUM(CAST('10' AS DECIMAL) * COALESCE(((12.048628015788 * i.score * t.count)), 0)/CAST('10' AS DECIMAL)) AS calculated_score
FROM (
SELECT
s.sid AS sid, s.word AS word, s.score AS score, s.type AS type
FROM
search_index s
UNION SELECT
m.nid AS sid, m.keyword AS word, (
SELECT
MAX(score) + m.id/(SELECT MAX(id) FROM mos_search_result_forcer)
FROM
search_index
) AS score, 'node' AS type
FROM
mos_search_result_forcer m
) i
INNER JOIN node n ON n.nid = i.sid
INNER JOIN search_total t ON i.word = t.word
INNER JOIN search_dataset d ON i.sid = d.sid AND i.type = d.type
WHERE (n.status = '1')
AND((i.word = 'turtles'))
AND (i.type = 'node')
/* this is the problem line... */
AND((d.data LIKE '% turtles %' ESCAPE '\\'))
/* ...end problem line */
GROUP BY i.type, i.sid
HAVING (COUNT(*) >= '1')
ORDER BY calculated_score DESC
LIMIT 10 OFFSET 0
...我需要的是 “问题行” 到阅读...
AND((d.data LIKE '% turtles %' ESCAPE '\\') OR (d.sid IN (SELECT nid FROM mos_search_result_forcer)))
...我可以使用什么钩子来添加该OR条件?
- 我不想破解Drupal的核心。
- 我不想更改联合/子查询(不是我的决定)。
- 我会稍后优化查询 - 功能更重要。
谢谢,聪明的人!
谢谢!该代码实际上与我正在试验的代码非常相似;然而,问题在于,在调用hook_query_alter之后,我需要修改的条件被添加到查询对象*中。我希望在调用查询的execute方法之前可能会有另一个钩子被调用,或者在查询对象向其本身添加条件之前调用正确的钩子? – chaseisabelle 2014-09-15 14:05:55
这很不可能(实际上我认为不可能)在'hook_query_alter()'之后查询被改变......可能发生的另一个模块也实现了'hook_query_alter()',它的实现只是在你的后面运行。你可以通过实现['hook_module_implements_alter()'](https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_module_implements_alter/7)并移动你的模块的' hook_query_alter()'钩到列表的末尾 – Clive 2014-09-15 14:09:58
再次谢谢你!当var_dumping查询对象时,我能够看到条件。查询arg(传递给'hook_query_alter')的类型是'PagerDefault',它具有'SearchQuery'类型的受保护属性'$ query'。受保护的'SearchQuery'属性具有我需要修改的条件数组。有没有办法可以修改这样的嵌套查询条件? – chaseisabelle 2014-09-15 15:06:32