Jump to content

Sorting found data by "most matching" relevance


LAPS
 Share

Recommended Posts

Hello there,

I've a serie of pages manually sorted in the admin having the following titles:

Black color
Black dark color
Black light color
White color
White dark color
White light color
Green color
Green dark color
Green light color
Blue color
Blue dark color
Blue light color
Red color
Red dark color
Red light color
Color
Dark Color
Light color

And (for now) I've a search form to search for the above pages (template is color-page) that use the following code:

$search_result = $pages->find("template=color-page, title={$search_term}, sort=sort");

When I perform the search with $search_term equal to "Dark Color" then I get the result sorted as follow (and as expected because of the sort=sort)

Black dark color
White dark color
Green dark color
Blue dark color
Red dark color
Dark Color

However, I would like to sort the $search_result by "relevance" to get the found pages sorted as follow:

Dark Color
Black dark color
Blue dark color
Green dark color
Red dark color
White dark color

That is, "Dark color" is the first item because it is the "most matching".

From the official PW documentation about sorting I know that if you don't specify your own sort=property the results are sorted according to MySQL's text searching relevance but this MySQL feature seems do not always work and I would like to make sure that found pages are sorted by "most matching" as in the example above.

How can I make that in PW?

Link to comment
Share on other sites

On 11/1/2020 at 8:53 AM, LAPS said:

However, I would like to sort the $search_result by "relevance" to get the found pages sorted as follow:


Dark Color
Black dark color
Blue dark color
Green dark color
Red dark color
White dark color

 

 

Here is example to get that with 2 steps:

// search term: "Dark color" or "dark color"
// 1: get all starts with
$items = $pages->find("template=color-page, title%^='dark color'");
// 2: get all EXCEPT items from step 1 AND sort them by title
$items->import($pages->find("template=color-page, id!=$items, title%='dark color', sort=title"));

Also there are and other options to get the same (sort array on PHP side, or do direct sql queries using PW api to get "order-by best match").

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...