Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest Service Implemented #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion Apache/Solr/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class Apache_Solr_Service
const SYSTEM_SERVLET = 'admin/system';
const THREADS_SERVLET = 'admin/threads';
const EXTRACT_SERVLET = 'update/extract';
const SUGGEST_SERVLET = 'suggest';

/**
* Server identification strings
Expand Down Expand Up @@ -164,7 +165,7 @@ class Apache_Solr_Service
*
* @var string
*/
protected $_pingUrl, $_updateUrl, $_searchUrl, $_systemUrl, $_threadsUrl;
protected $_pingUrl, $_updateUrl, $_searchUrl, $_systemUrl, $_threadsUrl, $_suggestUrl;

/**
* Keep track of whether our URLs have been constructed
Expand Down Expand Up @@ -290,6 +291,7 @@ protected function _initUrls()
$this->_systemUrl = $this->_constructUrl(self::SYSTEM_SERVLET, array('wt' => self::SOLR_WRITER));
$this->_threadsUrl = $this->_constructUrl(self::THREADS_SERVLET, array('wt' => self::SOLR_WRITER ));
$this->_updateUrl = $this->_constructUrl(self::UPDATE_SERVLET, array('wt' => self::SOLR_WRITER ));
$this->_suggestUrl = $this->_constructUrl(self::SUGGEST_SERVLET);

$this->_urlsInited = true;
}
Expand Down Expand Up @@ -1242,4 +1244,53 @@ public function search($query, $offset = 0, $limit = 10, $params = array(), $met
throw new Apache_Solr_InvalidArgumentException("Unsupported method '$method', please use the Apache_Solr_Service::METHOD_* constants");
}
}

/**
* Simple Suggest interface
*
* @param string $query The raw query string
* @param array $params key / value pairs for other query parameters (see Solr documentation), use arrays for parameter keys used more than once (e.g. facet.field)
* @param string $method The HTTP method (Apache_Solr_Service::METHOD_GET or Apache_Solr_Service::METHOD::POST)
* @return Apache_Solr_Response
*
* @throws Apache_Solr_HttpTransportException If an error occurs during the service call
* @throws Apache_Solr_InvalidArgumentException If an invalid HTTP method is used
*/
public function suggest($query, $params = array(), $method = self::METHOD_GET)
{
// ensure params is an array
if (!is_null($params))
{
if (!is_array($params))
{
// params was specified but was not an array - invalid
throw new Apache_Solr_InvalidArgumentException("\$params must be a valid array or null");
}
}
else
{
$params = array();
}

// construct our full parameters

// common parameters in this interface
$params['wt'] = self::SOLR_WRITER;
$params['q'] = $query;

$queryString = $this->_generateQueryString($params);

if ($method == self::METHOD_GET)
{
return $this->_sendRawGet($this->_suggestUrl . $this->_queryDelimiter . $queryString);
}
else if ($method == self::METHOD_POST)
{
return $this->_sendRawPost($this->_suggestUrl, $queryString, FALSE, 'application/x-www-form-urlencoded; charset=UTF-8');
}
else
{
throw new Apache_Solr_InvalidArgumentException("Unsupported method '$method', please use the Apache_Solr_Service::METHOD_* constants");
}
}
}