drupal查询按匹配结果中的关键字排序排序

时间:2018-04-06 11:21:23

标签: mysql sql drupal drupal-7

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

drupal sql conditional clauses for multiple tables?

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

IE中。寻找“香蕉苹果橙”返回:

  • 香蕉橙
  • 苹果
  • 香蕉

我设法按标题排序,但无法弄清楚如何按关键字出现次数排序

$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 occurence of a set of keywords
  

带上你的where子句,将所有或替换为+,并确保每个   像声明一样的个体用括号括起来,然后按它排序。

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

的示例

这是正确的方法/如何使用drupal查询吗?请帮助:)

更新:或以某种方式将orderby与COUNT 组合在一起?

1 个答案:

答案 0 :(得分: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."; }

另一件事。使用$_POST在Drupal中不是一个好习惯。如果您来自表单,则应使用$form_state['values']['keyword']代替$_POST['keyword']:请参阅this example

修改

$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();