src/Fts.php
public function search($table, $search, $limit = '', $where = '', array $fields = array(), array $weights = array())
    {
        if (is_null($this->rank)) {
            $this->rank = $this->db->connection()->createFunction('rank', array(&$this, 'rank'), 2);
        }
        if (!empty($where)) {
            $where = (stripos($where, 'WHERE') === false) ? "WHERE {$where} AND" : "{$where} AND";
        } else {
            $where = 'WHERE';
        }
        $fields = (!empty($fields)) ? implode(', ', $fields).',' : '';
        $weights = "'".implode(',', $weights)."'"; // we pass this along to our rank function
        #-- Join, Order, Values --#
        $join = '';
        $order = 'rank';
        $values = array($search);
        if (!empty($limit)) {
            if (is_numeric($limit)) {
                $offset = 0;
                $length = $limit;
            } else {
                $limit = explode(',', preg_replace('/[^0-9,]/', '', $limit));
                $offset = (isset($limit[0])) ? (int) $limit[0] : 0;
                $length = (isset($limit[1])) ? (int) $limit[1] : 10;
            }
            $join = implode("\n", array(
                'JOIN (',
                "  SELECT s.docid, rank(matchinfo(s.{$table}), {$weights}) AS rank",
                "  FROM {$table} AS s {$where} s.{$table} MATCH ?",
                "  ORDER BY rank DESC LIMIT {$length} OFFSET {$offset}",
                ') AS ranktable USING (docid)',
            ));
            $order = 'ranktable.rank';
            $values[] = $search; // add one more to the MATCH
        }
        #-- Query --#
        $results = array();
        if ($stmt = $this->db->query(array(
            "SELECT s.docid, {$fields}",
            "  snippet(s.{$table}, '<b>', '</b>', '<b>...</b>', -1, 50) AS snippet,",
            "  offsets(s.{$table}) AS offsets,",
            "  rank(matchinfo(s.{$table}), {$weights}) AS rank",
            "FROM {$table} AS s {$join} {$where} s.{$table} MATCH ?",
            "ORDER BY {$order} DESC",
        ), $values, 'assoc')) {
            while ($row = $this->db->fetch($stmt)) {
                $results[] = $row;
            }
            $this->db->close($stmt);
        }

        return $results;
    }
public function search($table, $search, $limit = '', $where = '', array $fields = array(), array $weights = array())
    {
        if (is_null($this->rank)) {
            $this->rank = $this->db->connection()->createFunction('rank', array(&$this, 'rank'), 2);
        }
        if (!empty($where)) {
            $where = (stripos($where, 'WHERE') === false) ? "WHERE {$where} AND" : "{$where} AND";
        } else {
            $where = 'WHERE';
        }
        $fields = (!empty($fields)) ? implode(', ', $fields).',' : '';
        $weights = "'".implode(',', $weights)."'"; // we pass this along to our rank function
        #-- Join, Order, Values --#
        $join = '';
        $order = 'rank';
        $values = array($search);
        if (!empty($limit)) {
            if (is_numeric($limit)) {
                $offset = 0;
                $length = $limit;
            } else {
                $limit = explode(',', preg_replace('/[^0-9,]/', '', $limit));
                $offset = (isset($limit[0])) ? (int) $limit[0] : 0;
                $length = (isset($limit[1])) ? (int) $limit[1] : 10;
            }
            $join = implode("\n", array(
                'JOIN (',
                "  SELECT s.docid, rank(matchinfo(s.{$table}), {$weights}) AS rank",
                "  FROM {$table} AS s {$where} s.{$table} MATCH ?",
                "  ORDER BY rank DESC LIMIT {$length} OFFSET {$offset}",
                ') AS ranktable USING (docid)',
            ));
            $order = 'ranktable.rank';
            $values[] = $search; // add one more to the MATCH
        }
        #-- Query --#
        $results = array();
        if ($stmt = $this->db->query(array(
            "SELECT s.docid, {$fields}",
            "  snippet(s.{$table}, '<b>', '</b>', '<b>...</b>', -1, 50) AS snippet,",
            "  offsets(s.{$table}) AS offsets,",
            "  rank(matchinfo(s.{$table}), {$weights}) AS rank",
            "FROM {$table} AS s {$join} {$where} s.{$table} MATCH ?",
            "ORDER BY {$order} DESC",
        ), $values, 'assoc')) {
            while ($row = $this->db->fetch($stmt)) {
                $results[] = $row;
            }
            $this->db->close($stmt);
        }

        return $results;
    }
Queries an FTS **$table** for the relevant **$search** word(s) found within.

@param string $table   The database table name.
@param string $search  The search term(s) to '**MATCH**'.
@param mixed  $limit   If you are not paginating results and only want the top whatever, then this is an integer. Otherwise it is an SQL '<b> LIMIT offset, length</b>' clause.
@param string $where   An additional string of restrictions you would like to place. If you don't include '**WHERE**' we will add it for you. If you are combining tables to deliver results then put your '**INNER JOIN ... WHERE**' clause here, and prefix the search **$table** and fields with '**s.**' eg. ``INNER JOIN my_table AS my ON s.docid = my.id WHERE my.field = ...``
@param array  $fields  An ``array('s.field', ...)`` of additional fields you would like to include in the search results. Remember to specify the table prefixes if needed.
@param array  $weights An array of importance that you would like to place on the **$table** fields searched in whatever order you placed them originally. The default weights are 1 for each field, meaning they are all of equal importance. If you want to make one field more relevant than another, then make this an ``array($weight, ...)`` of importance to place on each corresponding **$table** field. Even if you place an importance of 0 on a field it will still be included among the search results, it will just have a lower rank (possibly 0). All of this assumes you have more than one field in your **$table**, otherwise this will make no difference whatsoever.

@return array An associative array of results.

@example

```php
var_export($db->fts->search('results', 'fish'));
array(
array(
'docid' => 101,
'snippet' => "If wishes were <b>fishes</b>, we'd have a <b>fish</b> fry.",
'offsets' => '0 0 15 6 0 0 35 4',
'rank' => 1.333,
),
array(
'docid' => 102,
'snippet' => 'Women want me, <b>fish</b> fear me.',
'offsets' => '0 0 15 4',
'rank' => .666,
),
);
```