I have code like this:
function search_keyword(){
$keyword = $this->db->escape_like_str(trim($_POST['keyword']));
$sql = " ( SELECT name, id_global_info AS id, body AS body, tag AS tag ,info_type_id AS info_type, \"global_info\" AS mytable FROM global_info WHERE ";
$sql .= " MATCH (name, body, tag) AGAINST ( '$keyword') ";
$sql .= " ) UNION ALL ";
$sql .= " ( SELECT name, id_person AS id, surname AS body, info AS tag , location AS info_type, \"person\" AS mytable FROM person WHERE ";
$sql .= " MATCH (name, surname, info) AGAINST ( '$keyword' ) ";
$sql .= " ) UNION ALL ";
$sql .= "( SELECT name, id_event AS id, body AS body, caffe_id AS tag , date AS info_type, \"event\" AS mytable FROM event WHERE ";
$sql .= " MATCH (name, body) AGAINST ( '$keyword' ) ";
$sql .= " ) UNION ALL ";
$sql .= "( SELECT name, id_caffe AS id, description AS body, adress AS tag, location_id AS info_type, \"caffe\" AS mytable FROM caffe WHERE ";
$sql .= " MATCH (name, description) AGAINST ( '$keyword' ) ";
$sql .= " ) ";
$q = $this->db->query($sql);
return $q = $q->num_rows() == 0 ? FALSE : $q->result();
}
How can I count number of results per row and then order them by count result?
Example:
I want to search word car – first row have name car, and in the body word car is appearing 7 times, and in second row car is appearing 2 times in the body. I want to count how many time word car is found and then order results by that count result.
I think it’s best that you did it using PHP:
Since I do not know your database interface, this is not exact code but I believe you can get the general idea (I am assuming that your resultset object is an array of associative arrays).
Edit:
Alternate schema
Attributes explained
record_type : 1 -> global_info, 2 -> person , 3 -> event, 4 -> caffe
body : 1 -> body , 2 -> surname, 3 -> body , 4 -> description
tag : 1 -> tag , 2 -> info, 3 -> body , 4 -> description
You can then construct a single SQL using INNER JOINs to feed the records to Sphinx.