From 3eebbe44bdf54046ee870b294abc7087b501faf1 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Sun, 12 Feb 2017 13:55:05 -0500 Subject: [PATCH 1/2] New Route with faceting If called via http://localhost:8282/library_api_autocomplete/* it returns all libraries, can be used to partial matching, individual facets (unique values and their counts com back inside the buckets object) --- src/controllers.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/controllers.php b/src/controllers.php index 02c2a73..c510573 100644 --- a/src/controllers.php +++ b/src/controllers.php @@ -41,6 +41,48 @@ }) ->value("id", "*"); // set a default value +$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 $app->error(function (\Exception $e, Request $request, $code) use ($app) { if ($app['debug']) { return; From 75a7cfda271b8f8897ff961199dbac6ff278a6be Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Sun, 12 Feb 2017 14:00:19 -0500 Subject: [PATCH 2/2] should fix conflicts --- src/controllers.php | 61 +++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/src/controllers.php b/src/controllers.php index c510573..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,43 @@ }) ->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; + } + + // 404.html, or 40x.html, or 4xx.html, or error.html + $templates = array( + 'errors/'.$code.'.html.twig', + 'errors/'.substr($code, 0, 2).'x.html.twig', + 'errors/'.substr($code, 0, 1).'xx.html.twig', + 'errors/default.html.twig', + ); + + 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 @@ -82,19 +120,4 @@ $response = $client->search($params); return new JsonResponse($response); }) -->value("id", "*"); // set a default value -$app->error(function (\Exception $e, Request $request, $code) use ($app) { - if ($app['debug']) { - return; - } - - // 404.html, or 40x.html, or 4xx.html, or error.html - $templates = array( - 'errors/'.$code.'.html.twig', - 'errors/'.substr($code, 0, 2).'x.html.twig', - 'errors/'.substr($code, 0, 1).'xx.html.twig', - 'errors/default.html.twig', - ); - - return new Response($app['twig']->resolveTemplate($templates)->render(array('code' => $code)), $code); -}); +->value("id", "*"); // set a default value \ No newline at end of file