From 3d4c16505b43a950946e34a3d07b2f97893ac6d1 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 21 Aug 2019 20:33:42 +0200 Subject: [PATCH] wip #2941 @6 --- app/Http/Controllers/AjaxController.php | 51 ++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index e9a2161..a192381 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -15,7 +15,8 @@ use Illuminate\Support\Facades\Validator; class AjaxController extends CubistFrontController { // Shared validation logic for dynamic CMS forms - protected function _validate_form(Request $request) { + protected function _validate_form(Request $request) + { $data = $request->all(); /** @var PageData $page */ $page = Page::find($data['page'])->getPageData(); @@ -96,7 +97,8 @@ class AjaxController extends CubistFrontController }); } - public function cart(Request $request) { + public function cart(Request $request) + { $request->validate([ 'action' => 'required|string', // add/update/delete @@ -111,7 +113,7 @@ class AjaxController extends CubistFrontController // Get existing session or an empty array $cart_items = $request->session()->get('cart_items', []); - switch($request->input('action')) { + switch ($request->input('action')) { case 'add': // If the item already exists in the cart, increment the quantity @@ -137,10 +139,11 @@ class AjaxController extends CubistFrontController return Product::getCartData(); } - public function request_quote(Request $request) { + public function request_quote(Request $request) + { // Array of product IDs => quantity - $cartData = (array) json_decode($request->input('cart_data')); + $cartData = (array)json_decode($request->input('cart_data')); // Validated form fields $validatedData = $this->_validate_form($request); @@ -170,4 +173,42 @@ class AjaxController extends CubistFrontController return []; } + + public function search(Request $request) + { + $limit = $request->limit ?? 50; + $index = config('cubist.internal_search_index'); + + // Set weight of each field + $fields = ['short_title' => 4, 'long_title' => 1, 'keywords' => 5, 'description' => 1, 'main' => 2]; + + $queryfields = []; + foreach ($fields as $field => $weight) { + $queryfields[] = $field . '^' . $weight; + $queryfields[] = $field . '.stemmed' . '^' . $weight; + } + + $res = \Elasticsearch::search(['index' => $index, + 'body' => [ + //'explain' => true, + 'from' => 0, + 'size' => $limit, + 'query' => [ + 'multi_match' => [ + 'query' => $request->q, + 'fields' => $queryfields, + ] + ] + ] + ]); + + $hits = []; + foreach ($res['hits']['hits'] as $hit) { + $hits[] = ['url' => $hit['_id'], + 'title' => $hit['_source']['short_title'] + ]; + } + + return $hits; + } } -- 2.39.5