/**
  * Get statistics for each provider with new providers table and through ElasticSearch
  * 
  * @return Array list of providers with statistics
  */
 public static function statistics()
 {
     $providers = Utils::getProvidersES();
     foreach ($providers as &$provider) {
         $query = ['query' => ['match' => ['providerId' => $provider['_source']['id']]]];
         $provider['collections'] = ElasticSearch::countHits($query, 'resource', 'collection');
         $provider['datasets'] = ElasticSearch::countHits($query, 'resource', 'dataset');
         $provider['databases'] = ElasticSearch::countHits($query, 'resource', 'database');
         $provider['gis'] = ElasticSearch::countHits($query, 'resource', 'gis');
         $provider['textualDocuments'] = ElasticSearch::countHits($query, 'resource', 'textualDocument');
     }
     return $providers;
 }
 /**
  * Performs a faceted search depending on the GET-values
  * Eg ?q=dig&keyword=england does a free text search for dig in
  * resources where the keyword england exists
  *
  * @return View rendered pagination for search results
  */
 public function search()
 {
     $query = ['aggregations' => Config::get('app.elastic_search_aggregations')];
     // add geogrid aggregation
     $ghp = Request::has('ghp') ? Request::input('ghp') : 2;
     $query['aggregations']['geogrid'] = ['geohash_grid' => ['field' => 'spatial.location', 'precision' => intval($ghp)]];
     $startYear = Request::has("start") ? intval(Request::get("start")) : 00;
     $endYear = Request::has("end") ? intval(Request::get("end")) : 2000;
     // $query['aggregations']['range_buckets']= Resource::prepareRangeBucketsAggregation($startYear,$endYear,6);
     $q = ['match_all' => []];
     if (Request::has('q')) {
         $field_groups = Config::get('app.elastic_search_field_groups');
         if (Request::has('fields') && array_key_exists(Request::get('fields'), $field_groups)) {
             foreach ($field_groups[Request::get('fields')] as $field) {
                 $q = ['match' => [$field => Request::get('q')]];
                 $query['query']['bool']['should'][] = $q;
             }
         } else {
             $q = ['query_string' => ['query' => Request::get('q')]];
             $query['query']['bool']['must'][] = $q;
         }
     } else {
         $query['query']['bool']['must'][] = ['query_string' => ['query' => '*']];
     }
     foreach ($query['aggregations'] as $key => $aggregation) {
         if (Request::has($key)) {
             $values = Utils::getArgumentValues($key);
             $field = $aggregation['terms']['field'];
             foreach ($values as $value) {
                 $fieldQuery = [];
                 $fieldQuery[$field] = $value;
                 $query['query']['bool']['must'][] = ['match' => $fieldQuery];
             }
         }
     }
     // TODO: refactor so that ES service takes care of bbox parsing
     if (Request::has('bbox')) {
         $bbox = explode(',', Request::input('bbox'));
         $query['query'] = ['filtered' => ['query' => $query['query'], 'filter' => ['geo_bounding_box' => ['spatial.location' => ['top_left' => ['lat' => floatval($bbox[3]), 'lon' => floatval($bbox[0])], 'bottom_right' => ['lat' => floatval($bbox[1]), 'lon' => floatval($bbox[2])]]]]]];
     }
     $hits = Resource::search($query, 'resource');
     if (Request::wantsJson()) {
         return response()->json($hits);
     } else {
         return view('resource.search')->with('type', null)->with('aggregations', $query['aggregations'])->with('translateAggregations', Config::get('app.translate_aggregations'))->with('hits', $hits);
     }
 }