Skip to content

Instantly share code, notes, and snippets.

@egaumer
Created February 24, 2012 22:27
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/1904208 to your computer and use it in GitHub Desktop.
Save egaumer/1904208 to your computer and use it in GitHub Desktop.
Example of geoDistance facet using 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"
}
}
}
}'
/* Generating Geo Distance Facet
*
* The geoDistanceFacet facet provides information
* over a range of distances from a provided point.
*/
(function($) {
/* create a query object - match anything */
var query = c9.query.MatchAllQuery();
var geoFacet = c9.facet.GeoDistanceFacet("geo1")
.pointField("location")
.point(-75.57655, 40.53555)
.addUnboundedTo(10)
.addRange(11, 50)
.addUnboundedFrom(51);
/* a function to display results - uses underscore.js templates */
var resultsCallBack = function(results) {
if (results.hits) {
console.dir(results);
var template = _.template($("#results").html(), results);
$(".search").empty();
$(".search").append(template);
}
};
/* execute the request */
c9.search.Request()
.collections("restaurant")
.types("locations")
.query(query)
.facet(geoFacet)
.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>
<% }); %>
<h3>Geo Distance Facet</h3>
<% _.each(facets.geo1.ranges, function(range) { %>
<%= range.from || 0 %> - <%= range.to || '...' %> miles (<%= range.total_count %> results)<br>
<% }); %>
</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/geofacetSearch.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