Created
July 28, 2012 22:59
-
-
Save karussell/3195115 to your computer and use it in GitHub Desktop.
ElasticSearch - multiple geo_points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HOST="http://localhost:9200" | |
# rebuild index | |
curl -XDELETE "$HOST/osm" | |
curl -XPUT "$HOST/osm" | |
curl -XPUT "$HOST/osm/way/_mapping" -d ' | |
{ | |
"way" : { | |
"properties" : { | |
"location": { "type": "geo_point", "lat_lon": "true" } | |
} | |
} | |
}' | |
# attention [lon,lat] according to geojson! | |
curl -XPUT "$HOST/osm/way/1" -d ' | |
{ | |
"text" : "nice street", | |
"location": [48.6752901, 2.379928] | |
}' | |
curl -XPUT "$HOST/osm/way/2" -d ' | |
{ | |
"text" : "wonderful street", | |
"location": [48.7, 2.4] | |
}' | |
# force refresh in order to query | |
curl -XPOST "$HOST/osm/_refresh" | |
curl -XPOST "$HOST/osm/way/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"match_all" : {} | |
}, | |
"facets" : { | |
"geo1" : { | |
"geo_distance" : { | |
"way.location" : { | |
"lat" : 2.3, | |
"lon" : 48.7 | |
}, | |
"ranges" : [ | |
{ "to" : 10 }, | |
{ "from" : 10, "to" : 20 }, | |
{ "from" : 20, "to" : 100 }, | |
{ "from" : 100 } | |
] | |
} | |
} | |
} | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HOST="http://localhost:9200" | |
# rebuild index | |
curl -XDELETE "$HOST/osm" | |
curl -XPUT "$HOST/osm" | |
curl -XPUT "$HOST/osm/way/_mapping" -d ' | |
{ | |
"way" : { | |
"properties" : { | |
"nodes" : { | |
"properties" : { | |
"location": { "type": "geo_point", "lat_lon": "true" } | |
} | |
} | |
} | |
} | |
}' | |
# attention [lon,lat] according to geojson! | |
curl -XPUT "$HOST/osm/way/1" -d ' | |
{ | |
"text" : "nice street", | |
"nodes":[{ | |
"location": [49, 2.4] | |
}] | |
}' | |
curl -XPUT "$HOST/osm/way/2" -d ' | |
{ | |
"text" : "wonderful street", | |
"nodes":[{ | |
"location": [48, 2.4] | |
}, { | |
"location": [50, 2.4] | |
}] | |
}' | |
# force refresh in order to query | |
curl -XPOST "$HOST/osm/_refresh" | |
# check that 1km is low enough to be selective | |
curl -XPOST "$HOST/osm/way/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"filtered" : { | |
"query" : { | |
"match_all" : {} | |
}, | |
"filter" : { | |
"geo_distance" : { | |
"distance" : "1km", | |
"nodes.location" : { | |
"lat" : 2.4, | |
"lon" : 48.5 | |
} | |
} | |
} | |
} | |
} | |
}' | |
# get "wonderful street" via one point | |
curl -XPOST "$HOST/osm/way/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"filtered" : { | |
"query" : { | |
"match_all" : {} | |
}, | |
"filter" : { | |
"geo_distance" : { | |
"distance" : "1km", | |
"nodes.location" : { | |
"lat" : 2.4, | |
"lon" : 48 | |
} | |
} | |
} | |
} | |
} | |
}' | |
# get again "wonderful street" via other point | |
curl -XPOST "$HOST/osm/way/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"filtered" : { | |
"query" : { | |
"match_all" : {} | |
}, | |
"filter" : { | |
"geo_distance" : { | |
"distance" : "1km", | |
"nodes.location" : { | |
"lat" : 2.4, | |
"lon" : 50 | |
} | |
} | |
} | |
} | |
} | |
}' | |
# get "nice street" | |
curl -XPOST "$HOST/osm/way/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"filtered" : { | |
"query" : { | |
"match_all" : {} | |
}, | |
"filter" : { | |
"geo_distance" : { | |
"distance" : "1km", | |
"nodes.location" : { | |
"lat" : 2.4, | |
"lon" : 49 | |
} | |
} | |
} | |
} | |
} | |
}' | |
# faceting creates one set per facet range. so we can see 3 peaks here: | |
curl -XPOST "$HOST/osm/way/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"match_all" : {} | |
}, | |
"facets" : { | |
"geo1" : { | |
"geo_distance" : { | |
"nodes.location" : { | |
"lat" : 2.4, | |
"lon" : 47 | |
}, | |
"ranges" : [ | |
{ "to" : 50 }, | |
{ "from" : 50, "to" : 100 }, | |
{ "from" : 100, "to" : 150 }, | |
{ "from" : 150 } | |
] | |
} | |
} | |
} | |
}' | |
# but only 2 peaks here: | |
curl -XPOST "$HOST/osm/way/_search?pretty=true" -d ' | |
{ | |
"query" : { | |
"match_all" : {} | |
}, | |
"facets" : { | |
"geo1" : { | |
"geo_distance" : { | |
"nodes.location" : { | |
"lat" : 2.4, | |
"lon" : 47 | |
}, | |
"ranges" : [ | |
{ "to" : 2 }, | |
{ "from" : 2, "to" : 4 }, | |
{ "from" : 10, "to" : 15 }, | |
{ "from" : 15 } | |
] | |
} | |
} | |
} | |
}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment