Jump to content

Full text search that gives height weight to mathces in the title than in the body?


Anders
 Share

Recommended Posts

I want to allow full text search on my site. There is a very nice solution that comes right out of the box:

$selector = "title|body~=$q, limit=50"; 

This works, but to make it even better I would want to give higher weight to pages where the search term occurs in the title, than if it just occurs in the body. After all, a page with the title "Wine from France" is probably the best match for the search "france wine". How do I accomplish this in ProcessWire?

I can see three possible paths, but I am not very fond of any of them:

  1. Do a direct SQL query, circumventing the API, along these lines. But I would prefer to abstract away the database layout if at all possible.
  2. Use something like ElasticSearch, but to be honest that would be to complicated to set up and maintain in the long run.
  3. Make multiple lookups, first for matches in the title, then for matches in the body, and merge and sort in PHP. My suspicion is that this would get complicated quite quickly. For instance, how do you deal with a page that has two of the three search terms in the title and the third in the body?

Is there a magic option four I should look into? Or are any of the above options better than the others? Any input is welcome!

Link to comment
Share on other sites

On 2/24/2020 at 8:40 AM, Anders said:

Is there a magic option four I should look into?

There's no magic option - you have to code your own search in the way that suits you.

If the number of search results is not huge and pagination is not required then you can get all the results where any field matches, and then divide off the pages that have matches in the title field, rendering those results before the others.

Otherwise you might want to use an SQL query, perhaps returning just an array of page IDs which you could then slice according to the pagination number and load to a PageArray via $pages->getById().

  • Thanks 1
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

×
×
  • Create New...