Raw relevance query in Laravel. How to handle it?(Laravel 中的原始相关性查询.如何处理?)
问题描述
我目前正在将一个大项目转移到 Laravel 框架 http://hobbyathletes.com/events/south-america/all-event-types/all/.我需要迁移我的内部搜索引擎,它使用以下查询(如果搜索一个词,这里有额外的预选选项南美洲):
I'm currently transfering a big project to Laravel framework http://hobbyathletes.com/events/south-america/all-event-types/all/. I need to migrate my internal searchengine, which uses the following query (if searched for one word, here additional pre-selected option South America ):
SELECT *, (
(CASE WHEN name LIKE '%searchthis%' THEN 1000 ELSE 0 END) +
(CASE WHEN name LIKE 'searchthis %' THEN 10 ELSE 0 END) +
(CASE WHEN name LIKE 'searchthis%' THEN 1 ELSE 0 END) +
(CASE WHEN name LIKE '% searchthis %' THEN 5 ELSE 0 END) +
(CASE WHEN city LIKE '%searchthis%' THEN 500 ELSE 0 END) +
(CASE WHEN city LIKE 'searchthis%' THEN 10 ELSE 0 END) +
(CASE WHEN city LIKE 'searchthis %' THEN 1 ELSE 0 END) +
(CASE WHEN city LIKE '% searchthis %' THEN 5 ELSE 0 END) +
(CASE WHEN country LIKE '%searchthis%' THEN 500 ELSE 0 END) +
(CASE WHEN country LIKE 'searchthis%' THEN 10 ELSE 0 END) +
(CASE WHEN country LIKE 'searchthis %' THEN 1 ELSE 0 END) +
(CASE WHEN country LIKE '% searchthis %' THEN 5 ELSE 0 END) +
(CASE WHEN homepage LIKE '%searchthis%' THEN 600 ELSE 0 END) +
(CASE WHEN headline LIKE '%searchthis%' THEN 100 ELSE 0 END) +
(CASE WHEN headline LIKE 'searchthis%' THEN 10 ELSE 0 END) +
(CASE WHEN headline LIKE 'searchthis %' THEN 1 ELSE 0 END) +
(CASE WHEN headline LIKE '% searchthis %' THEN 5 ELSE 0 END) +
(CASE WHEN description LIKE '%searchthis%' THEN 100 ELSE 0 END) +
(CASE WHEN description LIKE 'searchthis%' THEN 10 ELSE 0 END) +
(CASE WHEN description LIKE 'searchthis %' THEN 1 ELSE 0 END) +
(CASE WHEN description LIKE '% searchthis %' THEN 5 ELSE 0 END) +
(CASE WHEN distances LIKE '%searchthis%' THEN 100 ELSE 0 END) +
(CASE WHEN distances LIKE 'searchthis%' THEN 10 ELSE 0 END) +
(CASE WHEN distances LIKE 'searchthis %' THEN 1 ELSE 0 END) +
(CASE WHEN distances LIKE '% searchthis %' THEN 5 ELSE 0 END) +
(CASE WHEN date LIKE '%2014%' THEN 300 ELSE 0 END) +
(CASE WHEN name LIKE '%2014%' THEN 200 ELSE 0 END) +
(CASE WHEN date LIKE '%2013%' THEN 200 ELSE 0 END) +
(CASE WHEN name LIKE '%2013%' THEN 100 ELSE 0 END)
)
AS relevance
FROM events
WHERE
(
country = 'Argentina' or
country = 'Bolivia' or
country = 'Brazil' or
country = 'Chile' or
country = 'Colombia' or
country = 'Ecuador' or
country = 'Guyana' or
country = 'Paraguay' or
country = 'Peru' or
country = 'Suriname' or
country = 'Uganda' or
country = 'Uruguay' or
country = 'Venezuela'
)
AND
(
name LIKE '%searchthis%' OR
country LIKE '%searchthis%' OR
city LIKE '%searchthis%' OR
homepage LIKE '%searchthis%' OR
headline LIKE '%searchthis%' OR
description LIKE '%searchthis%' OR
distances LIKE '%searchthis%'
)
ORDER BY relevance DESC
如何将此查询转移到 Eloquent Query builder?我已经尝试过 DB:raw($query)
这似乎不起作用......尝试使用的是非常需要的 ->paginate(25)
和 ->get()
,都不起作用.这里的任何人都可以让我朝着正确的方向前进吗?
How can I transfer this query to Eloquent Query builder? I tried already DB:raw($query)
which seems not to work... Tried is with the much needed ->paginate(25)
and with ->get()
, all not working. Anyone here can put me in the right direction?
推荐答案
Try with DB::select()
Try with DB::select()
DB::select(DB::raw("
SELECT * FROM bla bla bla
"));
这篇关于Laravel 中的原始相关性查询.如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 中的原始相关性查询.如何处理?
基础教程推荐
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01