PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE `ev_keyword` ( `keyword_id` int(11) UNSIGNED NOT NULL, `type` enum('host','path') NOT NULL, `route` enum('location/location','location/search','business/search','vehicle/search','product/product') NOT NULL, `key` varchar(191) NOT NULL, `value` varchar(191) NOT NULL, `name` varchar(255) NOT NULL, `keyword` varchar(191) NOT NULL, `group` varchar(191) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ALTER TABLE `ev_keyword` ADD PRIMARY KEY (`keyword_id`), ADD KEY `type` (`type`), ADD KEY `route` (`route`), ADD KEY `key` (`key`), ADD KEY `value` (`value`), ADD FULLTEXT `keyword` (`keyword`); ALTER TABLE `ev_keyword` MODIFY `keyword_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18; INSERT INTO `ev_keyword` (`keyword_id`, `type`, `route`, `key`, `value`, `name`, `keyword`, `group`) VALUES (1, 'path', 'vehicle/search', 'condition', '1', 'Novo 0km', 'novo-0km', '0'), (2, 'path', 'vehicle/search', 'condition', '2', 'Seminovo e usado', 'seminovo-e-usado', '0'), (3, 'path', 'vehicle/search', 'year', 'min', 'Ano de', 'ano-de', '0'), (4, 'path', 'vehicle/search', 'year', 'max', 'Ano até', 'ano-ate', '0'), (5, 'path', 'vehicle/search', 'price', 'min', 'Preço de', 'preco-de', '0'), (6, 'path', 'vehicle/search', 'price', 'max', 'Preço até', 'preco-ate', '0'), (7, 'path', 'vehicle/search', 'mileage', 'min', 'Quilometragem de', 'quilometragem-de', '0'), (8, 'path', 'vehicle/search', 'mileage', 'max', 'Quilometragem até', 'quilometragem-ate', '0'), (9, 'path', 'vehicle/search', 'plate', '1', 'Final 1 e 2', 'final-da-placa-1-e-2', '0'), (10, 'path', 'vehicle/search', 'plate', '2', 'Final 3 e 4', 'final-da-placa-3-e-4', '0'), (11, 'path', 'vehicle/search', 'plate', '3', 'Final 5 e 6', 'final-da-placa-5-e-6', '0'), (12, 'path', 'vehicle/search', 'plate', '4', 'Final 7 e 8', 'final-da-placa-7-e-8', '0'), (13, 'path', 'vehicle/search', 'plate', '5', 'Final 9 e 0', 'final-da-placa-9-e-0', '0'), (14, 'path', 'vehicle/search', 'advertiser', '1', 'Particular', 'particular', '0'), (15, 'path', 'vehicle/search', 'advertiser', '2', 'Revenda', 'revenda', '0'), (16, 'path', 'vehicle/search', 'advertisement', '1', 'Com fotos', 'com-fotos', '1'), (17, 'path', 'vehicle/search', 'advertisement', '2', 'Com preços expostos', 'com-precos-expostos', '1'), #(18, 'path', 'product/product', 'product_id', '1', 'ID', 'smartphone-em-{location_keyword}', '0'), #(19, 'path', 'product/product', 'product_id', '2', 'ID', 'ventilador-em-{location_keyword}', '0'), (18, 'path', 'product/product', 'product_id', '1', 'ID', 'smartphone', '0'), (19, 'path', 'product/product', 'product_id', '2', 'ID', 'ventilador', '0'), (20, 'path', 'product/product', 'product_id', '3', 'ID', 'geladeira', '0'), (21, 'path', 'product/product', 'product_id', '4', 'ID', 'geladeira-frost-free', '0'), (22, 'path', 'location/location', 'location_id', '1', 'ID', 'sao-paulo-sp', '0'), (23, 'path', 'location/location', 'location_id', '2', 'ID', 'americana-sp', '0'), (24, 'path', 'location/location', 'location_id', '3', 'ID', 'campinas-sp', '0'), (25, 'path', 'location/location', 'location_id', '4', 'ID', 'sao-pedro-sp', '0'), (26, 'path', 'product/product', 'product_id', '5', 'ID', 'geladeira-mini', '0'), (27, 'path', 'product/product', 'product_id', '6', 'ID', 'geladeira-minis', '0'), (28, 'path', 'product/product', 'product_id', '7', 'ID', 'geladeira-duplex', '0'); #SELECT * FROM `ev_keyword`; #SELECT * FROM `ev_keyword` WHERE MATCH (`keyword`) AGAINST ('smartphone-em-sao-paulo-sp' IN NATURAL LANGUAGE MODE); #SELECT * FROM `ev_keyword` WHERE MATCH (`keyword`) AGAINST ('geladeira-em-sao-paulo-sp' IN NATURAL LANGUAGE MODE);
Copy Clear
Copy Format Clear
<?php //use Carbon\Carbon; $start_time = microtime(true); //$route = 'geladeira-em-sao-paulo-sp'; //$route = 'geladeira-em-campinas-sp'; //$route = 'geladeira-mini-em-campinas-sp'; //$route = 'geladeira-mini-em-sao-paulo-sp'; //$route = 'geladeira-em-americana-sp'; //$route = 'geladeira-frost-free-em-americana-sp'; //$route = 'geladeira-frost-free-em-sao-paulo-sp'; //$route = 'geladeira-frost-free-em-sao-pedro-sp'; //$route = 'ventilador-em-campinas-sp'; //$item = 'geladeira'; //$item = 'geladeira-minis'; $item = 'geladeira-mini'; $location = 'sao-pedro-sp'; //$location_sep = '-em-'; $route_template = '{item}-em-{location}'; //$route_template = "{item}{$location_sep}{location}"; $location_sep = str_replace(['{item}', '{location}'], '', $route_template); //echo "\n-- Sep --\n{$location_sep}\n\n"; $route = str_replace(['{item}', '{location}'], [$item, $location], $route_template); $terms = explode($location_sep, $route); //$terms = implode(' ', explode($location_sep, $route)); //print_r($terms); echo "\n-- Route --\n{$location_sep}\n{$route_template}\n{$route}\n\n"; echo "\n-- Terms --\n"; print_r($terms); //$query = "SELECT * FROM `ev_keyword` WHERE MATCH (`keyword`) AGAINST ('geladeira-em-sao-paulo-sp' IN NATURAL LANGUAGE MODE);"; $query = "SELECT * FROM `ev_keyword` WHERE MATCH (`keyword`) AGAINST ('{$route}' IN NATURAL LANGUAGE MODE);"; //$query = "SELECT * FROM `ev_keyword` WHERE MATCH (`keyword`) AGAINST ('{$route}' IN NATURAL LANGUAGE MODE) ORDER BY `keyword` DESC;"; //$query = "SELECT * FROM `ev_keyword` WHERE MATCH (`keyword`) AGAINST ('{$route}' IN NATURAL LANGUAGE MODE) ORDER BY `keyword` ASC;"; //$query = "SELECT * FROM `ev_keyword` WHERE MATCH (`keyword`) AGAINST ('{$terms}' IN BOOLEAN MODE) ORDER BY `keyword` ASC;"; echo "\n-- Query --\n{$query}\n\n"; // get DB version using PDO /*$stmt = $pdo->prepare($query); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); printf('DB version (PDO): %s ' . PHP_EOL, $row['version']);*/ // Run query using mysqli $result = $mysqli->query($query); $rows = $result->fetch_all(MYSQLI_ASSOC); echo "-- Found --\n"; $attribute = []; foreach ($rows as $row) { //printf("%s (%s)\n", $row["route"], $row["keyword"]); echo "{$row["route"]} => {$row["keyword"]}\n"; /*$attribute[] = [ 'key' => $row["key"], 'value' => $row["value"], 'keyword' => $row["keyword"] ];*/ //if (str_contains($route, $row["keyword"])) { //if (in_array($row["keyword"], $terms, true)) { if (!isset($attribute[$row["key"]]) && in_array($row["keyword"], $terms, true)) { //if (str_contains($route, $row["keyword"]) && !isset($attribute[$row["key"]])) { $attribute[$row["key"]] = $row["value"]; //$attribute[$row["key"]] = $row["keyword"]; $attribute[$row["route"]] = $row["keyword"]; } } echo "\n-- Match --\n"; print_r($attribute); //echo sprintf('<pre>%s</pre>', print_r($attribute, true)); $end_time = microtime(true); echo "\n-- Time --\n".($end_time - $start_time);
Show:  
Copy Clear