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