SphinxClient::BuildKeywords PHP Method

BuildKeywords() public method

an array of words on success
public BuildKeywords ( $query, $index, $hits )
    function BuildKeywords($query, $index, $hits)
    {
        assert(is_string($query));
        assert(is_string($index));
        assert(is_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 query
        $req .= pack("N", strlen($index)) . $index;
        // req index
        $req .= pack("N", (int) $hits);
        ////////////////////////////
        // send query, get response
        ////////////////////////////
        $len = strlen($req);
        $req = pack("nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len) . $req;
        // add header
        if (!$this->_Send($fp, $req, $len + 8) || !($response = $this->_GetResponse($fp, 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

Esempio n. 1
0
 public function createKeywords($query, $index, $hits = false)
 {
     return $this->sphinxClient->BuildKeywords($query, $index, $hits);
 }
All Usage Examples Of SphinxClient::BuildKeywords