Skip to content

Commit

Permalink
Added support for realtime get queries
Browse files Browse the repository at this point in the history
  • Loading branch information
basdenooijer committed Jan 2, 2013
1 parent f848d22 commit 05cea9e
Show file tree
Hide file tree
Showing 9 changed files with 689 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/2.8-realtime-get-query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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();
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ <h1>Solarium examples</h1>

<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>
Expand Down
32 changes: 32 additions & 0 deletions library/Solarium/Core/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class Client extends Configurable
*/
const QUERY_EXTRACT = 'extract';

/**
* Querytype get
*/
const QUERY_REALTIME_GET = 'get';

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

/**
Expand Down Expand Up @@ -954,6 +960,21 @@ public function extract(QueryInterface $query, $endpoint = null)
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
*
Expand Down Expand Up @@ -1089,4 +1110,15 @@ public function createExtract($options = null)
{
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);
}
}
224 changes: 224 additions & 0 deletions library/Solarium/QueryType/RealtimeGet/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?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 <[email protected]>
* @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();
}
}
Loading

0 comments on commit 05cea9e

Please sign in to comment.