You are here

public function SearchApiElasticsearchConnectorStats::logStat in Elasticsearch Connector 7

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_connector_search_api/includes/elasticsearch_stats.inc \SearchApiElasticsearchConnectorStats::logStat()
  2. 7.2 modules/elasticsearch_connector_search_api/includes/elasticsearch_stats.inc \SearchApiElasticsearchConnectorStats::logStat()

Logging the statistics into the stats type in elasticsearch index.

Parameters

\Elasticsearch\Client $client:

array $access_log:

File

modules/elasticsearch_connector_search_api/includes/elasticsearch_stats.inc, line 150
@author nikolayignatov

Class

SearchApiElasticsearchConnectorStats

Code

public function logStat($response) {
  global $user;

  // Skip the log if the results matched.
  if (!empty($this->index->options['log_only_not_found']) && !empty($response['hits']['total'])) {
    return;
  }
  $client = $this->connector
    ->getConnectorObject();
  $ret = FALSE;
  $doc = $this->connector
    ->getIndexParam($this->index, TRUE);
  $doc['type'] .= self::TYPE_EXT;

  // Indexing document.
  try {
    if (!$client
      ->indices()
      ->existsType(array(
      'index' => $doc['index'],
      'type' => $doc['type'],
    ))) {
      $this
        ->createStatsType($doc);
    }
    $doc['body'] = array(
      'server_name' => $this->server->machine_name,
      'index_name' => $this->index->machine_name,
      'keywords_original' => $this
        ->getOriginalKeys(),
      'keywords' => $this
        ->parseKeys($this->query
        ->getOriginalKeys()),
      'filters' => $this
        ->getFilters(),
      'sort' => $this
        ->getSort(),
      'uid' => $user->uid,
      'results' => isset($response['hits']['total']) ? $response['hits']['total'] : 0,
      'username' => $user->uid > 0 ? $user->name : '',
      'timestamp' => time(),
      '_ttl' => $this->ttl,
    );
    $ret = $client
      ->index($doc);
    return $ret;
  } catch (Exception $e) {
    watchdog('elasticsearch_connector_statistics', $e
      ->getMessage(), array(), WATCHDOG_ERROR);
  }
}