0

I am trying to return the distance between two points provided as latitude and longitude values. Here's what i have tried doing.

await this.esService.search({
            index: IndexName,
            body: {
                sort: [{ avg_rating: { order: "desc" } }],
                "script_fields" : {
                    "distance" : {
                        "lang": "painless",
                        "params" : {
                           "lat" : 47.1,
                           "lon" : 8.1
                        },
                        "script" : "doc[\u0027user.userAddresses[0].location\u0027].distanceInKm(lat,lon)"
                     }
                 },
                from: offset,
                size: take,
                query: finalQuery
            }
        })

I am recieving this error parsing_exception: [parsing_exception] Reason: Unknown key for a VALUE_STRING in [lang]. Is there anything wrong going on here ?

1 Answer 1

1

You're almost there, you're just missing the script declaration

            "script_fields" : {
                "distance": {                  
                  "script" : {             <--- add this
                    "lang": "painless",
                    "params" : {
                       "lat" : 47.1,
                       "lon" : 8.1
                    },
                    "source" : "doc[\u0027user.userAddresses[0].location\u0027].distanceInKm(lat,lon)"
                       ^
                  }    |
                }      |
             },  also change this
Sign up to request clarification or add additional context in comments.

2 Comments

Well actually after doing above i get this error now Unknown key for a START_OBJECT in [script].
Which version of ES are you running? Script_fields are specified here

Your Answer

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