0
votes

I have this query in elastic search to get places by their name:

GET places/fastfood/_search
{
    "from" : 0, "size" : 10, 
    "query": {
        "bool": {
         "must": [
                { "regexp": { "name": ".*name.*"} },           
                { "term" : { "location_id" : id } }                         
        ]
      }      
    }
}

Where name and id are replaced to match what I need. Now I want to get the locations that match the name and id, but are closer to the location selected. Let's say I don't have a McDonalds in the location_id (city, zone) provided, I should get the closest to that location.

I have a field in every city/zone called location, that is a string with latitude, longitude, defined as geo_spot. And then also have latitude and longitude defined as long. Like this:

"location": "33.785400,-84.38480",
"latitude": "33.785400",
"longitude": "-84.384800",

So I try to add the sort by location, like this, but I get errors, something is wrong:

GET places/fastfood/_search
{
    "from" : 0, "size" : 10, 
    "query": {
        "bool": {
         "must": [
                { "regexp": { "name": ".*name.*"} },           
                { "term" : { "location_id" : location_id } }                         
        ]
      }      
    },
    "sort": [
    {
      "_geo_distance": {
        "location": { 
          "lat":  26.112,
          "lon": -73.998
        },
        "order":         "asc",
        "unit":          "km", 
        "distance_type": "plane" 
      }
    }
}

What I'm doing wrong? What should I change?

The error is this: { "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[n6y6tjWFRrCP5MXNhEV-YA][places_v1][0]: RemoteTransportException[[Boomer][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[places_v1][0]: query[+name:/.hosp./ +location_id:[10 TO 10]],from[0],size[10]: Parse Failure [Failed to parse source [{\r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\": \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]]; nested: JsonParseException[Unexpected close marker '}': expected ']' (for ARRAY starting at [Source: [B@4ac877f6; line: 10, column: 5])\n at [Source: UNKNOWN; line: 23, column: 2]]; }{[n6y6tjWFRrCP5MXNhEV-YA][places_v1][1]: RemoteTransportException[[Boomer][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[places_v1][1]: query[+name:/.hosp./ +location_id:[10 TO 10]],from[0],size[10]: Parse Failure [Failed to parse source [{\r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\": \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]]; nested: JsonParseException[Unexpected close marker '}': expected ']' (for ARRAY starting at [Source: [B@19f9b52f; line: 10, column: 5])\n at [Source: UNKNOWN; line: 23, column: 2]]; }{[7i0nOGiFRl-OYX8XvL_bSQ][places_v1][2]: SearchParseException[[places_v1][2]: query[+name:/.hosp./ +location_id:[10 TO 10]],from[0],size[10]: Parse Failure [Failed to parse source [{\r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\": \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]]; nested: JsonParseException[Unexpected close marker '}': expected ']' (for ARRAY starting at [Source: [B@427094b6; line: 10, column: 5])\n at [Source: [B@427094b6; line: 23, column: 2]]; }{[n6y6tjWFRrCP5MXNhEV-YA][places_v1][3]: RemoteTransportException[[Boomer][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[places_v1][3]: query[+name:/.hosp./ +location_id:[10 TO 10]],from[0],size[10]: Parse Failure [Failed to parse source [{\r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\": \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]]; nested: JsonParseException[Unexpected close marker '}': expected ']' (for ARRAY starting at [Source: [B@4f781e6c; line: 10, column: 5])\n at [Source: UNKNOWN; line: 23, column: 2]]; }{[n6y6tjWFRrCP5MXNhEV-YA][places_v1][4]: RemoteTransportException[[Boomer][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[places_v1][4]: query[+name:/.hosp./ +location_id:[10 TO 10]],from[0],size[10]: Parse Failure [Failed to parse source [{\r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\": \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]]; nested: JsonParseException[Unexpected close marker '}': expected ']' (for ARRAY starting at [Source: [B@586764c7; line: 10, column: 5])\n at [Source: UNKNOWN; line: 23, column: 2]]; }]", "status": 400 }

1

1 Answers

4
votes

It's a syntax error, you should close your sort with ] rather than }