diff --git a/src/controllers.php b/src/controllers.php index 02c2a73..b103562 100644 --- a/src/controllers.php +++ b/src/controllers.php @@ -17,16 +17,17 @@ ->bind('homepage') ; -$app->get('/libraries', function () { - return "Library index page"; +$app->get('/library/{id}', function ($id) use ($app) { + return $app['twig']->render('library.html.twig', array('id' => $id)); }); $app->get('/library_api/{id}', function ($id) use ($app) { $client = $app['elasticsearch']; $params = [ - 'index' => 'publiclib', + 'index' => 'publiclibdata', 'type' => 'logs', + 'size' => 500, 'body' => [ 'query' => [ 'match' => [ @@ -41,6 +42,27 @@ }) ->value("id", "*"); // set a default value +$app->get('/library_year/{id}', function ($id) use ($app) { + + $client = $app['elasticsearch']; + $params = [ + 'index' => 'publiclibdata', + 'type' => 'logs', + 'size' => 500, + 'body' => [ + 'query' => [ + 'match' => [ + 'Year' => trim($id) + ] + ] + ] + ]; + + $response = $client->search($params); + return new JsonResponse($response); +}) +->value("id", "2016"); // set a default value + $app->error(function (\Exception $e, Request $request, $code) use ($app) { if ($app['debug']) { return; @@ -56,3 +78,46 @@ return new Response($app['twig']->resolveTemplate($templates)->render(array('code' => $code)), $code); }); + +$app->get('/library_api_autocomplete/{id}', function ($id) use ($app) { + // example query http://localhost:8282/library_api_autocomplete/* + // This one uses the new, fixed index + // Facets come back in the Buckets object + if (empty($id)) { + $id = "*"; + } + $client = $app['elasticsearch']; + $params = [ + 'index' => 'publiclibdata', + 'type' => 'logs', + 'from' => 0, + 'size' => 100, + "_source" => [ + "Location" + ], + 'body' => [ + 'query' => [ + "query_string" => [ + "default_field" => "Location", + "query" => "{$id}" + ] + ], + "aggregations" => [ + "libraries" => [ + "terms" => [ + "field" => "Location.keyword", + "size" => 500 + ] + ], + "Years" => [ + "terms" => [ + "field" => "Year.keyword" + ] + ], + ] + ] + ]; + $response = $client->search($params); + return new JsonResponse($response); +}) +->value("id", "*"); // set a default value \ No newline at end of file