Skip to content

Commit

Permalink
Update SearchResponseIterator to remove old-style scan/scroll flow
Browse files Browse the repository at this point in the history
  • Loading branch information
polyfractal committed Sep 15, 2016
1 parent 90cbdfb commit 72f3b15
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 70 deletions.
22 changes: 10 additions & 12 deletions src/Elasticsearch/Helper/Iterators/SearchResponseIterator.php
Expand Up @@ -108,8 +108,6 @@ private function clearScroll()
/**
* Rewinds the iterator by performing the initial search.
*
* The "search_type" parameter will determine if the first "page" contains
* hits or if the first page contains just a "scroll_id"
*
* @return void
* @see Iterator::rewind()
Expand All @@ -130,27 +128,27 @@ public function rewind()
*/
public function next()
{
if ($this->current_key !== 0) {
$this->current_scrolled_response = $this->client->scroll(
array(
'scroll_id' => $this->scroll_id,
'scroll' => $this->scroll_ttl
)
);
$this->scroll_id = $this->current_scrolled_response['_scroll_id'];
}
$this->current_key++;
$this->current_scrolled_response = $this->client->scroll(
array(
'scroll_id' => $this->scroll_id,
'scroll' => $this->scroll_ttl
)
);
$this->scroll_id = $this->current_scrolled_response['_scroll_id'];
}

/**
* Returns a boolean value indicating if the current page is valid or not
* based on the number of hits in the page considering that the first page
* might not include any hits
*
* @return bool
* @see Iterator::valid()
*/
public function valid()
{
return ($this->current_key === 0) || isset($this->current_scrolled_response['hits']['hits'][0]);
return isset($this->current_scrolled_response['hits']['hits'][0]);
}

/**
Expand Down
Expand Up @@ -23,7 +23,6 @@ public function tearDown()
public function testWithNoResults()
{
$search_params = array(
'search_type' => 'scan',
'scroll' => '5m',
'index' => 'twitter',
'size' => 1000,
Expand All @@ -43,32 +42,7 @@ public function testWithNoResults()
->andReturn(array('_scroll_id' => 'scroll_id_01'));

$mock_client->shouldReceive('scroll')
->once()
->ordered()
->with(
array(
'scroll_id' => 'scroll_id_01',
'scroll' => '5m'
)
)
->andReturn(
array(
'_scroll_id' => 'scroll_id_02',
'hits' => array(
'hits' => array(
)
)
)
);

$mock_client->shouldReceive('scroll')
->never()
->with(
array(
'scroll_id' => 'scroll_id_02',
'scroll' => '5m'
)
);
->never();

$mock_client->shouldReceive('clearScroll')
->once()
Expand All @@ -81,10 +55,9 @@ public function testWithNoResults()
$this->assertCount(0, $responses);
}

public function testWithScan()
public function testWithHits()
{
$search_params = array(
'search_type' => 'scan',
'scroll' => '5m',
'index' => 'twitter',
'size' => 1000,
Expand All @@ -101,72 +74,85 @@ public function testWithScan()
->once()
->ordered()
->with($search_params)
->andReturn(array('_scroll_id' => 'scroll_id_01'));
->andReturn([
'_scroll_id' => 'scroll_id_01',
'hits' => [
'hits' => [
[
'foo' => 'bar'
]
]
]
]);

$mock_client->shouldReceive('scroll')
->once()
->ordered()
->with(
array(
[
'scroll_id' => 'scroll_id_01',
'scroll' => '5m'
)
]
)
->andReturn(
array(
[
'_scroll_id' => 'scroll_id_02',
'hits' => array(
'hits' => array(
array()
)
)
)
);
'hits' => [
'hits' => [
[
'foo' => 'bar'
]
]
]
]);

$mock_client->shouldReceive('scroll')
->once()
->ordered()
->with(
array(
[
'scroll_id' => 'scroll_id_02',
'scroll' => '5m'
)
]
)
->andReturn(
array(
[
'_scroll_id' => 'scroll_id_03',
'hits' => array(
'hits' => array(
array()
)
)
)
'hits' => [
'hits' => [
[
'foo' => 'bar'
]
]
]
]
);

$mock_client->shouldReceive('scroll')
->once()
->ordered()
->with(
array(
[
'scroll_id' => 'scroll_id_03',
'scroll' => '5m'
)
]
)
->andReturn(
array(
[
'_scroll_id' => 'scroll_id_04',
'hits' => array(
)
)
'hits' => [
'hits' => []
]
]
);

$mock_client->shouldReceive('scroll')
->never()
->with(
array(
[
'scroll_id' => 'scroll_id_04',
'scroll' => '5m'
)
]
);

$mock_client->shouldReceive('clearScroll')
Expand All @@ -176,6 +162,6 @@ public function testWithScan()

$responses = new SearchResponseIterator($mock_client, $search_params);

$this->assertCount(2, $responses);
$this->assertCount(4, $responses);
}
}

1 comment on commit 72f3b15

@mariusbancos
Copy link
Contributor

@mariusbancos mariusbancos commented on 72f3b15 Jan 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@polyfractal This means that the new scan/scroll style is to receive at first search not only the scroll_id but also results based on the size? If this is the case then you should update also the documentation to reflect this changes.

Please sign in to comment.