Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geo: Filter by distance based on location #279

Closed
kimchy opened this issue Jul 28, 2010 · 4 comments

Comments

@kimchy
Copy link
Member

commented Jul 28, 2010

The ability to define a query DSL filter, allowing to include only hits that exists within a specific distance from a geo point. Assuming the following indexed json:

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

Then the following simple query can be executed with a geo_distance filter:

{
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km"
                "pin.location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}

Accepted Formats

In much the same way the geo_point type can accept different representation of the geo point, the filter can accept it as well:

Lat Lon As Properties

{
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km"
                "pin.location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}

Lat Lon As Array

{
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km"
                "pin.location" : [40, -70]
            }
        }
    }
}

Lat Lon As String

{
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km"
                "pin.location" : "40,-70"
            }
        }
    }
}

Geohash

{
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km"
                "pin.location" : "drm3btev3e86"
            }
        }
    }
}

Options

The following are options allowed on the filter:

  • distance: The distance to include hits in the filter. The distance can be a numeric value, and then the distance_unit (either mi/miles or km can be set) controlling the unit. Or a single string with the unit as well.
  • distance_type: How to compute the distance. Can either be arc (better precision) or plane (faster). Defaults to arc.

geo_point Type

The filter does not require the geo_point type to be set. It assumes that the location object indexed includes a lat and lon numeric values. The geo_point type follows these rules and allows for more flexible indexing options / structure, but is not required.

Multi Location Per Document

The geo_distance filter can work with multiple locations / points per document. Once a single location / point matches the filter, the document will be included in the filter.

@kimchy

This comment has been minimized.

Copy link
Member Author

commented Jul 28, 2010

implemented.

@mikenereson

This comment has been minimized.

Copy link

commented Jan 7, 2013

In examples, missing , after "distance" : "12km"

@gauraangkhurana

This comment has been minimized.

Copy link

commented Dec 6, 2016

What goes before this json request ?

I tried this 'geo_distance' filter query about 20 times but it gives me this error.

My Code :

`curl` -XGET localhost:9200/resume/_search -d  ' { {
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "12km"
                "pin.location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}

The error produced is

{"error":{"root_cause":[{"type":"parsing_exception","reason":"Unknown key for a START_OBJECT in [filtered].","line":1,"col":19}],"type":"parsing_exception","reason":"Unknown key for a START_OBJECT in [filtered].","line":1,"col":19},"status":400}

Kindly help me.

@dadoonet

This comment has been minimized.

Copy link
Member

commented Dec 6, 2016

Ask questions on discuss.elastic.co

Filtered query has been removed in 5.0. See breaking changes in doc

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.