Skip to content

Instantly share code, notes, and snippets.

@egaumer
Created February 25, 2012 01:59
Show Gist options
  • Save egaumer/1905236 to your computer and use it in GitHub Desktop.
Save egaumer/1905236 to your computer and use it in GitHub Desktop.
Example of dateHistogramFacet 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.title %> published on <%= hit._source.date %></li>
<% }); %>
<h3>Date Histogram Facet (By Year)</h3>
<% _.each(facets.pubdate.entries, function(entry) { %>
<%= new Date(entry.time).getFullYear() + 1 %> (<%= entry.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/search.js"></script>
</body>
</html>
#!/bin/bash
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/1' -d '{
"title":"article 1","date": "2009-08-16"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/2' -d '{
"title":"article 2","date": "2009-09-03"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/3' -d '{
"title":"article 3","date": "2009-09-23"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/4' -d '{
"title":"article 4","date": "2009-10-09"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/5' -d '{
"title":"article 5","date": "2010-01-06"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/6' -d '{
"title":"article 6","date": "2010-01-26"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/7' -d '{
"title":"article 7","date": "2011-05-17"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/8' -d '{
"title":"article 8","date": "2012-03-02"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/9' -d '{
"title":"article 9","date": "2012-03-13"}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/blog/posts/10' -d '{
"title":"article 10","date": "2012-04-01"}'
/* Generating a Date Histogram Facet
*
* The dateHistogramFacet works with numeric data by building
* a histogram across intervals of the field values.
*/
(function($) {
/* create a query object - match anything */
var query = c9.query.MatchAllQuery();
var dateHistogramFacet = c9.facet.DateHistogramFacet("pubdate")
.field("date")
.interval("year");
/* 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("blog")
.types("posts")
.query(query)
.facet(dateHistogramFacet)
.sort("date")
.get(resultsCallBack);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment