-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlyra-blog.js
78 lines (64 loc) · 1.41 KB
/
lyra-blog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { create, insert, search } from '@lyrasearch/lyra';
import { createRequire } from 'module'; // to be able to use require at newer node
const require = createRequire(import.meta.url);
/*
* Create Data Schema
*/
const blogDB = create({
schema: {
title: 'string',
file: 'string',
tags: 'string',
createdAt: 'string',
}
});
/*
* Insert/Add Data
*/
const insertBlog = (b) => {
insert(blogDB, {
title: b['blogTitle'],
file: b['blogFile'],
tags: b['blogTags'],
createdAt: b['blogDate']
});
}
// if not already done, get json file 'curl -LkO https://abhishekkr.github.io/datum.json'
var fs = require('fs');
var bloglist = JSON.parse(fs.readFileSync('datum.json', 'utf8'));
bloglist.forEach(
function(data, index) {
insertBlog(data);
}
)
/*
* Search
*/
const showBlog = (s) => {
console.log("Hits: ", s.hits.length)
if (s.hits.length > 0) {
console.log("[first hit]")
console.log(s.hits[0].document);
}
}
const searchPuppet = search(blogDB, {
term: "puppet",
properties: "*",
tolerance: 0,
});
console.log("Search: puppet");
showBlog(searchPuppet);
const searchHowTo = search(blogDB, {
term: "how to",
properties: ["tags"],
tolerance: 1,
});
console.log("Search: how to");
showBlog(searchHowTo);
const searchHowto = search(blogDB, {
term: "howto",
properties: ["tags"],
tolerance: 1,
});
console.log("Search: howto");
showBlog(searchHowto);