Skip to content

Instantly share code, notes, and snippets.

@egaumer
Created February 23, 2012 21:24
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/1895130 to your computer and use it in GitHub Desktop.
Save egaumer/1895130 to your computer and use it in GitHub Desktop.
Example termStatsFacet using Cloud9 Javascript API
<!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.product %> - <%= hit._source.quantity %></li>
<% }); %>
<br>
<% _.each(facets.stats.terms, function(stat) { %>
<%= stat.term %> - <%= stat.total %> total sold<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/search.js"></script>
</body>
</html>
#!/bin/bash
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples/computed/1' -d '{
"price": 599.99,
"quantity": 1287,
"store": "San Francisco",
"product": "iPad"
}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples/computed/2' -d '{
"price": 599.99,
"quantity": 1411,
"store": "New York",
"product": "iPad"
}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples/computed/3' -d '{
"price": 199.99,
"quantity": 2253,
"store": "San Francisco",
"product": "iPhone"
}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples/computed/4' -d '{
"price": 199.99,
"quantity": 2739,
"store": "New York",
"product": "iPhone"
}'
/* Creating pivot tables with the Cloud9 API
*
* The termsStat facet is similar to a pivot table in that it groups
* documents by one field and calculates stats over another field.
*/
(function($) {
/* create a query object - match anything */
var query = c9.query.MatchAllQuery();
/* create a termsStat facet */
var totalSold = c9.facet.TermStatsFacet("stats")
.keyField("product")
.valueField("quantity")
.order("total");
/* 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("examples")
.types("computed")
.query(query)
.sort("product", "asc")
.sort("quantity")
.facet(totalSold)
.get(resultsCallBack);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment