How can I use elasticsearch to search with special commands in query?

119 Views Asked by At

I have an elastic index with many documents. A document represents some event. Every event has a date and geolocation.

I would like to send queries like these: "Some event in LA", "Some event tomorrow", "Some event near me"...

How could I build this solution with elastic?

1

There are 1 best solutions below

1
On

Elasticsearch have some tools to do that simple. For example we have a pin in a specific location (lat, long) like this:

{
    "pin" : {
        "location" : {
            "lat" : 40.12,
            "lon" : -71.34
        }
    }
}

To search in a range of 200km we can do a query like this:

{
    "bool" : {
        "must" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "200km",
                "pin.location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}

Is really easy And for dates you can use the a range query like this:

{
    "range" : {
        "date" : {
            "gte" : "now/d",
            "lt" :  "now+1d/d"
        }
    }
}

For more example take a look in the documentation https://www.elastic.co/guide/en/elasticsearch/guide/current/geoloc.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html