Commit 05cea9ed authored by Bas de Nooijer's avatar Bas de Nooijer

Added support for realtime get queries

parent f848d228
<?php
require(__DIR__.'/init.php');
htmlHeader();
// create a client instance
$client = new Solarium\Client($config);
// get an update query instance
$update = $client->createUpdate();
// create a new document
$id = time();
$doc1 = $update->createDocument();
$doc1->id = $id;
$doc1->name = 'realtime-get-test-'.date('Y-m-d H:i:s');
// set a very long commitWithin time and add it to solr
$update->addDocument($doc1, null, 1000000);
$client->update($update);
// try to get the document using a normal select, this should return 0 results
$query = $client->createSelect();
$query->setQuery('id:%1%', array($id));
$resultset = $client->select($query);
echo 'NumFound with standard select: '.$resultset->getNumFound().'<br/>';
// now with realtime get, this should return 1 result
$query = $client->createRealtimeGet();
$query->addId($id);
$result = $client->realtimeGet($query);
echo 'NumFound with realtime get: '.$result->getNumFound().'<br/>';
// Display the document
echo '<hr/><table>';
foreach($result->getDocument() AS $field => $value)
{
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
htmlFooter();
\ No newline at end of file
......@@ -86,6 +86,7 @@
<li><a href="2.6-suggester-query.php">2.6 Suggester query</a></li>
<li><a href="2.7-extract-query.php">2.7 Extract query</a></li>
<li><a href="2.8-realtime-get-query.php">2.8 Realtime get query</a></li>
</ul>
<li>4. Usage modes</li>
......
......@@ -122,6 +122,11 @@ class Client extends Configurable
*/
const QUERY_EXTRACT = 'extract';
/**
* Querytype get
*/
const QUERY_REALTIME_GET = 'get';
/**
* Default options
*
......@@ -149,6 +154,7 @@ class Client extends Configurable
self::QUERY_TERMS => 'Solarium\QueryType\Terms\Query',
self::QUERY_SUGGESTER => 'Solarium\QueryType\Suggester\Query',
self::QUERY_EXTRACT => 'Solarium\QueryType\Extract\Query',
self::QUERY_REALTIME_GET => 'Solarium\QueryType\RealtimeGet\Query',
);
/**
......@@ -954,6 +960,21 @@ class Client extends Configurable
return $this->execute($query, $endpoint);
}
/**
* Execute a RealtimeGet 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\QueryType\RealtimeGet\Query $query
* @param Endpoint|string|null
* @return \Solarium\QueryType\RealtimeGet\Result
*/
public function realtimeGet(QueryInterface $query, $endpoint = null)
{
return $this->execute($query, $endpoint);
}
/**
* Create a query instance
*
......@@ -1089,4 +1110,15 @@ class Client extends Configurable
{
return $this->createQuery(self::QUERY_EXTRACT, $options);
}
/**
* Create a RealtimeGet query instance
*
* @param mixed $options
* @return \Solarium\QueryType\RealtimeGet\Query
*/
public function createRealtimeGet($options = null)
{
return $this->createQuery(self::QUERY_REALTIME_GET, $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/
*/
/**
* @namespace
*/
namespace Solarium\QueryType\RealtimeGet;
use Solarium\Core\Query\Query as BaseQuery;
use Solarium\Core\Client\Client;
use Solarium\QueryType\RealtimeGet\RequestBuilder as RequestBuilder;
/**
* Get query
*
* Realtime Get query for one or multiple document IDs
*
* @see http://wiki.apache.org/solr/RealTimeGet
*/
class Query extends BaseQuery
{
/**
* Default options
*
* @var array
*/
protected $options = array(
'resultclass' => 'Solarium\QueryType\RealtimeGet\Result',
'documentclass' => 'Solarium\QueryType\Select\Result\Document',
'handler' => 'get',
'omitheader' => true,
);
/**
* Document IDs
*
* @var array
*/
protected $ids = array();
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Client::QUERY_REALTIME_GET;
}
/**
* Get a requestbuilder for this query
*
* @return RequestBuilder
*/
public function getRequestBuilder()
{
return new RequestBuilder;
}
/**
* The ping query has no response parser so we return a null value
*
* @return null;
*/
public function getResponseParser()
{
return new \Solarium\QueryType\Select\ResponseParser\ResponseParser;
}
/**
* Add an id
*
* @param string $id
* @return self Provides fluent interface
*/
public function addId($id)
{
$this->ids[$id] = true;
return $this;
}
/**
* Add multiple ids
*
* @param string|array $ids can be an array or string with comma separated ids
*
* @return self Provides fluent interface
*/
public function addIds($ids)
{
if (is_string($ids)) {
$ids = explode(',', $ids);
$ids = array_map('trim', $ids);
}
foreach ($ids as $id) {
$this->addId($id);
}
return $this;
}
/**
* Remove an id
*
* @param string $id
* @return self Provides fluent interface
*/
public function removeId($id)
{
if (isset($this->ids[$id])) {
unset($this->ids[$id]);
}
return $this;
}
/**
* Remove all IDs
*
* @return self Provides fluent interface
*/
public function clearIds()
{
$this->ids = array();
return $this;
}
/**
* Get the list of ids
*
* @return array
*/
public function getIds()
{
return array_keys($this->ids);
}
/**
* Set multiple ids
*
* This overwrites any existing ids
*
* @param array $ids
* @return self Provides fluent interface
*/
public function setIds($ids)
{
$this->clearIds();
$this->addIds($ids);
return $this;
}
/**
* Set a custom document class
*
* This class should implement the document interface
*
* @param string $value classname
* @return self Provides fluent interface
*/
public function setDocumentClass($value)
{
return $this->setOption('documentclass', $value);
}
/**
* Get the current documentclass option
*
* The value is a classname, not an instance
*
* @return string
*/
public function getDocumentClass()
{
return $this->getOption('documentclass');
}
/**
* No components for this querytype
*
* @return array
*/
public function getComponents()
{
return array();
}
}
<?php
/**
* Copyright 2012 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 2012 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\QueryType\RealtimeGet;
use Solarium\Core\Client\Request;
use Solarium\Core\Query\RequestBuilder as BaseRequestBuilder;
use Solarium\Core\Query\QueryInterface;
/**
* Build a RealtimeGet request
*/
class RequestBuilder extends BaseRequestBuilder
{
/**
* Build request for a ping query
*
* @param Query $query
* @return Request
*/
public function build(QueryInterface $query)
{
$request = parent::build($query);
$request->setMethod(Request::METHOD_GET);
$request->addParam('ids', implode(',', $query->getIds()));
return $request;
}
}
<?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/
*/
/**
* @namespace
*/
namespace Solarium\QueryType\RealtimeGet;
use Solarium\QueryType\Select\Result\Result as BaseResult;
use Solarium\QueryType\Select\Result\DocumentInterface;
/**
* RealtimeGet query results
*
* Extends the standard select result with a accessor method for the first document
*/
class Result extends BaseResult implements \IteratorAggregate, \Countable
{
/**
* Get first document in set
*
* @return DocumentInterface
*/
public function getDocument()
{
$docs = $this->getDocuments();
return reset($docs);
}
}
<?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.
*/
namespace Solarium\Tests\QueryType\RealtimeGet;
use Solarium\QueryType\RealtimeGet\Query;
use Solarium\Core\Client\Client;
class QueryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Query
*/
protected $query;
public function setUp()
{
$this->query = new Query;
}
public function testGetType()
{
$this->assertEquals(Client::QUERY_REALTIME_GET, $this->query->getType());
}
public function testGetResponseParser()
{
$this->assertInstanceOf('Solarium\QueryType\Select\ResponseParser\ResponseParser', $this->query->getResponseParser());
}
public function testGetRequestBuilder()
{
$this->assertInstanceOf('Solarium\QueryType\RealtimeGet\RequestBuilder', $this->query->getRequestBuilder());
}
public function testSetAndGetDocumentClass()
{
$this->query->setDocumentClass('MyDocument');
$this->assertEquals('MyDocument', $this->query->getDocumentClass());
}
public function testGetComponents()
{
$this->assertEquals(array(), $this->query->getComponents());
}
public function testAddId()
{
$expectedIds = $this->query->getIds();
$expectedIds[] = 'newid';
$this->query->addId('newid');
$this->assertEquals($expectedIds, $this->query->getIds());
}
public function testClearIds()
{
$this->query->addId('newid');
$this->query->clearIds();
$this->assertEquals(array(), $this->query->getIds());
}
public function testAddIds()
{
$ids = array('id1','id2');
$this->query->clearIds();
$this->query->addIds($ids);
$this->assertEquals($ids, $this->query->getIds());
}
public function testAddIdsAsStringWithTrim()
{
$this->query->clearIds();
$this->query->addIds('id1, id2');
$this->assertEquals(array('id1','id2'), $this->query->getIds());
}
public function testRemoveId()
{
$this->query->clearIds();
$this->query->addIds(array('id1','id2'));
$this->query->removeId('id1');
$this->assertEquals(array('id2'), $this->query->getIds());
}
public function testSetIds()
{
$this->query->clearIds();
$this->query->addIds(array('id1','id2'));
$this->query->setIds(array('id3','id4'));
$this->assertEquals(array('id3','id4'), $this->query->getIds());
}
}
<?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.
*/
namespace Solarium\Tests\QueryType\RealtimeGet;
use Solarium\Core\Client\Request;
use Solarium\QueryType\RealtimeGet\Query;
use Solarium\QueryType\RealtimeGet\RequestBuilder;
class RequestBuilderTest extends \PHPUnit_Framework_TestCase
{
public function testBuildSingleId()
{
$query = new Query;
$query->addId(123);
$builder = new RequestBuilder();
$request = $builder->build($query);
$this->assertEquals(
$request::METHOD_GET,
$request->getMethod()
);
$this->assertEquals(
'get?omitHeader=true&wt=json&ids=123',
urldecode($request->getUri())
);
}
public function testBuildMultiId()
{
$query = new Query;
$query->addId(123)->addId(456);
$builder = new RequestBuilder();
$request = $builder->build($query);
$this->assertEquals(
$request::METHOD_GET,
$request->getMethod()
);
$this->assertEquals(
'get?omitHeader=true&wt=json&ids=123,456',
urldecode($request->getUri())
);
}
}
<?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.
*/
namespace Solarium\Tests\QueryType\RealtimeGet;
use Solarium\QueryType\RealtimeGet\Result;
use Solarium\QueryType\Select\Result\Document;
class ResultTest extends \PHPUnit_Framework_TestCase
{
protected $doc, $result;
public function setUp()
{
$this->doc = new Document(array('id'=>1,'title'=>'doc1'));
$this->result = new ResultDummy(array($this->doc));
}
public function testGetDocument()
{
$this->assertEquals($this->doc, $this->result->getDocument());
}
}
class ResultDummy extends Result
{
protected $parsed = true;
public function __construct($docs)
{
$this->documents = $docs;
}
}
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