Created
February 23, 2012 15:52
-
-
Save egaumer/1893397 to your computer and use it in GitHub Desktop.
Simple Search with Cloud9 Javascript API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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