2

I'm trying to form the command for sorting using elasticsearch-dsl. However I have trouble passing the variable in correct format in.

The format should be

s=Search()
s = s.sort({"time":{"order":"asc"}}, {"anoter_field":{"order":"desc"}})
s.execute()

The problem is I'm trying to put {"time":{"order":"asc"}}, {"anoter_field":{"order":"desc"}} as a variable, but I can't seem to get this in the right syntax. I tried using dict, list, and string, and none seems to work.

My input would be a dict that looks like

input = {"time":"asc", "another_field":"desc"}

1 Answer 1

7
data_input = {"time":"asc", "another_field":"desc"}
args = [{k:{'order':v}} for k,v in data_input.items()]
s.sort(*args)

I guess is what you are asking? Its hard to tell ...

Sign up to request clarification or add additional context in comments.

3 Comments

yes.... you got it exactly right. Is it the * that makes passing in a list okay?
yep thats called list unpacking
Your answer completely satisfies my need but, I'm filtering using {"name": "asc", "age": "asc"} in this case my result is just taking age as the only parameter to sort, ignoring sorting by name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.