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.
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.
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 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.
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?
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.