Sphinx\SphinxClient::buildKeywords PHP Method

buildKeywords() public method

Extracts keywords from query using tokenizer settings for a given index
public buildKeywords ( string $query, string $index, boolean $hits ) : array | false
$query string query to extract keywords from
$index string name of the index to get tokenizing settings and keyword occurrence statistics from
$hits boolean whether keyword occurrence statistics are required
return array | false Array of hashes with per-keyword information, or false on failure.
    public function buildKeywords($query, $index, $hits)
    {
        if (!is_string($query)) {
            throw new \InvalidArgumentException('Query must be a string.');
        }
        if (!is_string($index)) {
            throw new \InvalidArgumentException('Index name must be a string.');
        }
        $hits = (bool) $hits;
        $this->mbPush();
        if (!($fp = $this->connect())) {
            $this->mbPop();
            return false;
        }
        // build request
        // v.1.0 req
        $req = pack('N', strlen($query)) . $query;
        $req .= pack('N', strlen($index)) . $index;
        $req .= pack('N', (int) $hits);
        // send query, get response
        $len = strlen($req);
        // add header
        $req = pack('nnN', self::SEARCHD_COMMAND_KEYWORDS, self::VER_COMMAND_KEYWORDS, $len) . $req;
        if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, self::VER_COMMAND_KEYWORDS))) {
            $this->mbPop();
            return false;
        }
        // parse response
        $pos = 0;
        $res = array();
        $rlen = strlen($response);
        list(, $nwords) = unpack('N*', substr($response, $pos, 4));
        $pos += 4;
        for ($i = 0; $i < $nwords; $i++) {
            list(, $len) = unpack('N*', substr($response, $pos, 4));
            $pos += 4;
            $tokenized = $len ? substr($response, $pos, $len) : '';
            $pos += $len;
            list(, $len) = unpack('N*', substr($response, $pos, 4));
            $pos += 4;
            $normalized = $len ? substr($response, $pos, $len) : '';
            $pos += $len;
            $res[] = array('tokenized' => $tokenized, 'normalized' => $normalized);
            if ($hits) {
                list($ndocs, $nhits) = array_values(unpack('N*N*', substr($response, $pos, 8)));
                $pos += 8;
                $res[$i]['docs'] = $ndocs;
                $res[$i]['hits'] = $nhits;
            }
            if ($pos > $rlen) {
                $this->error = 'incomplete reply';
                $this->mbPop();
                return false;
            }
        }
        $this->mbPop();
        return $res;
    }

Usage Example

Example #1
0
 public function testBuildKeywordsWithUnicode()
 {
     $sphinx = new SphinxClient();
     $keyword = $this->utf16to8('65e5672c8a9e');
     $results = $sphinx->buildKeywords($keyword, 'sphinxtest', 1);
     $this->assertEquals($results, array(array('tokenized' => $keyword, 'normalized' => $keyword, 'docs' => 1, 'hits' => 1)));
 }
All Usage Examples Of Sphinx\SphinxClient::buildKeywords