Skip to content

Instantly share code, notes, and snippets.

@egaumer
Created February 23, 2012 20:26
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/1894884 to your computer and use it in GitHub Desktop.
Save egaumer/1894884 to your computer and use it in GitHub Desktop.
Example nestedQuery using Cloud9 Javascript API
#!/bin/bash
# create index
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples'
# setup mapping
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples/nested/_mapping' -d '{
"nested": {
"properties": {
"rows": {
"type": "nested"
}
}
}
}'
<!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.title %></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/nestedSearch.js"></script>
</body>
</html>
/* This example shows how to construct a nestedQuery
*
* The query will only match documents where country and product_id
* match within the same row object.
*/
(function($) {
/* construct a Lucene query string object */
var queryString = c9.query.QueryString("rows.country:japan AND rows.product_id:2");
/* build a nestedQuery object setting the context (path) to the root doc object, rows */
var nestedQuery = c9.query.NestedQuery(queryString).path("rows");
/* 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("examples")
.types("nested")
.query(nestedQuery)
.get(resultsCallBack);
})(jQuery);
#!/bin/bash
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples/nested/1' -d '{
"title": "Nested example document",
"tags": ["tag1","tag2"],
"rows": [
{
"country": ["US","Europe"],
"product_id":1
},
{
"country": ["Japan"],
"product_id":2
},
{
"country": ["US","Japan"],
"product_id":3
}
]
}'
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples/nested/2' -d '{
"title": "Another nested example document",
"tags": ["tag1","tag2"],
"rows": [
{
"country": ["US","Europe"],
"product_id":1
},
{
"country": ["Europe"],
"product_id":2
},
{
"country": ["US","Japan"],
"product_id":3
}
]
}'
@egaumer
Copy link
Author

egaumer commented Feb 23, 2012

Nested queries are very similar to scope searches in FAST ESP. They allow you to search within sub-document fragments.

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