Skip to content

Commit 0bc9003

Browse files
committed
Added lunr test and updated package.json
1 parent b87f81d commit 0bc9003

File tree

7 files changed

+3247
-18
lines changed

7 files changed

+3247
-18
lines changed

lunr/shop/css/main.css

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
input#query {
2+
font-size: 16px;
3+
font-weight: 300;
4+
padding: 3px;
5+
width: 250px;
6+
}
7+
8+
label[for=query] {
9+
margin: 0 10px 0 0;
10+
}

lunr/shop/data/index.json

+1
Large diffs are not rendered by default.

lunr/shop/index.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<title>Lunr shop full text search demo</title>
3333

3434
<link rel="stylesheet" href="../../css/main.css" />
35+
<link rel="stylesheet" href="css/main.css" />
3536

3637
</head>
3738

@@ -41,15 +42,17 @@
4142

4243
<h1><a href="../index.html" title="simpl.info home page">simpl.info</a> Lunr shop</h1>
4344

44-
<p>View the console to see logging.</p>
45+
<label for="query">Search:</label><input id="query"></input>
46+
47+
<ul id="matches"></ul>
4548

4649
<a href="https://github.com/samdutton/simpl/blob/gh-pages/lunr" title="View source for this page on GitHub" id="viewSource">View source on GitHub</a>
4750

4851
</div>
4952

5053

5154
<!-- <script src="js/lib/lunr.min.js"></script> -->
52-
<script src="elasticlunr.min.js"></script>
55+
<script src="js/lib/elasticlunr.min.js"></script>
5356
<script src="js/main.js"></script>
5457
<script src="../../js/lib/ga.js"></script>
5558

lunr/shop/js/index.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ limitations under the License.
2020
// node js/index.js < data/data.json > data/index.json
2121

2222
// const lunr = require('lunr');
23-
const lunr = require('elasticlunr');
23+
// const lunr = require('elasticlunr');
24+
const elasticlunr = require('elasticlunr');
2425
const stdin = process.stdin;
2526
const stdout = process.stdout;
2627
const buffer = [];
@@ -44,16 +45,15 @@ stdin.on('end', () => {
4445
// }
4546
// });
4647

47-
const idx = elasticlunr(function() { // can't seem to use fat arrow :/
48-
this.setRef('name');
49-
this.addField('title');
50-
this.addField('description');
51-
for (let doc of documents) {
52-
this.addDoc(doc);
48+
const idx = elasticlunr(function() { // can't seem to use fat arrow :/
49+
this.addField('title');
50+
this.addField('description');
51+
this.setRef('name');
52+
// this.saveDocument(false);
53+
for (let doc of documents) {
54+
this.addDoc(doc);
5355
}
5456
});
5557

56-
57-
5858
stdout.write(JSON.stringify(idx));
5959
});

lunr/shop/js/main.js

+43-7
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,57 @@ limitations under the License.
1616

1717
'use strict';
1818

19-
/* global lunr */
19+
/* global elasticlunr, lunr */
20+
21+
const queryInput = document.getElementById('query');
22+
const matchList = document.getElementById('matches');
23+
24+
var index;
2025

2126
fetch('data/index.json').then(response => {
2227
return response.json();
2328
}).then(json => {
2429
startPerf();
25-
const index = lunr.Index.load(json);
30+
index = elasticlunr.Index.load(json);
2631
endPerf();
2732
logPerf('Index loading');
33+
// un-disable search
34+
});
35+
36+
queryInput.focus();
37+
queryInput.oninput = function() {
38+
matchList.innerHTML = '';
39+
const query = queryInput.value;
40+
if (query.length < 2) {
41+
return;
42+
}
2843
startPerf();
29-
window.matches = index.search('crew');
30-
endPerf();
31-
logPerf('Search');
44+
const options = {
45+
fields: {
46+
title: {boost: 2},
47+
description: {boost: 1}
48+
},
49+
bool: "OR",
50+
expand: true
51+
};
52+
const matches = window.matches = index.search(query, options);
53+
endPerf();
54+
logPerf('Search');
55+
displayMatches(matches);
56+
}
57+
58+
function displayMatches(matches) {
59+
if (matches.length === 0) {
60+
matchList.innerHTML = "No matches";
61+
return;
62+
}
3263
console.log('Matches: ', matches);
33-
});
64+
for (let match of matches) {
65+
matchList.innerHTML += '<li>' + match.ref + '</li>';
66+
}
67+
}
68+
69+
// window.performance utilities
3470

3571
function startPerf() {
3672
window.performance.mark('start');
@@ -44,6 +80,6 @@ function logPerf(message) {
4480
window.performance.clearMeasures();
4581
window.performance.measure('duration', 'start', 'end');
4682
const duration =
47-
performance.getEntriesByName('duration')[0].duration.toPrecision(4);
83+
performance.getEntriesByName('duration')[0].duration.toPrecision(4);
4884
console.log(`${message} took ${duration} ms`);
4985
}

0 commit comments

Comments
 (0)