/**
  * These values end up serialized into Avro which has strict typing
  * requirements. float !== int !== string.
  *
  * @param float $took Number of milliseconds the request took
  * @return array
  */
 private function buildLogContext($took)
 {
     $client = $this->connection->getClient();
     $query = $client->getLastRequest();
     $result = $client->getLastResponse();
     $params = $this->logContext;
     $this->logContext = array();
     $params += array('tookMs' => intval($took), 'source' => self::getExecutionContext(), 'executor' => self::getExecutionId(), 'identity' => self::generateIdentToken());
     if ($result) {
         $queryData = $query->getData();
         $resultData = $result->getData();
         $index = explode('/', $query->getPath());
         $params['index'] = $index[0];
         if (isset($resultData['took'])) {
             $elasticTook = $resultData['took'];
             $params['elasticTookMs'] = intval($elasticTook);
         }
         if (isset($resultData['hits']['total'])) {
             $params['hitsTotal'] = intval($resultData['hits']['total']);
         }
         if (isset($resultData['hits']['hits'])) {
             $num = count($resultData['hits']['hits']);
             $offset = isset($queryData['from']) ? $queryData['from'] : 0;
             $params['hitsReturned'] = $num;
             $params['hitsOffset'] = intval($offset);
         }
         if ($this->_isset($queryData, array('query', 'filtered', 'filter', 'terms', 'namespace'))) {
             $namespaces = $queryData['query']['filtered']['filter']['terms']['namespace'];
             $params['namespaces'] = array_map('intval', $namespaces);
         }
         if (isset($resultData['suggest']['suggest'][0]['options'][0]['text'])) {
             $params['suggestion'] = $resultData['suggest']['suggest'][0]['options'][0]['text'];
         }
     }
     if (self::$logContexts === null) {
         DeferredUpdates::addCallableUpdate(function () {
             ElasticsearchIntermediary::reportLogContexts();
         });
         self::$logContexts = array();
     }
     self::$logContexts[] = $params;
     return $params;
 }
 /**
  * Try to detect language using langdetect plugin
  * See: https://github.com/jprante/elasticsearch-langdetect
  * @param string $text
  * @return string|NULL Language name or null
  */
 public static function detectLanguage($text)
 {
     $client = Connection::getClient();
     try {
         $response = $client->request("_langdetect", Request::POST, $text);
     } catch (ResponseException $e) {
         // This happens when language detection is not configured
         LoggerFactory::getInstance('CirrusSearch')->warning("Could not connect to language detector: {exception}", array("exception" => $e->getMessage()));
         return null;
     }
     if ($response->isOk()) {
         $value = $response->getData();
         if ($value && !empty($value['languages'])) {
             $langs = $value['languages'];
             if (count($langs) == 1) {
                 // TODO: add minimal threshold
                 return $langs[0]['language'];
             }
             // FIXME: here I'm just winging it, should be something
             // that makes sense for multiple languages
             if (count($langs) == 2) {
                 if ($langs[0]['probability'] > 2 * $langs[1]['probability']) {
                     return $langs[0]['language'];
                 }
             }
         }
     }
     return null;
 }