0

Trying to search within an Index by query string, sort by numeric field all within a specific radius.

There is no reference in the Elastic documentation, so I may have to solve it at the application level rather than at the database level. But before inventing the wheel, I wanted to make sure I read it right.

Assume the following document structure (map):

* Title: String
* Description: String
* Rating: Numeric.
* Location: { Lat, Long}

I want to query this document with the following constraints:

  1. Within a 20km radius
    • Search (fuzzi) by title and description.
    • The results should be sorted by rating.
  2. If the results are less than X (let's assume 100) - do the same but increase the radius to 50km.

If the above is not possible at the database level, I'll do it at the application level. However, in order to do so, I still need to be able to query just 1 (i.e. within a specific radius search for X text and sort by rating).

To summarize:

1. Can I do the entire process in Elastic? If so, how?
2. Assuming I can't (which I suspect this is the situation), how can I query the above item 1 ?

Thanks :)

1 Answers1

0

You could start by testing geo queries. In the example below I used the geo distance.

Question 1 - Within a 20km radius

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "",
            "fuzziness": "AUTO",
            "fields": [
              "Title",
              "Description"
            ]
          }
        }
      ],
      "filter": {
        "geo_distance": {
          "distance": "20km",
          "pin.location": {
            "lat": 40,
            "lon": -70
          }
        }
      }
    }
  },
  "sort": [
    {
      "Rating": {
        "order": "desc"
      }
    }
  ]
}

Question 2:

Execute a new query with new distance.

rabbitbr
  • 2,991
  • 2
  • 4
  • 17