-
Notifications
You must be signed in to change notification settings - Fork 8
/
solr-index.js
55 lines (43 loc) · 1.32 KB
/
solr-index.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
var solr = require('solr-client');
var fs = require('fs');
var cheerio = require('cheerio');
var log = require("./log").log;
var client = solr.createClient();
client.deleteByQuery("*:*", function(err, response) {
if (err) throw err;
console.log('Deleted all docs matching query');
client.commit();
reIndex();
});
function reIndex() {
//Loop through files in birds-kb.
//Load content.
//Extract: title
//Extract: keywords
//extract body
var BASE_PATH = "./birds-kb/";
var BASE_LINK = "http://en.wikipedia.org/wiki/";
var files = fs.readdirSync(BASE_PATH);
var counter = 0;
//Loop through files and extract important content
for (var i = 0; i < files.length; i++) {
var data = fs.readFileSync(BASE_PATH + files[i], "utf8");
var doc = cheerio.load(data);
var title = doc("h1").text();
if (title === "") {
log("No title in:" + files[i]);
continue;
}
var articale = {
id: counter++,
title_t: title,
link_t: BASE_LINK + files[i].replace("_data.txt", ""),
body_t: doc("div#bodyContent").text(),
summary_t: doc("div#mw-content-text p").first().html()
};
client.add(articale, done);
}
function done() {
client.commit();
}
}