Created
February 26, 2012 17:23
-
-
Save egaumer/1917845 to your computer and use it in GitHub Desktop.
Example Span queries using Cloud9 Javascript API
This file contains hidden or 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._source.text %></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/spanSearch.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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/phrase/1' -d '{ | |
"text":"fish and chips is rather pleasing" | |
}' | |
curl -XPUT -H 'Content-Type:application/json' 'http://localhost:2600/v1/examples/phrase/2' -d '{ | |
"text":"fish and jam is quite disgusting" | |
}' |
This file contains hidden or 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
/* Example using Span queries | |
* | |
* Span queries allow for nested, positional | |
* restrictions when matching documents in Lucene. | |
*/ | |
(function($) { | |
/* create a span term query for fish in the text field */ | |
var fish = c9.query.SpanTermQuery("text", "fish"); | |
/* create a span term query for chips in the text field */ | |
var chips = c9.query.SpanTermQuery("text", "chips"); | |
/* create a span near query for fish within 2 words of chips */ | |
var fishNearChips = c9.query.SpanNearQuery([fish, chips]).slop(2).inOrder("true"); | |
/* create a span not query for fish that does not occurr within 2 words of chips */ | |
var query = c9.query.SpanNotQuery().include(fish).exclude(fishNearChips); | |
/* 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("phrase") | |
.query(query) | |
.get(resultsCallBack); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If order is not important (i.e., chips can be within 2 tokens to the left or the right of fish), then a Lucene proximity search is simpler and will perform better. You can use the Cloud9 queryString object which excepts any Lucene [Query Snytax](http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Proximity Searches)