Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering by random with seed triggers error #1490

Closed
jgmedialtd opened this issue Sep 17, 2019 · 9 comments
Closed

Ordering by random with seed triggers error #1490

jgmedialtd opened this issue Sep 17, 2019 · 9 comments

Comments

@jgmedialtd
Copy link

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
}

@jgmedialtd jgmedialtd added the type:bug Something isn't working. label Sep 17, 2019
@aaronjpitts
Copy link

Any update on this?

@brandwaffle
Copy link
Contributor

brandwaffle commented Jan 20, 2020

@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'

@jgmedialtd
Copy link
Author

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

@aaronjpitts
Copy link

aaronjpitts commented Jan 20, 2020

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.

@brandwaffle
Copy link
Contributor

brandwaffle commented Jan 20, 2020

@jgmediadesign @aaronjpitts got it, thanks!

Looks like we can do this by adding a "seed: <integer>" parameter to our random_score function here

$formatted_args['query']['function_score']['random_score'] = (object) [];
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!

@brandwaffle brandwaffle added enhancement and removed type:bug Something isn't working. labels Jan 20, 2020
@aaronjpitts
Copy link

aaronjpitts commented Jan 21, 2020

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

@aaronjpitts
Copy link

aaronjpitts commented Jan 21, 2020

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 );]

@brandwaffle
Copy link
Contributor

@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!!!

@aaronjpitts
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants