Elasticsearch sort works on and off

I am querying index in ES using below code

def cat_results_tmp(response):
	results=[]
	for hit in response['hits']['hits']:
		result_tuple = (hit["_source"]["fieldA"],hit["_source"]["fieldB"]
						)
		results.append(result_tuple)
	return results       

def cats(cat_sort_field="", sort_order="",country=["US"],cat=[]):
	client = Elasticsearch([{'host':host,'port':9200}])
	response = client.search( \
	index="cat", \
	body={\
		"query": {\
	  "constant_score" : {
	   "filter":{
		"bool":{
		"must":[
		{"terms":{"country.keyword":country}},
		{"bool":{
		"should":[
		{"terms": {"lvl0.keyword":cat[0]}}
		,{"terms": {"lvl1.keyword":cat[1]}},
		{"terms": {"lvl2.keyword":cat[2]}},
		{"terms": {"lvl3.keyword":cat[3]}}
		]
		}}]}}}}
	   ,"sort" : [
		 { cat_sort_field: {"order" : sort_order} }]
		,"size":100
		}
		)
	response3=cat_results_tmp(response)
	return response3

#get sort by fieldA asc result
	print(cats(cat_sort_field="fieldA",sort_order="asc",country=['US'],cat=[[''], [''], ['helloworld']]))
#get sort by fieldB desc result
	print(cats(cat_sort_field="fieldB",sort_order="desc",country=['US'],cat=[[''], [''], ['helloworld']]))

While the second one works as expected, the first one does not return sorted result.
I have tried casting fieldA to double with below, but still can't get sorted result.

curl -XPUT "http://host/cat/_mappings" -d '
 {
    "properties": {
      "fieldA": { "type": "double" }
    }
  }' -H 'Content-Type: application/json'

What is possibly going wrong here? Thanks.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.