We've been using ElasticPress with AWS ES for a couple of weeks now and many of the returned results don't appear to be relevant. Here's an example:
http://www.steamboattoday.com/search-results/?q=lacrosse&range=2017-03-02&sort=DESC
Many of the results do not include the word "lacrosse" in the post or title. I'm assuming that the returned results are matching because of stemming, but even then, some of these results appear to be odd matches. Oddly as well, last week we were using EP version 2.2x and had the same issue. But when I upgraded to 2.3, results appeared to be relevant again. Then a few days later, results are once again not relevant.
Any help or insight that could be provided would be greatly appreciated. Let me know if there's any additional debug info that you need from me. Thanks!
Configuration:
WordPress: 4.7.5
ElasticPress: 2.3
Elasticsearch: 5.2.2
Host: AWS/elastic.co
ep_integrate' => true
Debug query body:
{
"from": 0,
"size": 20,
"sort": [
{
"post_date": {
"order": "desc"
}
}
],
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "lacrosse",
"type": "phrase",
"fields": [
"post_title",
"post_content",
"post_excerpt",
"author_name",
"terms.post_tag.name",
"terms.category.name",
"post_author.login"
],
"boost": 4
}
},
{
"multi_match": {
"query": "lacrosse",
"fields": [
"post_title",
"post_content",
"post_excerpt",
"author_name",
"terms.post_tag.name",
"terms.category.name",
"post_author.login"
],
"boost": 2,
"fuzziness": 0,
"operator": "and"
}
},
{
"multi_match": {
"fields": [
"post_title",
"post_content",
"post_excerpt",
"author_name",
"terms.post_tag.name",
"terms.category.name",
"post_author.login"
],
"query": "lacrosse",
"fuzziness": 1
}
}
]
}
},
"exp": {
"post_date_gmt": {
"scale": "14d",
"decay": 0.25,
"offset": "7d"
}
},
"score_mode": "avg",
"boost_mode": "sum"
}
},
"post_filter": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"range": {
"post_date": {
"gt": "2017-03-02 00:00:00"
}
}
}
]
}
},
{
"term": {
"post_type.raw": "post"
}
},
{
"terms": {
"post_status": [
"publish",
"acf-disabled"
]
}
}
]
}
}
}
The problem seems to exist because of the sorting parameter. If you either drop this one (sorting default is _score: 'desc') or set it explicitly to the default one the results should be correct again. When using post_date the _score field is not used for sorting and therefore many "fuzzy" matches are listed on top. You can also drop the the multiple query statements or set the fuzziness for every condition to zero.
@fabianmarz thanks so much for pointing me in the right direction.
I went ahead and changed the ordeby parameter on my WP_Query args from:
'orderby' => 'date'
to::
'orderby' => 'relevance date' // orderby can include more than one order criterion
That causes the following to be passed to the elasticsearch query and appears to produce acceptable results:
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"post_date": {
"order": "desc"
}
}
],
So, in theory, results should be sorted by relevance first and then if any two results have the same relevance score, the newest/most recent post will show first.
I'm also using the sort url query parameter to pass either asc or desc and use the following to modify the post_date order value:
function set_es_search_args( $ep_formatted_args, $args = '' ) {
if ( isset( $_GET['sort'] ) && !empty( $_GET['sort'] ) ) {
$ep_formatted_args['sort'][1]['post_date']['order'] = $_GET['sort'];
}
return $ep_formatted_args;
}
add_filter('ep_formatted_args', 'set_es_search_args', 300, 2 );
Surprisingly though, when I switch back and forth between asc and desc, it does not appear to affect the results. I'm guessing that's because no two results have the exact same relevance score so the second criterion (post_date) doesn't kick in and affect the actual sort order. https://www.elastic.co/guide/en/elasticsearch/guide/current/_sorting.html#_multilevel_sorting
Anyhow, I'll still do some tweaking perhaps but your reply helped me immensely. :-) Feel free to add any additional thoughts that you might have. Thanks again!
Most helpful comment
@fabianmarz thanks so much for pointing me in the right direction.
I went ahead and changed the ordeby parameter on my WP_Query args from:
'orderby' => 'date'
to::
'orderby' => 'relevance date' // orderby can include more than one order criterion
That causes the following to be passed to the elasticsearch query and appears to produce acceptable results:
"sort": [ { "_score": { "order": "desc" } }, { "post_date": { "order": "desc" } } ],So, in theory, results should be sorted by relevance first and then if any two results have the same relevance score, the newest/most recent post will show first.
I'm also using the sort url query parameter to pass either asc or desc and use the following to modify the post_date order value:
function set_es_search_args( $ep_formatted_args, $args = '' ) { if ( isset( $_GET['sort'] ) && !empty( $_GET['sort'] ) ) { $ep_formatted_args['sort'][1]['post_date']['order'] = $_GET['sort']; } return $ep_formatted_args; } add_filter('ep_formatted_args', 'set_es_search_args', 300, 2 );Surprisingly though, when I switch back and forth between asc and desc, it does not appear to affect the results. I'm guessing that's because no two results have the exact same relevance score so the second criterion (post_date) doesn't kick in and affect the actual sort order. https://www.elastic.co/guide/en/elasticsearch/guide/current/_sorting.html#_multilevel_sorting
Anyhow, I'll still do some tweaking perhaps but your reply helped me immensely. :-) Feel free to add any additional thoughts that you might have. Thanks again!