could someone point me to an example of RandomScore? I'd like to use random score for (seeded) random sorting, similar to this github issue: https://github.com/elastic/elasticsearch/issues/1170#issuecomment-22819670
but I'd like to do this with elasticsearch-dsl-py
I do it like this:
class MyDocument(Document):
...
@classmethod
def random(cls, *, seed=0, field='_seq_no', num_docs=10):
search = cls.search(using=es_client, index='index-name')
search = search.query(
'function_score',
functions=({
'random_score': {'seed': seed, 'field': field},
}),
)
search = search[:num_docs]
return search
If you want to specify a seed, then you also need to specify a field. More info on that (as well as why I chose _seq_no for my field):
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random
Hope that helps!
thank you this works!
do you also know how I could incorporate search_after into the random sorting? basically I'd like to be able to make another call with random sorting, and not get any previously seen results using something like
some_score_or_value = ?
search = search.extra(search_after=some_score_or_value)
with a normal sort I could put a one-element list of the last sort value, and it'd work
actually I figured out a way, just have to add this:
search = search.sort({"_score": {"order": "asc"}})
then I could use the last sort value of the previous call, as the search_after on the next call
search = search.extra(search_after=last_result['sort'])