Skip to content

Instantly share code, notes, and snippets.

@emolr
Last active September 2, 2015 05:45
Show Gist options
  • Select an option

  • Save emolr/36f86af2964a9374e2a6 to your computer and use it in GitHub Desktop.

Select an option

Save emolr/36f86af2964a9374e2a6 to your computer and use it in GitHub Desktop.
Just clone me and run `npm install && ./node_modules/.bin/bower install`. Example at http://misc.ankr.dk/lunrjs/
bower_components/*
node_modules/*
{
"name": "lunr test",
"dependencies": {
"lunr.js": "v0.5.12"
}
}
var DATA = {
"1": {
"id": "1",
"title": "Empire Burlesque",
"artist": "Bob Dylan",
"country": "USA",
"label": "Columbia",
"price": "10.90",
"year": "1985"
},
"2": {
"id": "2",
"title": "Hide your heart",
"artist": "Bonnie Tyler",
"country": "UK",
"label": "CBS Records",
"price": "9.90",
"year": "1988"
},
"3": {
"id": "3",
"title": "Greatest Hits",
"artist": "Dolly Parton",
"country": "USA",
"label": "RCA",
"price": "9.90",
"year": "1982"
},
"4": {
"id": "4",
"title": "Still got the blues",
"artist": "Gary Moore",
"country": "UK",
"label": "Virgin records",
"price": "10.20",
"year": "1990"
},
"5": {
"id": "5",
"title": "Eros",
"artist": "Eros Ramazzotti",
"country": "EU",
"label": "BMG",
"price": "9.90",
"year": "1997"
},
"6": {
"id": "6",
"title": "One night only",
"artist": "Bee Gees",
"country": "UK",
"label": "Polydor",
"price": "10.90",
"year": "1998"
},
"7": {
"id": "7",
"title": "Sylvias Mother",
"artist": "Dr.Hook",
"country": "UK",
"label": "CBS",
"price": "8.10",
"year": "1973"
},
"8": {
"id": "8",
"title": "Maggie May",
"artist": "Rod Stewart",
"country": "UK",
"label": "Pickwick",
"price": "8.50",
"year": "1990"
},
"9": {
"id": "9",
"title": "Romanza",
"artist": "Andrea Bocelli",
"country": "EU",
"label": "Polydor",
"price": "10.80",
"year": "1996"
},
"10": {
"id": "10",
"title": "When a man loves a woman",
"artist": "Percy Sledge",
"country": "USA",
"label": "Atlantic",
"price": "8.70",
"year": "1987"
},
"11": {
"id": "11",
"title": "Black angel",
"artist": "Savage Rose",
"country": "EU",
"label": "Mega",
"price": "10.90",
"year": "1995"
},
"12": {
"id": "12",
"title": "1999 Grammy Nominees",
"artist": "Many",
"country": "USA",
"label": "Grammy",
"price": "10.20",
"year": "1999"
},
"13": {
"id": "13",
"title": "For the good times",
"artist": "Kenny Rogers",
"country": "UK",
"label": "Mucik Master",
"price": "8.70",
"year": "1995"
},
"14": {
"id": "14",
"title": "Big Willie style",
"artist": "Will Smith",
"country": "USA",
"label": "Columbia",
"price": "9.90",
"year": "1997"
},
"15": {
"id": "15",
"title": "Tupelo Honey",
"artist": "Van Morrison",
"country": "UK",
"label": "Polydor",
"price": "8.20",
"year": "1971"
},
"16": {
"id": "16",
"title": "Soulsville",
"artist": "Jorn Hoel",
"country": "Norway",
"label": "WEA",
"price": "7.90",
"year": "1996"
},
"17": {
"id": "17",
"title": "The very best of",
"artist": "Cat Stevens",
"country": "UK",
"label": "Island",
"price": "8.90",
"year": "1990"
},
"18": {
"id": "18",
"title": "Stop",
"artist": "Sam Brown",
"country": "UK",
"label": "A and M",
"price": "8.90",
"year": "1988"
},
"19": {
"id": "19",
"title": "Bridge of Spies",
"artist": "T'Pau",
"country": "UK",
"label": "Siren",
"price": "7.90",
"year": "1987"
},
"20": {
"id": "20",
"title": "Private Dancer",
"artist": "Tina Turner",
"country": "UK",
"label": "Capitol",
"price": "8.90",
"year": "1983"
},
"21": {
"id": "21",
"title": "Midt om natten",
"artist": "Kim Larsen",
"country": "EU",
"label": "Medley",
"price": "7.80",
"year": "1983"
},
"22": {
"id": "22",
"title": "Pavarotti Gala Concert",
"artist": "Luciano Pavarotti",
"country": "UK",
"label": "DECCA",
"price": "9.90",
"year": "1991"
},
"23": {
"id": "23",
"title": "The dock of the bay",
"artist": "Otis Redding",
"country": "USA",
"label": "Atlantic",
"price": "7.90",
"year": "1987"
},
"24": {
"id": "24",
"title": "Picture book",
"artist": "Simply Red",
"country": "EU",
"label": "Elektra",
"price": "7.20",
"year": "1985"
},
"25": {
"id": "25",
"title": "Red",
"artist": "The Communards",
"country": "UK",
"label": "London",
"price": "7.80",
"year": "1987"
},
"26": {
"id": "26",
"title": "Unchain my heart",
"artist": "Joe Cocker",
"country": "USA",
"label": "EMI",
"price": "8.20",
"year": "1987"
}
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>lunr.js test</title>
<link rel="stylesheet" href="style.css">
<script src="bower_components/lunr.js/lunr.js"></script>
<script src="data.js"></script>
<script src="main.js"></script>
</head>
<body>
<h1>lunr.js test</h1>
<input type="text" id="search" placeholder="search for your favorite music...">
<div id="results">
</div>
<div id="footer">
<a href="https://gist.github.com/ankr/ba47c0bfcfbad668afb5" target="_blank">view the source</a>
</div>
</body>
</html>
// Create our index
var idx = lunr(function() {
this.ref('id');
this.field('title', {boost: 10});
this.field('artist', {boost: 7});
this.field('year', {boost: 2});
this.field('label', {boost: 2});
this.field('country', {boost: 2});
this.field('price', {boost: 1});
});
// Populate index with data
Object.keys(DATA).forEach(function(key) {
if (!DATA.hasOwnProperty(key)) {
return;
}
idx.add(DATA[key]);
});
// Convenience method for searching
var search = function (str) {
var refs = idx.search(str);
return refs.map(function (ref) {
return DATA[ref.ref];
});
};
// Render search results
var render = (function () {
var template = '<div class="result"><b>{title}</b> ({year}) by <u>{artist} ({country})</u> on {label}&trade;</div>';
return function (results) {
var html = results.map(function (res) {
return template.replace(/\{(\w+)\}/g, function (m, g) {
return res[g];
});
}).join('\n');
document.getElementById('results').innerHTML = html;
};
}());
// Listen for user interaction
window.addEventListener('load', function (e) {
document.getElementById('search')
.addEventListener('keyup', function (e) {
var results = search(this.value);
render(results);
}, false);
}, false);
{
"name": "lunrjs-test",
"dependencies": {
"bower": "^1.5.2"
}
}
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
padding: 50px;
}
h1 {
font
}
#search {
font-size: 25px;
height: 45px;
width: 60%;
min-width: 500px;
padding: 5px;
margin: 30px 0 30px 0
}
.result {
padding: 6px;
}
#footer {
position: fixed;
bottom: 20px;
width: 90%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment