Skip to content

Instantly share code, notes, and snippets.

@egaumer
Created February 24, 2012 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egaumer/1903734 to your computer and use it in GitHub Desktop.
Save egaumer/1903734 to your computer and use it in GitHub Desktop.
Example of geoDistance filtering with Cloud9 Javascript API
#!/bin/bash
# run this before feeding example documents
# create the collection
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant'
# explicitly define the location field to be of type 'geo_point'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/_mapping' -d '{
"locations" : {
"properties" : {
"location" : {
"type" : "geo_point"
}
}
}
}'
/* Filtering results by distance with Cloud9 */
(function($) {
/* create a query object - match anything */
var query = c9.query.MatchAllQuery();
/* construct a geo-distance filter */
var geoFilter = c9.filter.GeoDistanceFilter("location").distance(10).point(-75.57655, 40.53555);
/* compose a filtered query */
var geoDistanceQuery = c9.query.FilteredQuery(query, geoFilter);
/* a function to display results - uses underscore.js templates */
var resultsCallBack = function(results) {
if (results.hits) {
var template = _.template($("#results").html(), results);
$(".search").empty();
$(".search").append(template);
}
};
/* execute the request */
c9.search.Request()
.collections("restaurant")
.types("locations")
.query(geoDistanceQuery)
.get(resultsCallBack);
})(jQuery);
<!doctype html>
<head>
<title>Simple Search</title>
<!-- Example search template using underscore.js -->
<script type="text/template" id="results">
<% _.each(hits.hits, function(hit) { %>
<li><%= hit._source.name %></li>
<% }); %>
</script>
</head>
<body>
<div class="search"></div>
<script src="js/jquery-1.6.2.min.js"></script>
<script src="js/underscore-min.js"></script>
<script src="js/c9/c9api.min.js"></script>
<!-- change 'yourapp' to the name of your Cloud9 application -->
<script defer src="yourapp/js/distanceSearch.js"></script>
</body>
</html>
#!/bin/bash
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/1' -d '{
"name": "Primo Hoagies", "location": [-75.531975, 40.611927]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/2' -d '{
"name": "Armettas", "location": [-75.4903992, 40.535961]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/3' -d '{
"name": "White Orchids Thai Cuisine", "location": [-75.4159845, 40.5545901]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/4' -d '{
"name": "On The Border", "location": [-75.4415223, 40.6378366]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/5' -d '{
"name": "Jack Creek Steakhouse", "location": [-75.4307138, 40.6405604]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/6' -d '{
"name": "Copperhead Grille", "location": [-75.4382547, 40.634955]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/7' -d '{
"name": "Zoup!", "location": [-75.4341065, 40.639597]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/8' -d '{
"name": "Buckeye Tavern", "location": [-75.544996, 40.518787]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/9' -d '{
"name": "P.F. Changs China Bistro", "location": [-75.2855685, 40.1151001]}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/restaurant/locations/10' -d '{
"name": "The Cheesecake Factory", "location": [-75.3843268, 40.0899479]}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment