tntsearch
tntsearch copied to clipboard
How to sort search results?
I have a table products, and table categories. Product belongs to category. How to create a weighted search? Does this package allows this? Eg. Categories: Tractors, Cultivators Product:
-
id: 1
-
title: John Deer Tractor for sale
-
description: Someone describes his awesome tractor.
-
cateogry: tractors
-
id: 2
-
title: John deer
-
category:cultivators
User search for word 'tractor'. Expecting to find all products that are in category tractors. User types 'john deer tractor' Expecting to get all products from category tractor, but on top of page shall be those with john deer in title. followed by rest of tractors, but not showing id 2, which belongs to category cultivators. User typed 'tractor', so no cultivators of same brand.
So, need the way to instruct search what takes priority on search. Other question is if i type Plant Catalyst, will i get all the products where plant catalyst words are next to each other first? followed by results where one or another is found?
TNTSearch doesn't support field weighting, neither sorting, as this would affect the relevance of results. And in most cases, this is not needed.
From your description, to provide the best user experience, you would need a faceted
search and navigation.
To understand a faceted search, you can go to eBay or a similar site and type jaguar
into their search. You'll get all kinds of results, but on the left side, you'll find a navigation that has categories like cars, clothing, etc. Now, if you select one of those categories, you will get search results only among this category.
The first thing you would do is creating an index that holds all of the products.
$indexer = $tnt->createIndex('products.index');
$indexer->query('SELECT id, title, decription FROM products;');
$indexer->run();
This index will be queried when the first search is made, and the user hasn't specified a category. When you display results, you display a navigation on the left side of the screen with categories. The user is now able to narrow down a category, and a new search is performed.
For all the categories, you'll have an index that stores only products within this category.
$categories = ['tractors', 'curltivators'];
foreach($categories as $category) {
$indexer = $tnt->createIndex("products-{$category}.index");
$indexer->query("SELECT id, title, decription FROM products WHERE category = $category;");
$indexer->run();
}
Now, when the user knows the category, you can query the required index which will hold relevant results.
It'll also be essential to know what the users are searching for, so you might checkout TNT Analytics, but you don't have to