简体   繁体   中英

php mysql full text search multiple table joined by id

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

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM