简体   繁体   中英

How to make my product search more efficient?

I am using MySQL as back end for an eCommerce project.In first screen i am showing result while user searches something, and with these results user can select any one result and base on selection, user gets all the products related to selected result.

Example : Screen 1(From Search bar): user search for "tropicana" then the result set will be :
1st: tropicana mix fruit juice in juices(category)
2nd: tropicana apple juice in juices(category)
3rd: tropicana orange juice in juices(category)
4th: tropicana pineapple juice in juices(category)
5th: tropicana guava juice in juices(category) and all other tropicana related products irrespective of any category

Screen 2(After selecting "tropicana pineapple fruit juice") : user gets result sets base on the selected product name and its category (i have used pagination in this screen):

1st: tropicana mix fruit juice with all basic details
2nd: tropicana apple juice in with all basic details
3rd: tropicana orange juice with all basic details
4th: tropicana pineapple juice with all basic details
5th: tropicana guava juice with all basic details

But what i want is that user should see the selected product details first and then all other related products :

Example :
1st: tropicana pineapple juice with all basic details
2nd: tropicana apple juice in with all basic details
3rd: tropicana orange juice with all basic details
4th: tropicana mix fruit juice with all basic details
5th: tropicana guava juice with all basic details

I have used this sql Query:

("select p.id,p.product_name,p.rating,p.rating_count,p.product_mrp from product p
    INNER JOIN product_category_mapping pcm ON p.id = pcm.product_id
    where (p.product_name like concat('",in_product_name,"','%') or p.product_name like concat('%','",in_product_name,"','%')) 
    order by p.",in_order_by," ",in_order_type," limit ", var_offset,",",var_limit);

can any one suggests any good option for this?

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,product VARCHAR(255) NOT NULL 
);

INSERT INTO my_table (product) VALUES
('tropicana pineapple juice'),
('tropicana apple juice'),
('tropicana orange juice'),
('tropicana mix fruit juice'),
('tropicana guava juice'),
('jolt pineapple juice'),
('jolt apple juice'),
('jolt orange juice'),
('jolt mix fruit juice'),
('jolt guava juice'),
('duff pineapple juice'),
('duff apple juice'),
('duff orange juice'),
('duff mix fruit juice'),
('duff guava juice'),
('acme pineapple juice'),
('acme apple juice'),
('acme orange juice'),
('acme mix fruit juice'),
('acme guava juice');

ALTER TABLE my_table ADD FULLTEXT(product);

SELECT *
     , MATCH(product) AGAINST('tropicana pineapple') x 
  FROM my_table;
+----+---------------------------+-----------------+
| id | product                   | x               |
+----+---------------------------+-----------------+
|  1 | tropicana pineapple juice | 2.4020363687754 |
|  2 | tropicana apple juice     | 1.0619741682407 |
|  3 | tropicana orange juice    | 1.0619741682407 |
|  4 | tropicana mix fruit juice | 1.0619741682407 |
|  5 | tropicana guava juice     | 1.0619741682407 |
|  6 | jolt pineapple juice      | 1.3400622005347 |
|  7 | jolt apple juice          |               0 |
|  8 | jolt orange juice         |               0 |
|  9 | jolt mix fruit juice      |               0 |
| 10 | jolt guava juice          |               0 |
| 11 | duff pineapple juice      | 1.3400622005347 |
| 12 | duff apple juice          |               0 |
| 13 | duff orange juice         |               0 |
| 14 | duff mix fruit juice      |               0 |
| 15 | duff guava juice          |               0 |
| 16 | acme pineapple juice      | 1.3400622005347 |
| 17 | acme apple juice          |               0 |
| 18 | acme orange juice         |               0 |
| 19 | acme mix fruit juice      |               0 |
| 20 | acme guava juice          |               0 |
+----+---------------------------+-----------------+
20 rows in set (0.00 sec)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM