Skip Navigation

[Resolved] Sorting by a weighted score

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by collinC-2 4 years ago.

Assisted by: Shane.

Author
Posts
#1825313

I have a client that wants to use a weighted score to sort their results by. Here's a simplified example of how it would work.

The custom post type has two multiple choice (ex. checkbox) custom fields like this:

Custom Field #1
Value #1
Value #2

Custom Field #2
Value #3
Value #4

The weighted score will be based on the number of values selected during the query. So if someone searched for Value #1, any record that had Value #1 would score a 100% and those that didn't would score a 0%.

If a user search for Custom Field#1 = Value #1 and Custom Field #2 = Value #3, any values that matched both Custom Field#1 = Value #1 and Custom Field #2 = Value #3 would score a 100%. Any value that matched either Custom Field#1 = Value #1 or Custom Field #2 = Value #3 would score a 50% (1 match divided by 2 search parameters). Those entries that matched neither would score a 0%.

If a user search for Custom Field#1 = Value #1, Custom Field#1 = Value #2, and Custom Field #2 = Value #3, any values that matched both Custom Field#1 = Value #1 and Custom Field#1 = Value #2 and Custom Field #2 = Value #3 would score a 100%. Any value that matched two of the query parameters like Custom Field#1 = Value #1 and Custom Field #2 = Value #3 would score a 66% (2 matches divided by 3 search parameters). Any value that matched one value like Custom Field#1 = Value #1, Custom Field#1 = Value #2, or Custom Field #2 = Value #3 would score a 33% (1 match divided by 3 search parameters). Those entries that matched neither would score a 0%.

Once the score has been calculated, I need to have the ability to sort the results by the calculated weight. Is there a way to do this with Toolset?

Please let me know if I can provide more information on what I'm trying to do.

Thanks,

Collin Condray

#1826395

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Collin,

Thank you for getting in touch.

Taking a look at this issue it seems a bit complex. The first thing is that views can only sort by pre-existing values that are stored on the posts.

Is the intention to generate the weighted score dynamically? Then store this value on the post themselves?

I can see an issue with this as per each search from different users the value will need to change based on each individual search, so each search will generate a weighted score from a previous user.

This is what I understand from your query, however its definitely something that will require extensive custom coding in order to achieve.

Thanks,
Shane

#1827483

Shane,

No, the score will be calculated based on how many search elements match the results from an OR search. The more matches the record has, the higher the score. This can change depending on how many query options are selected.

If you can think of a way around this, can you point me to something specific in the docs I can take a look at to get me started?

Thanks,

Collin

#1827663

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Collin,

Thanks for the clarity. Actually there isn't any specific document that we have that can help with this one.

You will definitely need some form of custom code that can log the results and the frequency at which these results appear based on what was searched.

Beyond this there isn't much else I can do. Perhaps what you can do is to create a custom shortcode that will log the search results and pick up from the url what was being searched for.

Thanks
Shane

#1829743

Shane,

I found this https://stackoverflow.com/posts/14879153/revisions which is similar to what I want to do. It does the original query and calculates the value for each result. Then it resets the query and associated the calculated result with the new query. Is there a way to do this with Toolset?

Thanks,

Collin

#1832017

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Collin,

Unfortunately no this is not possible with our Toolset plugins. As you mentioned you are doing the weighted score based on the user's searches.

In order for Views to filter the post or sort the post there must be a physical value that is stored on all the posts that can be sorted.

The revisions are only generated when a post is edited. So you will need an algorithm that can check the items being searched and stockpile a weighted index and store it on the posts.

Thanks,
Shane

#1833471

Thanks for letting me know what's possible on this. Please consider my issue is resolved now. Thank you!