Search code examples
sortingelasticsearchgeofoselasticabundle

ES giving error when sorting by distance


I'm trying to sort search results by distance. However, when i try i get the following error:

{
   "error": {
      "root_cause": [
         {
            "type": "illegal_argument_exception",
            "reason": "sort option [location] not supported"
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "roeselaredev",
            "node": "2UYlfd7sTd6qlJWgdK2wzQ",
            "reason": {
               "type": "illegal_argument_exception",
               "reason": "sort option [location] not supported"
            }
         }
      ]
   },
   "status": 400
}

The query i sent looks like this:

GET _search
{
    "query": {
        "match_all": [] 
    },
    "sort": [

        {
            "geo_distance": {
                "location": {
                    "lat": 50.9436034,
                    "long": 3.1242917
                },
                "order":"asc",
                "unit":"km",
                "distance_type":"plane"
            }
        },
        {
            "_score": {
                "order":"desc"   
            }        
        }
    ]
}

As near as i can tell i followed the instructions in the documentation to the letter. I'm not getting a malformed query result. I'm just getting a not supported result for the sort by distance option. Any ideas as to what i'm doing wrong?


Solution

  • The query dsl is invalid the OP is almost-correct :) but missing an under-score.

    While sorting by distance it is _geo_distance and not geo_distance.

    Example:

    GET _search
    {
        "query": {
            "match_all": [] 
        },
        "sort": [
    
            {
                "_geo_distance": {
                    "location": {
                        "lat": 50.9436034,
                        "long": 3.1242917
                    },
                    "order":"asc",
                    "unit":"km",
                    "distance_type":"plane"
                }
            },
            {
                "_score": {
                    "order":"desc"   
                }        
            }
        ]
    }