Skip to content

Commit

Permalink
Merge branch 'feature/suggester' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
basdenooijer committed Jan 13, 2012
2 parents c48ca8c + 84332b2 commit 9e02303
Show file tree
Hide file tree
Showing 15 changed files with 1,295 additions and 1 deletion.
39 changes: 39 additions & 0 deletions examples/2.6-suggester-query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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();
2 changes: 2 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ <h1>Solarium examples</h1>
</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>
Expand Down
35 changes: 35 additions & 0 deletions library/Solarium/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ class Solarium_Client extends Solarium_Configurable
*/
const QUERYTYPE_TERMS = 'terms';

/**
* Querytype suggester
*/
const QUERYTYPE_SUGGESTER = 'suggester';

/**
* Default options
*
Expand Down Expand Up @@ -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'
),
);

/**
Expand Down Expand Up @@ -691,6 +701,20 @@ public function terms($query)
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
*
Expand Down Expand Up @@ -793,4 +817,15 @@ public function createTerms($options = null)
{
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);
}
}
67 changes: 67 additions & 0 deletions library/Solarium/Client/RequestBuilder/Suggester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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;
}

}
98 changes: 98 additions & 0 deletions library/Solarium/Client/ResponseParser/Suggester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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,
);
}

}
Loading

0 comments on commit 9e02303

Please sign in to comment.