Commit 559c9c6d authored by Bas de Nooijer's avatar Bas de Nooijer

- added component structure

- added morelikethis as the first component
parent a23a3644
......@@ -81,6 +81,17 @@ class Solarium_Client_Request_Select extends Solarium_Client_Request
}
}
foreach ($this->_query->getComponents() as $component) {
switch ($component->getType())
{
case Solarium_Query_Select_Component::MORELIKETHIS:
$this->addMoreLikeThis($component);
break;
default:
throw new Solarium_Exception('Unknown component type');
}
}
$facets = $this->_query->getFacets();
if (count($facets) !== 0) {
......@@ -149,4 +160,27 @@ class Solarium_Client_Request_Select extends Solarium_Client_Request
);
}
/**
* Add params for morelikethis
*
* @param Solarium_Query_Select_Component_MoreLikeThis $component
* @return void
*/
public function addMoreLikeThis($component)
{
// enable morelikethis
$this->_params['mlt'] = 'true';
$this->addParam('mlt.fl', $component->getFields());
$this->addParam('mlt.mintf', $component->getMinimumTermFrequency());
$this->addParam('mlt.mindf', $component->getMinimumDocumentFrequency());
$this->addParam('mlt.minwl', $component->getMinimumWordLength());
$this->addParam('mlt.maxwl', $component->getMaximumWordLength());
$this->addParam('mlt.maxqt', $component->getMaximumQueryTerms());
$this->addParam('mlt.maxntp', $component->getMaximumNumberOfTokens());
$this->addParam('mlt.boost', $component->getBoost());
$this->addParam('mlt.qf', $component->getQueryFields());
$this->addParam('mlt.count', $component->getCount());
}
}
\ No newline at end of file
......@@ -56,6 +56,13 @@ class Solarium_Client_Response_Select extends Solarium_Client_Response
*/
protected $_facets = array();
/**
* Component results
*
* @var array
*/
protected $_components = array();
/**
* Get a result instance for the response
*
......@@ -75,6 +82,18 @@ class Solarium_Client_Response_Select extends Solarium_Client_Response
}
}
// component results
foreach ($this->_query->getComponents() as $component) {
switch ($component->getType())
{
case Solarium_Query_Select_Component::MORELIKETHIS:
$this->_addMoreLikeThis($component);
break;
default:
throw new Solarium_Exception('Unknown component type');
}
}
// create facet results
foreach ($this->_query->getFacets() AS $facet) {
switch ($facet->getType()) {
......@@ -97,7 +116,7 @@ class Solarium_Client_Response_Select extends Solarium_Client_Response
// create the result instance that combines all data
$resultClass = $this->_query->getOption('resultclass');
return new $resultClass(
$status, $queryTime, $numFound, $documents, $this->_facets
$status, $queryTime, $numFound, $documents, $this->_facets, $this->_components
);
}
......@@ -143,4 +162,40 @@ class Solarium_Client_Response_Select extends Solarium_Client_Response
new Solarium_Result_Select_Facet_Query($value);
}
}
/**
* Add morelikethis result
*
* @param Solarium_Query_Select_Component_MoreLikeThis $component
* @return void
*/
protected function _addMoreLikeThis($component)
{
$results = array();
if (isset($this->_data['moreLikeThis'])) {
$documentClass = $this->_query->getOption('documentclass');
$searchResults = $this->_data['moreLikeThis'];
foreach($searchResults AS $key => $result) {
// create document instances
$docs = array();
foreach ($result['docs'] AS $fields) {
$docs[] = new $documentClass($fields);
}
$results[$key] = new Solarium_Result_Select_MoreLikeThis_Result(
$result['numFound'],
$result['maxScore'],
$docs
);
}
}
$moreLikeThis = new Solarium_Result_Select_MoreLikeThis($results);
$this->_components[$component->getType()] = $moreLikeThis;
}
}
\ No newline at end of file
......@@ -97,6 +97,13 @@ class Solarium_Query_Select extends Solarium_Query
*/
protected $_facets = array();
/**
* Search components
*
* @var array
*/
protected $_components = array();
/**
* Initialize options
*
......@@ -643,4 +650,66 @@ class Solarium_Query_Select extends Solarium_Query
$this->addFacets($facets);
}
/**
* Get all registered components
*
* @return array
*/
public function getComponents()
{
return $this->_components;
}
/**
* Get a component instance by key
*
* You can optionally supply an autoload class to create a new component
* instance if there is no registered component for the given key yet.
*
* @param string $key Use one of the constants
* @param string $autoload Class to autoload if component needs to be created
* @return object|null
*/
public function getComponent($key, $autoload = null)
{
if (isset($this->_components[$key]) && $this->_components[$key] !== null) {
return $this->_components[$key];
} else {
if ($autoload !== null) {
$component = new $autoload;
$this->setComponent($key, $component);
return $this->_components[$key];
}
return null;
}
}
/**
* Set a component instance
*
* This overwrites any existing component registered with the same key.
* If you want to remove a component use NULL as value.
*
* @param string $key
* @param object|null $value
* @return Solarium_Query_Select Provides fluent interface
*/
public function setComponent($key, $value)
{
$this->_components[$key] = $value;
return $this;
}
/**
* Get a MoreLikeThis component instance
*
* This is a convenience method that maps presets to getComponent
*
* @return Solarium_Query_Select_Component_MoreLikeThis
*/
public function getMoreLikeThis()
{
return $this->getComponent('MoreLikeThis', 'Solarium_Query_Select_Component_MoreLikeThis');
}
}
\ 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
*
* @package Solarium
* @subpackage Query
*/
/**
* Query component base class
*
* @package Solarium
* @subpackage Query
*/
class Solarium_Query_Select_Component extends Solarium_Configurable
{
/**
* Component types
*/
const MORELIKETHIS = 'morelikethis';
/**
* Component type
*
* To be implemented in extending classes
*
* @var string
*/
protected $_type = '';
/**
* Get component type
*
* @return string
*/
public function getType()
{
return $this->_type;
}
}
\ 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
*
* @package Solarium
* @subpackage Query
*/
/**
* MoreLikeThis component
*
* @link http://wiki.apache.org/solr/MoreLikeThis
*
* @package Solarium
* @subpackage Query
*/
class Solarium_Query_Select_Component_MoreLikeThis
extends Solarium_Query_Select_Component
{
/**
* Component type
*
* @var string
*/
protected $_type = self::MORELIKETHIS;
/**
* Default options
*
* @var array
*/
protected $_options = array(
'fields' => null,
'minimumtermfrequency' => null,
'minimumdocumentfrequency' => null,
'minimumwordlength' => null,
'maximumwordlength' => null,
'maximumqueryterms' => null,
'maximumnumberoftokens' => null,
'boost' => null,
'queryfields' => null,
'count' => null,
);
/**
* Set fields option
*
* The fields to use for similarity. NOTE: if possible, these should have a
* stored TermVector
*
* Separate multiple fields with commas.
*
* @param string $fields
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setFields($fields)
{
return $this->_setOption('fields',$fields);
}
/**
* Get fields option
*
* @return string|null
*/
public function getFields()
{
return $this->getOption('fields');
}
/**
* Set minimumtermfrequency option
*
* Minimum Term Frequency - the frequency below which terms will be ignored
* in the source doc.
*
* @param int $minimum
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setMinimumTermFrequency($minimum)
{
return $this->_setOption('minimumtermfrequency',$minimum);
}
/**
* Get minimumtermfrequency option
*
* @return integer|null
*/
public function getMinimumTermFrequency()
{
return $this->getOption('minimumtermfrequency');
}
/**
* Set minimumdocumentfrequency option
*
* Minimum Document Frequency - the frequency at which words will be
* ignored which do not occur in at least this many docs.
*
* @param int $minimum
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setMinimumDocumentFrequency($minimum)
{
return $this->_setOption('minimumdocumentfrequency',$minimum);
}
/**
* Get minimumdocumentfrequency option
*
* @return integer|null
*/
public function getMinimumDocumentFrequency()
{
return $this->getOption('minimumdocumentfrequency');
}
/**
* Set minimumwordlength option
*
* Minimum word length below which words will be ignored.
*
* @param int $minimum
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setMinimumWordLength($minimum)
{
return $this->_setOption('minimumwordlength',$minimum);
}
/**
* Get minimumwordlength option
*
* @return integer|null
*/
public function getMinimumWordLength()
{
return $this->getOption('minimumwordlength');
}
/**
* Set maximumwordlength option
*
* Maximum word length above which words will be ignored.
*
* @param int $maximum
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setMaximumWordLength($maximum)
{
return $this->_setOption('maximumwordlength',$maximum);
}
/**
* Get maximumwordlength option
*
* @return integer|null
*/
public function getMaximumWordLength()
{
return $this->getOption('maximumwordlength');
}
/**
* Set maximumqueryterms option
*
* Maximum number of query terms that will be included in any generated
* query.
*
* @param int $maximum
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setMaximumQueryTerms($maximum)
{
return $this->_setOption('maximumqueryterms',$maximum);
}
/**
* Get maximumqueryterms option
*
* @return integer|null
*/
public function getMaximumQueryTerms()
{
return $this->getOption('maximumqueryterms');
}
/**
* Set maximumnumberoftokens option
*
* Maximum number of tokens to parse in each example doc field that is not
* stored with TermVector support.
*
* @param int $maximum
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setMaximumNumberOfTokens($maximum)
{
return $this->_setOption('maximumnumberoftokens',$maximum);
}
/**
* Get maximumnumberoftokens option
*
* @return integer|null
*/
public function getMaximumNumberOfTokens()
{
return $this->getOption('maximumnumberoftokens');
}
/**
* Set boost option
*
* If true the query will be boosted by the interesting term relevance.
*
* @param boolean $boost
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setBoost($boost)
{
return $this->_setOption('boost',$boost);
}
/**
* Get boost option
*
* @return boolean|null
*/
public function getBoost()
{
return $this->getOption('boost');
}
/**
* Set queryfields option
*
* Query fields and their boosts using the same format as that used in
* DisMaxQParserPlugin. These fields must also be specified in fields.
*
* Separate multiple fields with commas.
*
* @param string $queryFields
* @return Solarium_Query_Component_MoreLikeThis Provides fluent interface
*/
public function setQueryFields($queryFields)
{
return $this->_setOption('queryfields',$queryFields);
}
/**
* Get queryfields option
*
* @return string|null
*/
public function getQueryFields()
{
return $this->getOption('queryfields');
}
/**
* Set count option
*
* The number of similar documents to return for each result
*
* @param int $count
* @return Solarium_Query_Component_MoreLikeThis 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');
}
}
\ No newline at end of file
......@@ -85,6 +85,11 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/
protected $_facets;
/**
* Component results
*/
protected $_components;
/**
* Constructor
*
......@@ -96,16 +101,18 @@ class Solarium_Result_Select extends Solarium_Result_Query
* @param int $numFound
* @param array $documents
* @param array $facets
* @param array $components
* @return void
*/
public function __construct($status, $queryTime, $numFound, $documents,
$facets)
$facets, $components)
{
$this->_status = $status;
$this->_queryTime = $queryTime;
$this->_numFound = $numFound;
$this->_documents = $documents;
$this->_facets = $facets;
$this->_components = $components;
}
/**
......@@ -175,4 +182,41 @@ class Solarium_Result_Select extends Solarium_Result_Query
{
return count($this->_documents);
}
/**
* Get all component results
*
* @return array
*/
public function getComponents()
{
return $this->_components;
}
/**
* Get a component result by key
*
* @param string $key
* @return Solarium_Result_Select_Component
*/
public function getComponent($key)
{
if (isset($this->_components[$key])) {
return $this->_components[$key];
} else {
return null;
}
}
/**
* Get morelikethis component result
*
* This is a convenience method that maps presets to getComponent
*
* @return Solarium_Result_Select_Component_MoreLikeThis
*/
public function getMoreLikeThis()
{
return $this->getComponent(Solarium_Query_Select_Component::MORELIKETHIS);
}
}
\ 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
*
* @package Solarium
* @subpackage Result
*/
/**
* Select component morelikethis result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_MoreLikeThis implements IteratorAggregate, Countable
{
/**
* Result array
*
* @var array
*/
protected $_results;
/**
* Constructor
*
* @param array $results
* @return void
*/
public function __construct($results)
{
$this->_results = $results;
}
/**
* Get a result by key
*
* @param mixed $key
* @return Solarium_Result_Select_MoreLikeThis_Result|null
*/
public function getResult($key)
{
if (isset($this->_results[$key])) {
return $this->_results[$key];
} else {
return null;
}
}
/**
* Get all results
*
* @return array
*/
public function getResults()
{
return $this->_results;
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_results);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->_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.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Result
*/
/**
* Select component morelikethis result item
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_MoreLikeThis_Result implements IteratorAggregate, Countable
{
/**
* Document instances array
*
* @var array
*/
protected $_documents;
/**
* Solr numFound
*
* This is NOT the number of MLT documents fetched from Solr!
*
* @var int
*/
protected $_numFound;
/**
* Maximum score in this MLT set
*
* @var float
*/
protected $_maxScore;
/**
* Constructor
*
* @param int $numFound
* @param float $maxScore
* @param array $documents
* @return void
*/
public function __construct($numFound, $maxScore, $documents)
{
$this->_numFound = $numFound;
$this->_maxScore = $maxScore;
$this->_documents = $documents;
}
/**
* get Solr numFound
*
* Returns the number of MLT documents found by Solr (this is NOT the
* number of documents fetched from Solr!)
*
* @return int
*/
public function getNumFound()
{
return $this->_numFound;
}
/**
* Get maximum score in the MLT document set
*
* @return float
*/
public function getMaxScore()
{
return $this->_maxScore;
}
/**
* Get all documents
*
* @return array
*/
public function getDocuments()
{
return $this->_documents;
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_documents);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->_documents);
}
}
\ 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