I want to add sorting by \"field1\" if it\'s exists in document, and by \"field2\" if not. Help me please with query syntax.
As already pointed in this SO question
try to use something like this for int fields :
sort=min(def(A_160018,9000000),def(A_chandigarh1,9000000))
For string fields the option would be to use in the core/collection an extra field containing the concatenation of the fields - e.g. fullname: lastname + ' ' + firstname
written explicitly in the document when it is created.
Then you can still do sort=fullname asc