Skip to content

Commit 0d2c9f4

Browse files
committed
add json parameter to the scroll function
1 parent 36235c6 commit 0d2c9f4

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ The package is developed and tested under Elasticsearch ``v6.*``. It should be a
987987
|:--------:|:--------:|:-----------------------:|:---------:|:-----------------------------------------------------:|
988988
|scroll_alive| | ``string`` |``5m`` | This specifies the life span of the scroll session. |
989989
|scroll_size| | ``integer`` |``500`` | This specifies the number of records retrieved from Elasticsearch per batch.|
990-
990+
|json | | ``boolean`` |``false`` | If encode the results from each batch in JSON format. Fetching data and put the data in PHP array can consume a lot of memory. This option can greatly reduce the memory usage.|
991991
* Output
992992
993993
``array``

src/LaravelElasticsearchQueryBuilder.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ public function get() {
748748
return $this;
749749
}
750750

751-
public function scroll($scroll_alive = '5m', $scroll_size = 500) {
751+
public function scroll($scroll_alive = '5m', $scroll_size = 500, $json = false) {
752752
$this->scroll_alive = $scroll_alive;
753753
$this->scroll_size = $scroll_size;
754754
$scroll_id = $this->get()->rawResults()['_scroll_id'];
@@ -760,9 +760,13 @@ public function scroll($scroll_alive = '5m', $scroll_size = 500) {
760760
]
761761
);
762762
if (count($response['hits']['hits']) > 0) {
763-
array_map(function ($value) use (&$results) {
764-
$results[] = $value['_source'];
765-
}, $response['hits']['hits']);
763+
if($json) {
764+
$results[] = json_encode(array_column($response['hits']['hits'], '_source'));
765+
} else {
766+
array_map(function ($value) use (&$results) {
767+
$results[] = $value['_source'];
768+
}, $response['hits']['hits']);
769+
}
766770
$scroll_id = $response['_scroll_id'];
767771
} else {
768772
break;

0 commit comments

Comments
 (0)