Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
How to return related posts for French content #1077
I've been struggling to return related posts on my site. I've used the code in the related posts widget as an example to make my own related posts function. Though, I cannot get related posts to be returned. Instead, recent posts on my site are returned and are mostly unrelated to the main post.
Here is my related posts function:
I'm thinking this might have something to do with the fact that my site is in French, so I've been trying to get the French analyzer working with related posts. After installing the ICU Analysis Plugin and restarting the Elasticsearch service, I've created the following code that I placed in my theme's functions.php :
Although I'm pretty sure this code is correct, I'm still not getting any related posts to show up. After reindexing my site with ElasticPress, only the recent posts show up like before. The only way I was able to get some related results was by using only the post_title field, and not post_content or terms.post_tag.name. I also tried reindexing with only the post_content analyzer mapping, but the same problem occurs and only recent posts are shown.
If anyone can provide any type of advice, that would be great.
I'll happily provide more details should someone request them.
Hi again @allan23. So I did a bit of research / testing on my end and ended up with pretty nice results.
Here's what I stumbled on after installing the Debug Bar & Debug Bar ElasticPress plugins for Wordpress and looking at the different Queries.
While looking at the Query Result, I noticed that the "max_score" and "_score" were always "null". While searching on the web, I found out that when a Query is sorted, it needs to have "track_scores" set to true for the scores to be computed. After looking at the Query Body, I found out that the query was indeed being sorted by "post_date". I tried passing "track_scores" to the query, but although I was now getting some scores, the posts remained the same because they were still sorted by date, and not by relevance.
What I did was edit the class-ep-api.php file to change the default sort order from "date" to "_score", and just like magic, related results were now being returned properly, even better than I could have imagined.
What I would suggest is to have the related post sorted by relevance (score) AND by date, if that's possible, by passing "track_scores" to the query to make verything work. If not, then sorting by relevance only is probably more than enough.
I hope my findings will help.
I really love this plugin as it is adding a whole bunch of great features to my site, and I'll be really happy if my findings can help other people too.
So here's what I've come up with to fix the issue.
Replace this code in class-ep-api.php
With this code
That way, the default sort is still by date, but people can change it to "_score" using the ep_set_default_sort filter if they wish to.
But maybe it would still be a good idea to have the more like this query results sorted by score AND by date... you tell me !