Simple example of how to re-order your original query and wrap it to randomize the results.
This is using the official PHP ES library: https://github.com/elastic/elasticsearch-php
<?php
if ($this->random) {
$originalQuery = $this->params['body']['query'];
$this->params['body']['query'] = [
'function_score' => [
'query' => $originalQuery,
'random_score' => (new \stdClass()),
],
];
}
$searchRawResult = $this->client->search($this->params);
Top comments (2)
This is my search data:
Results come up, but unfortunately without random sorting. :-(
When I use this search data as a json object in ElasticVue (a browser based Elasticsearch client) the results are random though.
I am looking for a method to sort a search result from Elasticsearch in a random order. Nothing seems to work. I tried your example. No error message comes up, but the sorting doesn't change. It is difficult to debug.
I am using this composer package:
"elasticsearch/elasticsearch":"~7"