drupal 查询按匹配结果中关键字出现次数排序

问题描述 投票:0回答:1

在 @Fky 尤其是 @Syscall 的大力帮助下,我设法将 drupal 查询更改为在 3 个表而不是两个表内搜索,并添加条件以及关键字拆分和删除空格。

drupal sql 多个表的条件子句?

我希望结果 1) 按匹配结果的关键字出现次数排序,然后 2) 按 ASC 中的标题排序。

即。搜索“香蕉苹果橙”返回:

  • 香蕉橙
  • 苹果
  • 香蕉
  • 橙色

我设法按标题排序,但不知道如何先按关键字出现次数排序?

$term = strip_tags(drupal_substr($_POST['keyword'], 0, 100));
$terms = explode(' ', $term); // split using ' '
$terms = array_map('trim', $terms); // remove unwanted spaces
$termsfiltered = array_filter($terms);
 $or = db_or();
  foreach ($termsfiltered as $term) {
    $or->condition('fd.field_detailed_question_value', '%'.db_like($term).'%', 'LIKE');
    $or->condition('fb.body_value','%'.db_like($term).'%' , 'LIKE');
    $or->condition('n.title','%'.db_like($term).'%' , 'LIKE');
    }
 $query = db_select('node', 'n');

$query->fields('n');
$query->range(0,20); //LIMIT to 15 records
$query->orderBy('title', 'ASC'); //ORDER BY title
$query->leftJoin('field_data_body' , 'fb', 'fb.entity_id=n.nid');
$query->leftJoin('field_data_field_detailed_question' ,'fd', 'fd.entity_id=n.nid');
$query->condition($or);
$query->condition('n.status','1');
$stmt = $query->execute(); // execute the query (returns the "statement" to fetch).

我发现了一个问题Sort sql result by Outences of a set of keywords提到

将 where 子句中的所有 or 替换为 +,并确保每个 个人喜欢的陈述用括号括起来,然后按它排序。

我还看到了使用 $query->addExpression...

的示例

这是正确的方法/如何使用 drupal 查询来做到这一点?请帮忙:)

更新:或者以某种方式将 orderby 与 COUNT 结合起来?

php sql mysql drupal drupal-7
1个回答
1
投票
链接的答案似乎是执行此操作的正确方法。

为 Drupal 查询创建

ORDER BY

 语句可以像这样完成:

$term = strip_tags(drupal_substr($_POST['keyword'], 0, 100)); $terms = explode(' ', $term); // split using ' ' $terms = array_map('trim', $terms); // remove unwanted spaces $termsfiltered = array_filter($terms); $order_array = [] ; // Create an empty array to create the string $or = db_or() ; foreach ($termsfiltered as $term) { // Or condition $or->condition('fd.field_detailed_question_value', '%'.db_like($term).'%', 'LIKE'); $or->condition('fb.body_value','%'.db_like($term).'%' , 'LIKE'); $or->condition('n.title','%'.db_like($term).'%' , 'LIKE'); // Order by array (add the concat of each fields) $order_array[] = '(concat(title, fd.field_detailed_question_value, fb.body_value) like \'%'.db_like($term).'%\')'; } $query = db_select('node', 'n'); $query->fields('n'); $query->range(0,20); $query->orderBy(implode('+',$order_array), 'desc'); // Dynamic order by $query->leftJoin('field_data_body' , 'fb', 'fb.entity_id=n.nid'); $query->leftJoin('field_data_field_detailed_question' ,'fd', 'fd.entity_id=n.nid'); $query->condition($or); $query->condition('n.status','1'); $stmt = $query->execute(); // execute the query (returns the "statement" to fetch).

添加的内容是:

循环之前
  • $order_array = [] ;
    
    
  • $order_array[] = '(concat...)
    在循环内
  • $query->orderBy(implode('+',$order_array), 'desc');
    改变了
请注意,您可以通过使用(或类似的东西)来防止错误:

if (empty($termsfiltered)) { return "No matches."; }


另一件事。在 Drupal 中使用

$_POST

 并不是一个好的做法。如果您来自表单,则应使用 
$form_state['values']['keyword']
 而不是 
$_POST['keyword']
:请参阅 
此示例


编辑

$term = strip_tags(drupal_substr($_POST['keyword'], 0, 100)); $terms = explode(' ', $term); // split using ' ' $terms = array_map('trim', $terms); // remove unwanted spaces $termsfiltered = array_filter($terms); $order_array = ['title'=>[],'question'=>[],'body'=>[]] ; // Create an empty array to create the string $or = db_or() ; foreach ($termsfiltered as $term) { // Or condition $or->condition('fd.field_detailed_question_value', '%'.db_like($term).'%', 'LIKE'); $or->condition('fb.body_value','%'.db_like($term).'%' , 'LIKE'); $or->condition('n.title','%'.db_like($term).'%' , 'LIKE'); // Order by array (add the concat of each fields) $order_array['title'][] = '(title like \'%'.db_like($term).'%\')'; $order_array['question'][] = '(fd.field_detailed_question_value like \'%'.db_like($term).'%\')'; $order_array['body'][] = '(fb.body_value like \'%'.db_like($term).'%\')'; } $query = db_select('node', 'n'); $query->fields('n'); $query->range(0,20); $query->orderBy(implode('+',$order_array['title']), 'desc'); // Dynamic order by $query->orderBy(implode('+',$order_array['question']), 'desc'); // Dynamic order by $query->orderBy(implode('+',$order_array['body']), 'desc'); // Dynamic order by $query->leftJoin('field_data_body' , 'fb', 'fb.entity_id=n.nid'); $query->leftJoin('field_data_field_detailed_question' ,'fd', 'fd.entity_id=n.nid'); $query->condition($or); $query->condition('n.status','1'); $stmt = $query->execute();
    
© www.soinside.com 2019 - 2024. All rights reserved.