Commit cfbe19df authored by Thomas's avatar Thomas

Added support for deep paging with a cursor

parent e66f168e
......@@ -91,6 +91,13 @@ class PrefetchIterator extends AbstractPlugin implements \Iterator, \Countable
*/
protected $position;
/**
* Cursor mark.
*
* @var string
*/
protected $cursormark;
/**
* Documents from the last resultset.
*
......@@ -196,6 +203,10 @@ class PrefetchIterator extends AbstractPlugin implements \Iterator, \Countable
// this condition prevent useless re-fetching of data if a count is done before the iterator is used
if ($this->start !== $this->options['prefetch']) {
$this->start = 0;
if (null !== $this->cursormark) {
$this->cursormark = '*';
}
}
}
......@@ -251,8 +262,19 @@ class PrefetchIterator extends AbstractPlugin implements \Iterator, \Countable
*/
protected function fetchNext()
{
if (null === $this->cursormark && null !== $this->query->getCursormark()) {
$this->cursormark = '*';
}
if (null === $this->cursormark) {
$this->query->setStart($this->start)->setRows($this->getPrefetch());
}
else {
$this->query->setCursormark($this->cursormark)->setRows($this->getPrefetch());
}
$this->result = $this->client->execute($this->query, $this->getOption('endpoint'));
$this->cursormark = $this->result->getNextCursorMark();
$this->documents = $this->result->getDocuments();
$this->start += $this->getPrefetch();
}
......@@ -266,5 +288,6 @@ class PrefetchIterator extends AbstractPlugin implements \Iterator, \Countable
$this->result = null;
$this->documents = null;
$this->start = 0;
$this->cursormark = null;
}
}
......@@ -1057,6 +1057,40 @@ class Query extends BaseQuery
return $this->addTags($tags);
}
/**
* Set the cursor mark to fetch.
*
* Cursor functionality requires a sort containing a uniqueKey field tie breaker
*
* @param string $cursormark
*
* @return self Provides fluent interface
*/
public function setCursormark($cursormark)
{
return $this->setOption('cursormark', $cursormark);
}
/**
* Get the cursor mark.
*
* @return string|null
*/
public function getCursormark()
{
return $this->getOption('cursormark');
}
/**
* Remove the cursor mark.
*
* @return self Provides fluent interface
*/
public function clearCursormark()
{
return $this->setOption('cursormark', null);
}
/**
* Initialize options.
*
......@@ -1094,6 +1128,9 @@ class Query extends BaseQuery
}
$this->addTags($value);
break;
case 'cursormark':
$this->setCursormark($value);
break;
}
}
}
......
......@@ -74,6 +74,7 @@ class RequestBuilder extends BaseRequestBuilder
$request->addParam('fl', implode(',', $query->getFields()));
$request->addParam('q.op', $query->getQueryDefaultOperator());
$request->addParam('df', $query->getQueryDefaultField());
$request->addParam('cursorMark', $query->getCursormark());
// add sort fields to request
$sort = array();
......
......@@ -107,6 +107,12 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt
$maxScore = null;
}
if (isset($data['nextCursorMark'])) {
$nextCursorMark = $data['nextCursorMark'];
} else {
$nextCursorMark = null;
}
return $this->addHeaderInfo(
$data,
array(
......@@ -114,6 +120,7 @@ class ResponseParser extends ResponseParserAbstract implements ResponseParserInt
'maxscore' => $maxScore,
'documents' => $documents,
'components' => $components,
'nextcursormark' => $nextCursorMark,
)
);
}
......
......@@ -83,6 +83,15 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
*/
protected $maxscore;
/**
* Solr nextcursormark.
*
* Will only be available if 'cursormark' was set for your query
*
* @var string
*/
protected $nextcursormark;
/**
* Document instances array.
*
......@@ -171,6 +180,21 @@ class Result extends BaseResult implements \IteratorAggregate, \Countable
return $this->maxscore;
}
/**
* get Solr nextcursormark.
*
* Returns the next cursor mark for deep paging
* Will only be available if 'cursormark' was set for your query
*
* @return string
*/
public function getNextCursorMark()
{
$this->parseResponse();
return $this->nextcursormark;
}
/**
* Get all documents.
*
......
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