📄 service.php
字号:
* Set the string used to separate the parameters in thequery string * Defaulted to '&' * * @param string $queryStringDelimiter */ public function setQueryStringDelimiter($queryStringDelimiter) { $this->_queryStringDelimiter = $queryStringDelimiter; } /** * Call the /admin/ping servlet, can be used to quickly tell if a connection to the * server is able to be made. * * @param float $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 5) * @return float Actual time taken to ping the server, FALSE if timeout occurs */ public function ping($timeout = 5) { $timeout = (float) $timeout; if ($timeout <= 0) { $timeout = -1; } $start = microtime(true); //try to connect to the host with timeout $fp = fsockopen($this->_host, $this->_port, $errno, $errstr, $timeout); if ($fp) { //If we have a timeout set, then determine the amount of time we have left //in the request and set the stream timeout for the write operation if ($timeout > 0) { //do the calculation $writeTimeout = $timeout - (microtime(true) - $start); //check if we're out of time if ($writeTimeout <= 0) { return false; } //convert to microseconds and set the stream timeout $writeTimeoutInMicroseconds = (int) $writeTimeout * 1000000; stream_set_timeout($fp, 0, $writeTimeoutInMicroseconds); } $request = 'HEAD ' . $this->_path . self::PING_SERVLET . ' HTTP/1.1' . "\r\n" . 'host: ' . $this->_host . "\r\n" . 'Connection: close' . "\r\n" . "\r\n"; fwrite($fp, $request); //check the stream meta data to see if we timed out during the operation $metaData = stream_get_meta_data($fp); if ($metaData['timeout']) { fclose($fp); return false; } //if we have a timeout set and have made it this far, determine the amount of time //still remaining and set the timeout appropriately before the read operation if ($timeout > 0) { //do the calculation $readTimeout = $timeout - (microtime(true) - $start); //check if we've run out of time if ($readTimeout <= 0) { return false; } //convert to microseconds and set the stream timeout $readTimeoutInMicroseconds = $readTimeout * 1000000; stream_set_timeout($fp, 0, $readTimeoutInMicroseconds); } $response = fread($fp, 15); //check the stream meta data to see if we timed out during the operation $metaData = stream_get_meta_data($fp); if ($metaData['timeout']) { fclose($fp); return false; } //we made it, return the approximate ping time return microtime(true) - $start; } return false; } /** * Call the /admin/threads servlet and retrieve information about all threads in the * Solr servlet's thread group. Useful for diagnostics. * * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function threads() { return $this->_sendRawGet($this->_threadsUrl); } /** * Raw Add Method. Takes a raw post body and sends it to the update service. Post body * should be a complete and well formed "add" xml document. * * @param string $rawPost * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function add($rawPost) { return $this->_sendRawPost($this->_updateUrl, $rawPost); } /** * Add a Solr Document to the index * * @param Apache_Solr_Document $document * @param boolean $allowDups * @param boolean $overwritePending * @param boolean $overwriteCommitted * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function addDocument(Apache_Solr_Document $document, $allowDups = false, $overwritePending = true, $overwriteCommitted = true) { $dupValue = $allowDups ? 'true' : 'false'; $pendingValue = $overwritePending ? 'true' : 'false'; $committedValue = $overwriteCommitted ? 'true' : 'false'; $rawPost = '<add allowDups="' . $dupValue . '" overwritePending="' . $pendingValue . '" overwriteCommitted="' . $committedValue . '">'; $rawPost .= $this->_documentToXmlFragment($document); $rawPost .= '</add>'; return $this->add($rawPost); } /** * Add an array of Solr Documents to the index all at once * * @param array $documents Should be an array of Apache_Solr_Document instances * @param boolean $allowDups * @param boolean $overwritePending * @param boolean $overwriteCommitted * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function addDocuments($documents, $allowDups = false, $overwritePending = true, $overwriteCommitted = true) { $dupValue = $allowDups ? 'true' : 'false'; $pendingValue = $overwritePending ? 'true' : 'false'; $committedValue = $overwriteCommitted ? 'true' : 'false'; $rawPost = '<add allowDups="' . $dupValue . '" overwritePending="' . $pendingValue . '" overwriteCommitted="' . $committedValue . '">'; foreach ($documents as $document) { if ($document instanceof Apache_Solr_Document) { $rawPost .= $this->_documentToXmlFragment($document); } } $rawPost .= '</add>'; return $this->add($rawPost); } /** * Create an XML fragment appropriate for use inside a Solr add call * * @return string */ private function _documentToXmlFragment(Apache_Solr_Document $document) { $xml = '<doc>'; foreach ($document as $key => $value) { $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); if (is_array($value)) { foreach ($value as $multivalue) { $multivalue = htmlspecialchars($multivalue, ENT_NOQUOTES, 'UTF-8'); $xml .= '<field name="' . $key . '">' . $multivalue . '</field>'; } } else { $value = htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8'); $xml .= '<field name="' . $key . '">' . $value . '</field>'; } } $xml .= '</doc>'; return $xml; } /** * Send a commit command. Will be synchronous unless both wait parameters are set * to false. * * @param boolean $waitFlush * @param boolean $waitSearcher * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function commit($waitFlush = true, $waitSearcher = true) { $flushValue = $waitFlush ? 'true' : 'false'; $searcherValue = $waitSearcher ? 'true' : 'false'; $rawPost = '<commit waitFlush="' . $flushValue . '" waitSearcher="' . $searcherValue . '" />'; return $this->_sendRawPost($this->_updateUrl, $rawPost); } /** * Raw Delete Method. Takes a raw post body and sends it to the update service. Body should be * a complete and well formed "delete" xml document * * @param string $rawPost * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function delete($rawPost) { return $this->_sendRawPost($this->_updateUrl, $rawPost); } /** * Create a delete document based on document ID * * @param string $id * @param boolean $fromPending * @param boolean $fromCommitted * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function deleteById($id, $fromPending = true, $fromCommitted = true) { $pendingValue = $fromPending ? 'true' : 'false'; $committedValue = $fromCommitted ? 'true' : 'false'; //escape special xml characters $id = htmlspecialchars($id, ENT_NOQUOTES, 'UTF-8'); $rawPost = '<delete fromPending="' . $pendingValue . '" fromCommitted="' . $committedValue . '"><id>' . $id . '</id></delete>'; return $this->delete($rawPost); } /** * Create a delete document based on a query and submit it * * @param string $rawQuery * @param boolean $fromPending * @param boolean $fromCommitted * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function deleteByQuery($rawQuery, $fromPending = true, $fromCommitted = true) { $pendingValue = $fromPending ? 'true' : 'false'; $committedValue = $fromCommitted ? 'true' : 'false'; //escape special xml characters $rawQuery = htmlspecialchars($rawQuery, ENT_NOQUOTES, 'UTF-8'); $rawPost = '<delete fromPending="' . $pendingValue . '" fromCommitted="' . $committedValue . '"><query>' . $rawQuery . '</query></delete>'; return $this->delete($rawPost); } /** * Send an optimize command. Will be synchronous unless both wait parameters are set * to false. * * @param boolean $waitFlush * @param boolean $waitSearcher * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function optimize($waitFlush = true, $waitSearcher = true) { $flushValue = $waitFlush ? 'true' : 'false'; $searcherValue = $waitSearcher ? 'true' : 'false'; $rawPost = '<optimize waitFlush="' . $flushValue . '" waitSearcher="' . $searcherValue . '" />'; return $this->_sendRawPost($this->_updateUrl, $rawPost); } /** * Simple Search interface * * @param string $query The raw query string * @param int $offset The starting offset for result documents * @param int $limit The maximum number of result documents to return * @param array $params key / value pairs for query parameters, use arrays for multivalued parameters * @return Apache_Solr_Response * * @throws Exception If an error occurs during the service call */ public function search($query, $offset = 0, $limit = 10, $params = array()) { if (!is_array($params)) { $params = array(); } //construct our full parameters //sending the version is important in case the format changes $params['version'] = self::SOLR_VERSION; //common parameters in this interface $params['wt'] = self::SOLR_WRITER; $params['q'] = $query; $params['start'] = $offset; $params['rows'] = $limit; //escape all parameters appropriately for inclusion in the GET parameters $escapedParams = array(); do { //because some parameters can be included multiple times, loop through all //params and include their value or their first array value. unset values as //they are fully added so that the params list can be iteratively added. // //NOTE: could be done all at once, but this way makes the query string more //readable at little performance cost foreach ($params as $key => &$value) { if (is_array($value)) { //parameter has multiple values that need passed //array_shift pops off the first value in the array and also removes it $escapedParams[] = urlencode($key) . '=' . urlencode(array_shift($value)); if (empty($value)) { unset($params[$key]); } } else { //simple, single value case $escapedParams[] = urlencode($key) . '=' . urlencode($value); unset($params[$key]); } } } while (!empty($params)); return $this->_sendRawGet($this->_searchUrl . $this->_queryDelimiter . implode($this->_queryStringDelimiter, $escapedParams)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -