Skip to content

Instantly share code, notes, and snippets.

@JnBrymn
Created June 8, 2013 04:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JnBrymn/5733984 to your computer and use it in GitHub Desktop.
Save JnBrymn/5733984 to your computer and use it in GitHub Desktop.
This is a demo search UI for demonstrating Search As You Type with Solr.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>search as you type</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<style>
#searchbox { width: 25em; }
#results {margin-top: 100px;}
</style>
<script>
$(function() {
function split( val ) {
return val.split( /\s+/ );
}
function extractLast( term ) {
return split( term ).pop();
}
function updateResults( results ) {
$('#results').empty();
$.map(results, function(item) {
$('#results').append('<li>'+item+'</li>');
})
console.log(results)
}
$( "#searchbox" ).autocomplete({
source: function( request, response ) {
if(request.term[request.term.length-1]==' ') {
response()
return;
}
$.ajax({
url: "http://localhost:8983/solr/browse",
dataType: "jsonp",
jsonp: 'json.wrf',
cache: true,
data: {
q:request.term+" "+extractLast( request.term )+"*",
"facet.prefix":extractLast( request.term ),
wt:"json",
facet:true,
"facet.field":"title",
mm:1
},
success: function( data ) {
var results = $.map(data.response.docs, function(item) { return item.title; });
updateResults(results);
response( $.map(data.facet_counts.facet_fields.title, function(item,i) {
if(1-i%2) {
return item;
}
}));
}
});
},
minLength: 2,
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( " " );
return false;
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="searchbox">Search Box: </label>
<input id="searchbox" />
</div>
<div>
<ul id="results">
</ul>
</div>
</body>
</html>
@ttennebkram
Copy link

Hi John, thanks for posting this.

A reminder to update your blog post that links to it (http://www.opensourceconnections.com/2013/06/07/search-as-you-type-with-solr/), it's still pointing at your old GitHub JohnBerryman username.

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