Symfony 4 Match Against按相关性排序

时间:2018-12-08 17:01:00

标签: symfony match-against

我想通过启示来排序,这意味着如果我在数据库中:

  • FooBa
  • FooBar
  • FooB
  • Foo

我想输入 foo ,它将被排序:

  • Foo
  • FooB
  • FooBa
  • FooBar
public function findBySearchQueryPagin2(string $rawQuery, int $limit = 999, string $sort = "DESC", string $sortBy = "relevance", int $page = 1): pagerfanta
{
    $query = $this->sanitizeSearchQuery($rawQuery);
    $sort = $this->sanitizeSearchQuery($sort);
    $sortBy = $this->sanitizeSearchQuery($sortBy);
    $limit = $this->sanitizeSearchQuery($limit);
    $page = $this->sanitizeSearchQuery($page);
    if(($sort != 'ASC')&&($sort != 'DESC')) $sort = 'DESC';
    if(($sortBy != 'date')&&($sortBy != 'name')&&($sortBy != 'relevance')){
        $sortBy = null;
    } else {
        if($sortBy=="name") $sortBy = "primarytitle";
        if($sortBy=="date") $sortBy = "startyear";
        if($sortBy=="relevance") $sortBy = "relevance";
    }
    $queryBuilder = $this->createQueryBuilder('p')
        ->andWhere("MATCH_AGAINST (p.primarytitle, :searchTerms) > 1")
        ->setParameter('searchTerms', $query)
        ->setMaxResults($limit);
    var_dump($query);
       if($sortBy !== "relevance"){
           $queryBuilder
               ->orderBy('p.' . $sortBy, $sort);
       }
    return $this->createPaginator($queryBuilder->getQuery(), $page);
}

0 个答案:

没有答案