Skip to content

Instantly share code, notes, and snippets.

@egaumer
Created February 23, 2012 15:52
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/1893397 to your computer and use it in GitHub Desktop.
Save egaumer/1893397 to your computer and use it in GitHub Desktop.
Simple Search with Cloud9 Javascript API
<!doctype html>
<head>
<title>Simple Search</title>
<!-- Example search template using underscore.js -->
<script type="text/template" id="results">
<% _.each(hits, function(hit) { %>
<li><%= hit._source.message %></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/search.js"></script>
</body>
</html>
#!/bin/bash
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/twitter/tweet/1' -d '{
"user": "jsmith",
"date": "2012-03-02",
"message": "A simple message saying hello"
}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/twitter/tweet/2' -d '{
"user": "jsmith",
"date": "2012-03-03",
"message": "A simple message saying goodbye"
}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/twitter/tweet/3' -d '{
"user": "jdoe",
"date": "2012-03-02",
"message": "A simple message saying hello back"
}'
/* A simple search example using the Cloud9 API */
(function($) {
/* create a query object */
var query = c9.query.QueryString("user:jsmith AND message:hello");
/* a function to display results - uses underscore.js templates */
var resultsCallBack = function(results) {
if (results.hits) {
var template = _.template($("#results").html(), results.hits);
$(".search").empty();
$(".search").append(template);
}
};
/* execute the request */
c9.search.Request()
.collections("twitter")
.types("tweet")
.query(query)
.get(resultsCallBack);
})(jQuery);
@egaumer
Copy link
Author

egaumer commented Feb 23, 2012

Note that Lucene filters are faster than boolean queries. This query can be rewritten to use a filter instead. See example of that here - https://gist.github.com/1893529

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment