Skip to content

Instantly share code, notes, and snippets.

@egaumer
Last active October 1, 2015 01:27
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/1893930 to your computer and use it in GitHub Desktop.
Save egaumer/1893930 to your computer and use it in GitHub Desktop.
Example boolQuery using Cloud9 Javascript API
/* fullscale.examples.elasticjs */
(function($) {
/* compose a BoolQuery out of several TermQuery objs */
var boolQuery = evo.BoolQuery()
.must(
evo.TermQuery("message", "hello"))
.mustNot(
evo.TermQuery("message", "back"))
.should(
evo.TermQuery("user", "jsmith"));
/* 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 */
evo.Request()
.indices("twitter")
.types("tweet")
.query(boolQuery)
.doSearch(resultsCallBack);
})(jQuery);
<!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/boolSearch.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"
}'
@egaumer
Copy link
Author

egaumer commented Feb 23, 2012

Although Lucene provides the ability to create your own queries through its API, it also provides a rich query language through the Query Parser, a lexer which interprets a string into a Lucene Query.

For detailed information of that syntax see the Lucene Query Parser Syntax

Cloud9 exposes this query syntax through its queryString object which provides an easier mechanism for writing more complex queries.

An example of that can be seen here - https://gist.github.com/1893397

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