$res_arr = $results['hits']['hits']; $brands_arr = []; foreach ($res_arr as $item) { $brands_arr[] = $item['_source']['brnd']; } $brands_arr = array_count_values($brands_arr); arsort($brands_arr); return $brands_arr; } public static function getAllRanges($results) { $res_arr = $results['hits']['hits']; $range = [0, 0, 0]; //range[0] contains no. of products between 0-10000 & same for range[1] & range[2] foreach ($res_arr as $item) { if (intval($item['_source']['price']) <= 10000) { $range[0] += 1; } else { if (intval($item['_source']['price']) <= 20000) { $range[1] += 1; } else { $range[2] += 1; } } } return $range; } } $esObject = new ElasticSearch($index, $type, $size); $results = $esObject->Query('', 0); print_r($results['aggregations']['price_ranges']['buckets']);
} if (isset($_POST['range'])) { $range = $_POST['range']; $arr = explode('-', $_POST['range']); $lower_bound = $arr[0]; $upper_bound = $arr[1]; if ($lower_bound == 20000) { $upper_bound += 100000; } $range_array = ["range" => ["least_price" => ["lte" => $upper_bound, "gt" => $lower_bound]]]; $filter[] = $range_array; } if (empty($filter)) { $filter = 0; } } if (isset($_POST['brand'])) { $brandFields = $_POST['brand']; } if (isset($_POST['range'])) { $arr = explode('-', $_POST['range']); $rangeField = $arr[0]; } $from = $cur_page * 10; $results = $esObject->Query($query, $filter, $from); $total = $results['hits']['total']; if ($total == 0) { header("Location: pagenotfound.html"); } $allBrands = ElasticSearch::getAllBrands($results); $range_to_filter = ElasticSearch::getAllRanges($results);