Commit 9e02303e authored by Bas de Nooijer's avatar Bas de Nooijer

Merge branch 'feature/suggester' into develop

parents c48ca8c2 84332b28
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a suggester query instance
$query = $client->createSuggester();
$query->setQuery('ap ip v'); //multiple terms
$query->setDictionary('suggest');
$query->setOnlyMorePopular(true);
$query->setCount(10);
$query->setCollate(true);
// this executes the query and returns the result
$resultset = $client->suggester($query);
echo '<b>Query:</b> '.$query->getQuery().'<hr/>';
// display results for each term
foreach ($resultset as $term => $termResult) {
echo '<h3>' . $term . '</h3>';
echo 'NumFound: '.$termResult->getNumFound().'<br/>';
echo 'StartOffset: '.$termResult->getStartOffset().'<br/>';
echo 'EndOffset: '.$termResult->getEndOffset().'<br/>';
echo 'Suggestions:<br/>';
foreach($termResult as $result){
echo '- '.$result.'<br/>';
}
echo '<hr/>';
}
// display collation
echo 'Collation: '.$resultset->getCollation();
htmlFooter();
\ No newline at end of file
......@@ -83,6 +83,8 @@
</ul>
<li><a href="2.5-terms-query.php">2.5 Terms query</a></li>
<li><a href="2.6-suggester-query.php">2.6 Suggester query</a></li>
</ul>
<li>4. Usage modes</li>
......
......@@ -92,6 +92,11 @@ class Solarium_Client extends Solarium_Configurable
*/
const QUERYTYPE_TERMS = 'terms';
/**
* Querytype suggester
*/
const QUERYTYPE_SUGGESTER = 'suggester';
/**
* Default options
*
......@@ -142,6 +147,11 @@ class Solarium_Client extends Solarium_Configurable
'requestbuilder' => 'Solarium_Client_RequestBuilder_Terms',
'responseparser' => 'Solarium_Client_ResponseParser_Terms'
),
self::QUERYTYPE_SUGGESTER => array(
'query' => 'Solarium_Query_Suggester',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Suggester',
'responseparser' => 'Solarium_Client_ResponseParser_Suggester'
),
);
/**
......@@ -691,6 +701,20 @@ class Solarium_Client extends Solarium_Configurable
return $this->execute($query);
}
/**
* Execute a suggester query
*
* @internal This is a convenience method that forwards the query to the
* execute method, thus allowing for an easy to use and clean API.
*
* @param Solarium_Query_Suggester $query
* @return Solarium_Result_Suggester
*/
public function suggester($query)
{
return $this->execute($query);
}
/**
* Create a query instance
*
......@@ -793,4 +817,15 @@ class Solarium_Client extends Solarium_Configurable
{
return $this->createQuery(self::QUERYTYPE_TERMS, $options);
}
/**
* Create a suggester query instance
*
* @param mixed $options
* @return Solarium_Query_Suggester
*/
public function createSuggester($options = null)
{
return $this->createQuery(self::QUERYTYPE_SUGGESTER, $options);
}
}
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Client
*/
/**
* Build a Suggester query request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Suggester extends Solarium_Client_RequestBuilder
{
/**
* Build request for a Suggester query
*
* @param Solarium_Query_Suggester $query
* @return Solarium_Client_Request
*/
public function build($query)
{
$request = parent::build($query);
$request->addParam('spellcheck', 'true');
$request->addParam('q', $query->getQuery());
$request->addParam('spellcheck.dictionary', $query->getDictionary());
$request->addParam('spellcheck.count', $query->getCount());
$request->addParam('spellcheck.onlyMorePopular', $query->getOnlyMorePopular());
$request->addParam('spellcheck.collate', $query->getCollate());
return $request;
}
}
<?php
/**
* Copyright 2011 Gasol Wu. PIXNET Digital Media Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Gasol Wu <gasol.wu@gmail.com>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Client
*/
/**
* Parse Suggester response data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Suggester extends Solarium_Client_ResponseParser
{
/**
* Get result data for the response
*
* @param Solarium_Result_Terms $result
* @return array
*/
public function parse($result)
{
$data = $result->getData();
$query = $result->getQuery();
$status = null;
$queryTime = null;
if (isset($data['responseHeader'])) {
$status = $data['responseHeader']['status'];
$queryTime = $data['responseHeader']['QTime'];
}
$suggestions = array();
$collation = null;
if (isset($data['spellcheck']['suggestions']) && is_array($data['spellcheck']['suggestions'])) {
$suggestResults = $data['spellcheck']['suggestions'];
$termClass = $query->getOption('termclass');
for ($i = 0; $i < count($suggestResults); $i += 2) {
$term = $suggestResults[$i];
$termData = $suggestResults[$i+1];
if ($term == 'collation'&& $i == count($suggestResults)-2) {
$collation = $termData;
} else {
$suggestions[$term] = new $termClass(
$termData['numFound'],
$termData['startOffset'],
$termData['endOffset'],
$termData['suggestion']
);
}
}
}
return array(
'status' => $status,
'queryTime' => $queryTime,
'results' => $suggestions,
'collation' => $collation,
);
}
}
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Query
*/
/**
* Suggester Query
*
* Can be used for an autocomplete feature. See http://wiki.apache.org/solr/Suggester for more info.
*
* @package Solarium
* @subpackage Query
*/
class Solarium_Query_Suggester extends Solarium_Query
{
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Solarium_Client::QUERYTYPE_SUGGESTER;
}
/**
* Default options
*
* @var array
*/
protected $_options = array(
'handler' => 'suggest',
'resultclass' => 'Solarium_Result_Suggester',
'termclass' => 'Solarium_Result_Suggester_Term',
);
/**
* Set query option
*
* Query to spellcheck
*
* @param string $query
* @return self Provides fluent interface
*/
public function setQuery($query)
{
return $this->_setOption('query', $query);
}
/**
* Get query option
*
* @return string|null
*/
public function getQuery()
{
return $this->getOption('query');
}
/**
* Set dictionary option
*
* The name of the dictionary to use
*
* @param string $dictionary
* @return self Provides fluent interface
*/
public function setDictionary($dictionary)
{
return $this->_setOption('dictionary', $dictionary);
}
/**
* Get dictionary option
*
* @return string|null
*/
public function getDictionary()
{
return $this->getOption('dictionary');
}
/**
* Set count option
*
* The maximum number of suggestions to return
*
* @param int $count
* @return self Provides fluent interface
*/
public function setCount($count)
{
return $this->_setOption('count', $count);
}
/**
* Get count option
*
* @return int|null
*/
public function getCount()
{
return $this->getOption('count');
}
/**
* Set onlyMorePopular option
*
* Only return suggestions that result in more hits for the query than the existing query
*
* @param boolean $onlyMorePopular
* @return self Provides fluent interface
*/
public function setOnlyMorePopular($onlyMorePopular)
{
return $this->_setOption('onlymorepopular', $onlyMorePopular);
}
/**
* Get onlyMorePopular option
*
* @return boolean|null
*/
public function getOnlyMorePopular()
{
return $this->getOption('onlymorepopular');
}
/**
* Set collate option
*
* @param boolean $collate
* @return self Provides fluent interface
*/
public function setCollate($collate)
{
return $this->_setOption('collate', $collate);
}
/**
* Get collate option
*
* @return boolean|null
*/
public function getCollate()
{
return $this->getOption('collate');
}
}
......@@ -37,7 +37,7 @@
*/
/**
* Terns query
* Terms query
*
* A terms query provides access to the indexed terms in a field and the number of documents that match each term.
* This can be useful for doing auto-suggest or other things that operate at the term level instead of the search
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Result
*/
/**
* Suggester query result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Suggester extends Solarium_Result_QueryType implements IteratorAggregate, Countable
{
/**
* Status code returned by Solr
*
* @var int
*/
protected $_status;
/**
* Solr index queryTime
*
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
*
* @var int
*/
protected $_queryTime;
/**
* Suggester results
*
* @var array
*/
protected $_results;
/**
* Collation result
*
* Only available when collate is enabled in the suggester query
*
* @var string
*/
protected $_collation;
/**
* Get Solr status code
*
* This is not the HTTP status code! The normal value for success is 0.
*
* @return int
*/
public function getStatus()
{
$this->_parseResponse();
return $this->_status;
}
/**
* Get Solr query time
*
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
*
* @return int
*/
public function getQueryTime()
{
$this->_parseResponse();
return $this->_queryTime;
}
/**
* Get all results
*
* @return array
*/
public function getResults()
{
$this->_parseResponse();
return $this->_results;
}
/**
* Get results for a specific term
*
* @param string $field
* @return array
*/
public function getTerm($term)
{
$this->_parseResponse();
if (isset($this->_results[$term])) {
return $this->_results[$term];
} else {
return array();
}
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
$this->_parseResponse();
return new ArrayIterator($this->_results);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
$this->_parseResponse();
return count($this->_results);
}
/**
* Get collation
*
* @return null|string
*/
public function getCollation()
{
$this->_parseResponse();
return $this->_collation;
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Result
*/
/**
* Suggester query term result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Suggester_Term implements IteratorAggregate, Countable
{
/**
* @var int
*/
protected $_numFound;
/**
* @var int
*/
protected $_startOffset;
/**
* @var int
*/
protected $_endOffset;
/**
* @var array
*/
protected $_suggestions;
/**
* Constructor
*
* @param int $numFound
* @param int $startOffset
* @param int $endOffset
* @param array $suggestions
*/
public function __construct($numFound, $startOffset, $endOffset, $suggestions)
{
$this->_numFound = $numFound;
$this->_startOffset = $startOffset;
$this->_endOffset = $endOffset;
$this->_suggestions = $suggestions;
}
/**
* Get NumFound
*
* @return int
*/
public function getNumFound()
{
return $this->_numFound;
}
/**
* Get StartOffset
*
* @return int
*/
public function getStartOffset()
{
return $this->_startOffset;
}
/**
* Get EndOffset
*
* @return int
*/
public function getEndOffset()
{
return $this->_endOffset;
}
/**
* Get suggestions
*
* @return array
*/
public function getSuggestions()
{
return $this->_suggestions;
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_suggestions);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->_suggestions);
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Client_RequestBuilder_SuggesterTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Suggester
*/
protected $_query;
/**
* @var Solarium_Client_RequestBuilder_Suggester
*/
protected $_builder;
public function setUp()
{
$this->_query = new Solarium_Query_Suggester;
$this->_builder = new Solarium_Client_RequestBuilder_Suggester;
}
public function testBuildParams()
{
$this->_query->setCollate(true);
$this->_query->setCount(13);
$this->_query->setDictionary('suggest');
$this->_query->setQuery('ap ip');
$this->_query->setOnlyMorePopular(true);
$request = $this->_builder->build($this->_query);
$this->assertEquals(
array(
'spellcheck' => 'true',
'q' => 'ap ip',
'spellcheck.dictionary' => 'suggest',
'spellcheck.count' => 13,
'spellcheck.onlyMorePopular' => 'true',
'spellcheck.collate' => 'true',
'wt' => 'json',
),
$request->getParams()
);
$this->assertEquals(
Solarium_Client_Request::METHOD_GET,
$request->getMethod()
);
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Client_ResponseParser_SuggesterTest extends PHPUnit_Framework_TestCase
{
public function testParse()
{
$data = array(
'responseHeader' => array(
'status' => 1,
'QTime' => 13,
),
'spellcheck' => array(
'suggestions' => array(
'd',
array(
'numFound' => 2,
'startOffset' => 3,
'endOffset' => 7,
'suggestion' => array(
'disk',
'ddr'
)
),
'vid',
array(
'numFound' => 1,
'startOffset' => 2,
'endOffset' => 5,
'suggestion' => array(
'video',
)
),
'collation',
'disk video'
),
),
);
$query = new Solarium_Query_Suggester();
$resultStub = $this->getMock('Solarium_Result_Suggester', array(), array(), '', false);
$resultStub->expects($this->any())
->method('getData')
->will($this->returnValue($data));
$resultStub->expects($this->any())
->method('getQuery')
->will($this->returnValue($query));
$parser = new Solarium_Client_ResponseParser_Suggester;
$result = $parser->parse($resultStub);
$expected = array(
'd' => new Solarium_Result_Suggester_Term(2,3,7,array('disk','ddr')),
'vid' => new Solarium_Result_Suggester_Term(1,2,5,array('video'))
);
$this->assertEquals($expected, $result['results']);
$this->assertEquals('disk video', $result['collation']);
}
}
......@@ -656,6 +656,18 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$observer->terms($query);
}
public function testSuggester()
{
$query = new Solarium_Query_Suggester();
$observer = $this->getMock('Solarium_Client', array('execute'));
$observer->expects($this->once())
->method('execute')
->with($this->equalTo($query));
$observer->suggester($query);
}
public function testCreateQuery()
{
$options = array('optionA' => 1, 'optionB' => 2);
......@@ -812,6 +824,18 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$observer->createTerms($options);
}
public function testCreateSuggester()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Client', array('createQuery'));
$observer->expects($this->once())
->method('createQuery')
->with($this->equalTo(Solarium_Client::QUERYTYPE_SUGGESTER), $this->equalTo($options));
$observer->createSuggester($options);
}
public function testTriggerEvent()
{
$eventName = 'Test';
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Query_SuggesterTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Suggester
*/
protected $_query;
public function setUp()
{
$this->_query = new Solarium_Query_Suggester;
}
public function testGetType()
{
$this->assertEquals(Solarium_Client::QUERYTYPE_SUGGESTER, $this->_query->getType());
}
public function testSetAndGetQuery()
{
$value = 'testquery';
$this->_query->setQuery($value);
$this->assertEquals(
$value,
$this->_query->getQuery()
);
}
public function testSetAndGetDictionary()
{
$value = 'myDictionary';
$this->_query->setDictionary($value);
$this->assertEquals(
$value,
$this->_query->getDictionary()
);
}
public function testSetAndGetCount()
{
$value = 11;
$this->_query->setCount($value);
$this->assertEquals(
$value,
$this->_query->getCount()
);
}
public function testSetAndGetOnlyMorePopular()
{
$value = false;
$this->_query->setOnlyMorePopular($value);
$this->assertEquals(
$value,
$this->_query->getOnlyMorePopular()
);
}
public function testSetAndGetCollate()
{
$value = false;
$this->_query->setCollate($value);
$this->assertEquals(
$value,
$this->_query->getCollate()
);
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Result_Suggester_TermTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Suggester_Term
*/
protected $_result;
/**
* @var int
*/
protected $_numFound;
/**
* @var int
*/
protected $_startOffset;
/**
* @var int
*/
protected $_endOffset;
/**
* @var array
*/
protected $_suggestions;
public function setUp()
{
$this->_numFound = 5;
$this->_startOffset = 2;
$this->_endOffset = 6;
$this->_suggestions = array(
'suggestion1',
'suggestion2',
);
$this->_result = new Solarium_Result_Suggester_Term(
$this->_numFound, $this->_startOffset, $this->_endOffset, $this->_suggestions
);
}
public function testGetNumFound()
{
$this->assertEquals(
$this->_numFound,
$this->_result->getNumFound()
);
}
public function testGetStartOffset()
{
$this->assertEquals(
$this->_startOffset,
$this->_result->getStartOffset()
);
}
public function testGetEndOffset()
{
$this->assertEquals(
$this->_endOffset,
$this->_result->getEndOffset()
);
}
public function testGetSuggestions()
{
$this->assertEquals(
$this->_suggestions,
$this->_result->getSuggestions()
);
}
public function testCount()
{
$this->assertEquals(count($this->_suggestions), count($this->_result));
}
public function testIterator()
{
$results = array();
foreach($this->_result AS $key => $doc)
{
$results[$key] = $doc;
}
$this->assertEquals($this->_suggestions, $results);
}
}
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Result_SuggesterTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Suggester
*/
protected $_result;
/**
* @var array
*/
protected $_data;
/**
* @var string
*/
protected $_collation;
public function setUp()
{
$this->_data = array(
'term1' => 'data1',
'term2' => 'data2',
);
$this->_collation = 'collation result';
$this->_result = new Solarium_Result_SuggesterDummy($this->_data, $this->_collation);
}
public function testGetStatus()
{
$this->assertEquals(
1,
$this->_result->getStatus()
);
}
public function testGetQueryTime()
{
$this->assertEquals(
12,
$this->_result->getQueryTime()
);
}
public function testGetResults()
{
$this->assertEquals($this->_data, $this->_result->getResults());
}
public function testGetTerm()
{
$this->assertEquals($this->_data['term1'], $this->_result->getTerm('term1'));
}
public function testGetTermsWithInvalidFieldName()
{
$this->assertEquals(array(), $this->_result->getTerm('term3'));
}
public function testCount()
{
$this->assertEquals(count($this->_data), count($this->_result));
}
public function testIterator()
{
$results = array();
foreach($this->_result AS $key => $doc)
{
$results[$key] = $doc;
}
$this->assertEquals($this->_data, $results);
}
public function testGetCollation()
{
$this->assertEquals($this->_collation, $this->_result->getCollation());
}
}
class Solarium_Result_SuggesterDummy extends Solarium_Result_Suggester
{
protected $_parsed = true;
public function __construct($results, $collation)
{
$this->_results = $results;
$this->_collation = $collation;
$this->_status = 1;
$this->_queryTime = 12;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment