Created
February 23, 2012 21:06
-
-
Save egaumer/1895052 to your computer and use it in GitHub Desktop.
Example of creating a computedProperty using 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.hits, function(hit) { %> | |
<li><%= hit.fields.amount %></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/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" | |
}' |
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
/* Returning computed properties with the Cloud9 API | |
* | |
* A computedProperty is a dynamically generated document property that is constructed | |
* from existing properties. This example creates a computedProperty called "amount" | |
* that is derived from multiplying the price and quantity fields. | |
*/ | |
(function($) { | |
/* create a query object - match anything */ | |
var query = c9.query.MatchAllQuery(); | |
var amount = c9.search.ComputedProperty("amount") | |
.lang("js") | |
.script("_source.price * _source.quantity"); | |
/* 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) | |
.computedProperty(amount) | |
.fields(["_source"]) | |
.get(resultsCallBack); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment