You are here

public function SearchApiElasticsearchConnector::getDateGranularity in Elasticsearch Connector 7

Same name and namespace in other branches
  1. 7.2 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::getDateGranularity()

Helper function to return date gap.

1 call to SearchApiElasticsearchConnector::getDateGranularity()
SearchApiElasticsearchConnector::getDateFacetInterval in modules/elasticsearch_connector_search_api/service.inc
Helper function which add params to date facets.

File

modules/elasticsearch_connector_search_api/service.inc, line 1861
Provides a Elasticsearch-based service class for the Search API using Elasticsearch Connector module.

Class

SearchApiElasticsearchConnector
Search service class.

Code

public function getDateGranularity($adapter, $facet_id) {

  // Date gaps.
  $gap_weight = array(
    'YEAR' => 2,
    'MONTH' => 1,
    'DAY' => 0,
  );
  $gaps = array();
  $date_gap = 'YEAR';

  // Get the date granularity.
  if (isset($adapter)) {

    // Get the current date gap from the active date filters.
    $active_items = $adapter
      ->getActiveItems(array(
      'name' => $facet_id,
    ));
    if (!empty($active_items)) {
      foreach ($active_items as $active_item) {
        $value = $active_item['value'];
        if (strpos($value, ' TO ') > 0) {
          list($date_min, $date_max) = explode(' TO ', str_replace(array(
            '[',
            ']',
          ), '', $value), 2);
          $gap = self::getDateGap($date_min, $date_max, FALSE);
          if (isset($gap_weight[$gap])) {
            $gaps[] = $gap_weight[$gap];
          }
        }
      }
      if (!empty($gaps)) {

        // Minimum gap.
        $date_gap = array_search(min($gaps), $gap_weight);
      }
    }
  }
  return $date_gap;
}