The nearest hotel problem

Let's assume that we are at a particular point where the latitude is 1.234 and the longitude is 2.132. We need to find the nearest restaurants to this location.

For this purpose, the function_score query is the best option. We can use the decay (Gauss) functionality of the function score query to achieve this:

curl -XPOST 'http://localhost:9200/restaurants/_search' -d '{
  "query": {
    "function_score": {
      "functions": [
        {
          "gauss": {
            "location": {
              "scale": "1km",
              "origin": [
                1.231,
                1.012
              ]
            }
          }
        }
      ]
    }
  }
}'

Here, we tell Elasticsearch to give a higher score to the restaurants that are nearby the referral point we gave it. The closer it is, the higher is the importance.

Get Elasticsearch Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.