Elasticpress: Ordering by random with seed triggers error

Created on 17 Sep 2019  路  9Comments  路  Source: 10up/ElasticPress

Currently, when attempting to order randomly, but retain pagination using a random seed as shown in the example below, an error is returned. It is possible to support this sorting mechanic?

'orderby' => string 'RAND(8)'

ElasticPress returns the following error:

{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "No mapping found for [RAND(8)] in order to sort on",
"index_uuid": "dIoG24_OSIGfs40rPkmoZg",
"index": "booking8890-post-1"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "booking8890-post-1",
"node": "lT4wyZTURuqZ9S6hc_GivQ",
"reason": {
"type": "query_shard_exception",
"reason": "No mapping found for [RAND(8)] in order to sort on",
"index_uuid": "dIoG24_OSIGfs40rPkmoZg",
"index": "booking8890-post-1"
}
}
]
},
"status": 400
}

enhancement

Most helpful comment

@jgmediadesign @aaronjpitts got it, thanks!

Looks like we can do this by adding a "seed: <integer>" parameter to our random_score function here https://github.com/10up/ElasticPress/blob/5eb81bb04982f09523845fe659d6b9f91180000c/includes/classes/Indexable/Post/Post.php#L1120 if we parse similarly to how WP handles it here https://core.trac.wordpress.org/changeset/36632.

I'll add an enhancement label to this and we will get it worked on as soon as possible (which could be a few weeks and also depends a bit on release schedule). In the meantime it should be filterable via ep_formatted_args which could be used to override the previous values and set the seed parameter if you parse the regex the same way as WP does.

Thanks for bringing this one to my attention!

All 9 comments

Any update on this?

@jgmediadesign @aaronjpitts I'm not sure exactly what the use-case is here. Are you attempting to pass a parameter of 8 into a RAND function and apply that to the Elasticsearch query?

Currently, my understanding is that WP_Query only supports a "rand" value for random order, which ElasticPress does support. We don't have any support for any other formats of RAND, but if you can share where this comes from I'm happy to dig in. Otherwise, if you're looking to get random sort order, pass your parameter like this 'orderby' => 'rand'

Probably a bit niche - but if you want to randomly order posts, but also use pagination you need to use a random seed - this may explain things a bit better...

https://core.trac.wordpress.org/ticket/35692

Hi @brandwaffle, yes @jgmediadesign is correct.

The number is the seed which provides the random order, but by providing the seed it will ensure the same randomised order will always be shown given that seed. This is critical for pagination etc. and if you want to allow users to share search results by sharing the seed in the URL etc.

@jgmediadesign @aaronjpitts got it, thanks!

Looks like we can do this by adding a "seed: <integer>" parameter to our random_score function here https://github.com/10up/ElasticPress/blob/5eb81bb04982f09523845fe659d6b9f91180000c/includes/classes/Indexable/Post/Post.php#L1120 if we parse similarly to how WP handles it here https://core.trac.wordpress.org/changeset/36632.

I'll add an enhancement label to this and we will get it worked on as soon as possible (which could be a few weeks and also depends a bit on release schedule). In the meantime it should be filterable via ep_formatted_args which could be used to override the previous values and set the seed parameter if you parse the regex the same way as WP does.

Thanks for bringing this one to my attention!

Thank you for the update @brandwaffle. Could you please give me an idea on how I could do this in the meantime with ep_formatted_args ? I can't seem to figure out how that filter works.

Many thanks

I figured it out. For anyone else that may be needing this you can use the below, remember to add a 'seed' property to your WP_Query $args.

add_filter('ep_formatted_args', function ($ep_formatted_args, $args) {
    if ($args['orderby'] == 'rand') {
        $ep_formatted_args['query']['function_score']['random_score'] = (object) ['seed' => $args['seed']];
    }

    return $ep_formatted_args;
}, 10, 2 );]

@aaronjpitts @jgmediadesign would you mind testing out the PR submitted above to see if it properly creates seeded random sort queries? Thanks again for submitting this ticket and for the active participation!!!

@brandwaffle I just tried the PR and I can confirm it works :)

Was this page helpful?
0 / 5 - 0 ratings