php mysql full text search multiple table joined by id

自闭症网瘾萝莉.ら 提交于 2019-12-11 09:49:06

问题


I have been reading on full text search for some time now, I have read some articles that say its possible but they don't provide sample query,

I'm stuck finding a workaround on how to search on multiple table joined by an ID using full text search

this is my minified table look like

here is what i have so far

    $users = User::select('users.*','departments.department_name')
    ->join('departments', 'departments.id', ' =', 'users.dept_id')
    ->whereRaw(
        "MATCH(user_name) AGAINST(? IN BOOLEAN MODE)", 
        array($q)
        )
    ->paginate(10);
}

how can I include department_name when searching? please help, thanks in advance


回答1:


You could create a 'materialized view'. http://en.wikipedia.org/wiki/Materialized_view

basically a table that is the results of the JOIN, and the create a full text index on that.

CREATE TABLE materialized (FULLTEXT idx (user_name,department_name)) 
 SELECT u.id,user_name,department_name 
 FROM users u INNER JOIN departments d ON (d.id = dept_id) 

You can then run queries on that table instead..

SELECT * FROM materialized WHERE MATCH(user_name,department_name) AGAINST('test' IN BOOLEAN MODE)

but...

You will need to updatethe table periodically (or when the underlying tables update) - easiest is just to DROP and recreate - Or you can use TRUNCATE then INSERT INTO ... SELECT ... FROM ... format.

(more elaborate schemes involve triggers to keep the 'view' updated, or even watching the binlog, and replaying updates against the 'view')




回答2:


I think you are too close to your result. Using query-builder (but not tested) this code should work:

$user = DB::table('users')
->selectRaw('users.*, departments.department_name')
->join('departments', 'departments.id', ' =', 'users.dept_id')
->whereRaw(
    "MATCH(users.user_name) AGAINST(? IN BOOLEAN MODE) OR
     MATCH(departments.department_name) AGAINST(? IN BOOLEAN MODE)",
    array($q, $q))
)->paginate(10);

There are also some restrictions on where you could use FULLTEXT in MySQL tables and where not http://dev.mysql.com/doc/refman/5.6/en/fulltext-restrictions.html. It is good practise to have FULLTEXT index on column or column list which you use in MATCH() clause.



来源:https://stackoverflow.com/questions/28448152/php-mysql-full-text-search-multiple-table-joined-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!