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

drupal query sorted by keywords occurrence in matching result

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

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

即。搜索 "banana apple orange" 到 return:

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

$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).

我发现一个问题 提到了

Take your where clause, replace all the or with +, and make sure each individual like the statement is wrapped in parenthesis, then order by it.

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

的示例

用 drupal 查询 approach/how 这样做是正确的吗?请帮忙:)

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

链接的答案似乎是执行此操作的正确方法。

可以像这样为 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']:请参阅 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();