/**
  * perform the current bulk request
  *
  * @return void
  */
 public function flush()
 {
     if (count($this->currentBulkRequest) === 0) {
         return;
     }
     $content = '';
     foreach ($this->currentBulkRequest as $bulkRequestTuple) {
         $tupleAsJson = '';
         foreach ($bulkRequestTuple as $bulkRequestItem) {
             $itemAsJson = json_encode($bulkRequestItem);
             if ($itemAsJson === FALSE) {
                 $this->logger->log('Indexing Error: Bulk request item could not be encoded as JSON - ' . json_last_error_msg(), LOG_ERR, $bulkRequestItem);
                 continue 2;
             }
             $tupleAsJson .= $itemAsJson . chr(10);
         }
         $content .= $tupleAsJson;
     }
     if ($content !== '') {
         $responseAsLines = $this->getIndex()->request('POST', '/_bulk', array(), $content)->getOriginalResponse()->getContent();
         foreach (explode("\n", $responseAsLines) as $responseLine) {
             $response = json_decode($responseLine);
             if (!is_object($response) || isset($response->errors) && $response->errors !== FALSE) {
                 $this->logger->log('Indexing Error: ' . $responseLine, LOG_ERR);
             }
         }
     }
     $this->currentBulkRequest = array();
 }
 /**
  * @param array $hits
  * @return array Array of Node objects
  */
 protected function convertHitsToNodes(array $hits)
 {
     $nodes = [];
     $elasticSearchHitPerNode = [];
     /**
      * TODO: This code below is not fully correct yet:
      *
      * We always fetch $limit * (numerOfWorkspaces) records; so that we find a node:
      * - *once* if it is only in live workspace and matches the query
      * - *once* if it is only in user workspace and matches the query
      * - *twice* if it is in both workspaces and matches the query *both times*. In this case we filter the duplicate record.
      * - *once* if it is in the live workspace and has been DELETED in the user workspace (STILL WRONG)
      * - *once* if it is in the live workspace and has been MODIFIED to NOT MATCH THE QUERY ANYMORE in user workspace (STILL WRONG)
      *
      * If we want to fix this cleanly, we'd need to do an *additional query* in order to filter all nodes from a non-user workspace
      * which *do exist in the user workspace but do NOT match the current query*. This has to be done somehow "recursively"; and later
      * we might be able to use https://github.com/elasticsearch/elasticsearch/issues/3300 as soon as it is merged.
      */
     foreach ($hits['hits'] as $hit) {
         $nodePath = current($hit['fields']['__path']);
         $node = $this->contextNode->getNode($nodePath);
         if ($node instanceof NodeInterface && !isset($nodes[$node->getIdentifier()])) {
             $nodes[$node->getIdentifier()] = $node;
             $elasticSearchHitPerNode[$node->getIdentifier()] = $hit;
             if ($this->limit > 0 && count($nodes) >= $this->limit) {
                 break;
             }
         }
     }
     if ($this->logThisQuery === true) {
         $this->logger->log('Returned nodes (' . $this->logMessage . '): ' . count($nodes), LOG_DEBUG);
     }
     $this->elasticSearchHitsIndexedByNodeFromLastRequest = $elasticSearchHitPerNode;
     return array_values($nodes);
 }
 /**
  * Clean up old indexes (i.e. all but the current one)
  *
  * @return void
  */
 public function cleanupCommand()
 {
     try {
         $indicesToBeRemoved = $this->nodeIndexer->removeOldIndices();
         if (count($indicesToBeRemoved) > 0) {
             foreach ($indicesToBeRemoved as $indexToBeRemoved) {
                 $this->logger->log('Removing old index ' . $indexToBeRemoved);
             }
         } else {
             $this->logger->log('Nothing to remove.');
         }
     } catch (\Flowpack\ElasticSearch\Transfer\Exception\ApiException $exception) {
         $response = json_decode($exception->getResponse());
         $this->logger->log(sprintf('Nothing removed. ElasticSearch responded with status %s, saying "%s"', $response->status, $response->error));
     }
 }
 /**
  * Return the total number of hits for the query.
  *
  * @return integer
  * @api
  */
 public function count()
 {
     $timeBefore = microtime(TRUE);
     $request = $this->request;
     foreach ($this->unsupportedFieldsInCountRequest as $field) {
         if (isset($request[$field])) {
             unset($request[$field]);
         }
     }
     $response = $this->elasticSearchClient->getIndex()->request('GET', '/_count', array(), json_encode($request));
     $timeAfterwards = microtime(TRUE);
     $treatedContent = $response->getTreatedContent();
     $count = $treatedContent['count'];
     if ($this->logThisQuery === TRUE) {
         $this->logger->log('Query Log (' . $this->logMessage . '): ' . json_encode($this->request) . ' -- execution time: ' . ($timeAfterwards - $timeBefore) * 1000 . ' ms -- Total Results: ' . $count, LOG_DEBUG);
     }
     return $count;
 }