Skip to content

Instantly share code, notes, and snippets.

@rpgraham84
Created June 10, 2015 16:52
Show Gist options
  • Save rpgraham84/79fdfe28c064f67b816d to your computer and use it in GitHub Desktop.
Save rpgraham84/79fdfe28c064f67b816d to your computer and use it in GitHub Desktop.
var Search = function (window) {
var data = {};
function getSearchResults (p) {
var params = {
// If no tokens are passed in, use the value of the search input.
tokens: document.querySelector('#id_q').value.split(' '),
// Facets are currently not tacked anywhere on the page.
selectedFacets: []
}, queryString, key, value, k;
// Override parameters with the object passed in.
for (k in p) {
params[k] = p[k];
}
// Build query portion from tokens.
queryString = 'q=' + params.tokens.join('+');
// Add each selected facet to the query string.
for (key in params.selectedFacets) {
value = params.selectedFacets[key];
queryString = queryString.concat('&selected_facets=' + key + ':' + value);
}
// Make a request using the query string
$.ajax({
dataType: "json",
url: '/search/?' + queryString,
async: false,
context: data,
success: function (d) {
data = d.map(function (e) {
if (e.type_ === "Product") {
// images, downloads, attributes, and features all come
// back from the server as JSON strings that need to
// be parsed for the time being. This will probably change
// at some point.
e.images = JSON.parse(e.images);
e.downloads = JSON.parse(e.downloads);
e.features = JSON.parse(e.features);
e.attributes = JSON.parse(e.attributes);
}
return e;
});
this.data = data;
}
});
return data
}
return {
getSearchResults: getSearchResults
}
};
var search = Search({});
// Search on the value already present in the search bar
var results = search.getSearchResults();
console.log("Search based on search bar contents: ", results);
// All of the searchable things
var results = search.getSearchResults({tokens:[], selectedFacets: []});
console.log("All of the searchable things: ", results);
// All of the articles
results = search.getSearchResults({
'selectedFacets': {
'type__exact': 'Article'
}
});
console.log("Articles: ", results);
// Example of searching for anything with 'Wolf Range' in the corpus text
results = search.getSearchResults({
'tokens': ['Wolf', 'Range']
});
console.log("Anything with 'Wolf Range' in the corpus text: ", results);
// Example of searching for only products with 'Wolf Range' in the corpus text
results = search.getSearchResults({
'tokens': ['Wolf', 'Range'],
'selectedFacets': {
'type__exact': 'Product'
}
});
console.log("Products with 'Wolf Range' in the corpus text: ", results);
// Example of searching by facet for anything tagged 'Grill'
results = search.getSearchResults({
'selectedFacets': {
'tags_exact': 'Grill'
}
});
console.log("Anything tagged 'Grill': ", results);
// Just the cooktops (strict search by product class)
results = search.getSearchResults({
'selectedFacets': {
'product_class': 'Cooktop'
}
});
console.log("Cooktops only: ", results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment