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;
}