/** * Scan documents * * @param string $index */ protected function scanDocuments($index) { $params = ["search_type" => "scan", "scroll" => "50s", "size" => 50, "index" => $index, "body" => ["query" => ["match_all" => []]]]; $docs = $this->elasticsearch_client->search($params); $scroll_id = $docs['_scroll_id']; while (\true) { $response = $this->elasticsearch_client->scroll(["scroll_id" => $scroll_id, "scroll" => "30s"]); echo "<pre>"; print_r($response); echo "</pre>"; die; if (count($response['hits']['hits']) > 0) { $scroll_id = $response['_scroll_id']; } else { break; } } }
/** * Fetches every "page" after the first one using the latest "scroll_id" * * @return void * @see Iterator::next() */ public function next() { $this->currentKey++; $this->currentScrolledResponse = $this->client->scroll(array('scroll_id' => $this->scrollId, 'scroll' => $this->scrollTtl)); $this->scrollId = $this->currentScrolledResponse['_scroll_id']; }
/** * Search functionality exposed for use through the service provider. * It uses scan search type and scroll API to retrieve large numbers of documents. * * @param array $indices The list of indices to include in the query. Defaults to searching all indices. * @param array $types The list of types to include in the query. Defaults to searching all types. * @param array $query The query to run against the specified indexes and types. * * @return array Like [ * 'total' => (integer), Number of documents returned by the query, * 'hits' => [ * result set * ], * ]; */ public function search(array $indices, array $types, array $query = []) { if (empty($indices)) { $indices = ['_all']; } if ($types === null) { $types = []; } $req = ['search_type' => 'scan', 'scroll' => '1m', 'index' => implode(',', $indices), 'type' => implode(',', $types), 'body' => $query]; $response = $this->client->search($req); $scrollId = $response['_scroll_id']; $totalResults['total'] = $response['hits']['total']; $totalResults['hits'] = []; do { $totalResults['hits'] = array_merge($totalResults['hits'], $response['hits']['hits']); $response = $this->client->scroll(['scroll_id' => $scrollId, 'scroll' => '1m']); $results = $response['hits']['hits']; $scrollId = $response['_scroll_id']; } while (count($results) > 0); return $totalResults; }
/** * Fetches every "page" after the first one using the lastest "scroll_id" * * @return void * @see Iterator::next() */ public function next() { $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']; }