Bahrami-Reza
Bahrami-Reza

Reputation: 608

full text search in codeigniter

I have a query to search keywords using like, but I also want to search full text so I changed it to full text search query, but it doesn't work.

The old query that's working fine:

$data = $this->db
    ->select('content.title,content.id,content.category,content.body,path')
    ->from('content')   
    ->join('categories','content.category = categories.id')             
    ->like('content.title', $searchterm)
    ->like('content.body', $searchterm)
    ->order_by("content.id","DESC")
    ->get();

and this is my new query for full text search:

$data = $this->db
    ->select('content.title,content.id,content.category,content.body,path')
    ->from('content')   
    ->join('categories','content.category = categories.id')                 
    ->where('MATCH (content.body, content.title) AGAINST ("'. $searchterm .'")')
    ->order_by("content.id","DESC")
    ->get();

Upvotes: 1

Views: 7684

Answers (4)

codegames
codegames

Reputation: 1921

if you have multiple column you can use them in single line like this:

->where("MATCH(title, model) AGAINST('$text')", null, false);

or just one column:

->where("MATCH(title) AGAINST('$text')", null, false);

don't forget to escape your inputs. because we disabled escaping with that false over there. use this for escaping:

$text = $this->db->escape_str($text);

Upvotes: 0

deviloper
deviloper

Reputation: 7240

  1. if you are using mysql version 5.5 or lower, make sure all the tables involved have the engine MyISAM.
  2. make sure the your column has the FULLTEXT INDEX.
  3. where() takes 3 arguments, example:

    $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
    $this->db->get('table');  
    
  4. more than one columns in match() triggers Error Number: 1191, so separate them:

    ->where('MATCH (content.title) AGAINST ("'. $searchterm .'")')
    ->where('MATCH (content.body) AGAINST ("'. $searchterm .'")')
    

Upvotes: 5

ali panahi
ali panahi

Reputation: 78

you did not use the correct syntax of mathch .... against try this:

    ->where MATCH (content.body, content.title) AGAINST ("'. $searchterm .'") > 0

Upvotes: 0

Nil'z
Nil'z

Reputation: 7475

Try by changing the where clause in your query, see if it helps:

->where('MATCH (content.body, content.title) AGAINST ("'. $searchterm .'")', NULL, false)

This sets the value to NULL and tells CI not to escape the string.

Upvotes: 1

Related Questions