-
Notifications
You must be signed in to change notification settings - Fork 5
/
fusion_xml_crawler.js
257 lines (215 loc) · 10.1 KB
/
fusion_xml_crawler.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var fusionWebCrawler = function(doc){
var parsedDocs = java.util.ArrayList;
var jsoupXmlParser = function(doc){
var Jsoup = org.jsoup.Jsoup;
var jdoc = org.jsoup.nodes.Document;
var ex = java.lang.Exception;
var Parser = org.jsoup.parser.Parser;
var element = org.jsoup.Element;
var xmlstr = java.lang.String;
var docs = java.util.ArrayList;
var outdocs = java.util.ArrayList;
var pipelineDoc = com.lucidworks.apollo.common.pipeline.PipelineDocument;
var docurl = java.lang.String;
var elements = org.jsoup.select.Elements;
var extractedText = java.lang.String;
var ele = org.jsoup.Element;
var client = org.apache.http.client.HttpClient;
var DefaultHttpClient = org.apache.http.impl.client.DefaultHttpClient;
var ClientConnectionManager = org.apache.http.conn.ClientConnectionManager;
var PoolingClientConnectionManager = org.apache.http.impl.conn.PoolingClientConnectionManager;
var CloudSolrClient = org.apache.solr.client.solrj.impl.CloudSolrClient;
try{
docs = new java.util.ArrayList();
xmlstr = doc.getFirstFieldValue("body");
jdoc = Jsoup.parse(xmlstr, "", Parser.xmlParser());
for each(element in jdoc.select("loc")) {
docurl = element.ownText();
if(docurl !== null && docurl !== ""){
logger.info("Parsed URL: "+element.ownText());
pipelineDoc = new com.lucidworks.apollo.common.pipeline.PipelineDocument(element.ownText());
docs.add(pipelineDoc);
}
}
outdocs = new java.util.ArrayList();
// now crawl each doc in the feed
for each(pipelineDoc in docs){
docurl = pipelineDoc.getId();
jdoc = Jsoup.connect(docurl).get();
if(jdoc !== null){
logger.info("FOUND a JSoup document for url "+doc.getId());
extractedText = new java.lang.String();
elements = jdoc.select("p");
logger.info("ITERATE OVER ELEMENTS");
// then parse elements and pull just the text
for each (ele in elements) {
if (ele !== null) {
if (ele.ownText() !== null) {
extractedText += ele.ownText();
}
}
}
pipelineDoc.addField('extracted_text', extractedText);
logger.info("Extracted: "+extractedText);
outdocs.add(pipelineDoc);
} else {
logger.warn("Jsoup Document was NULL **** ");
}
}
}catch(ex){
logger.error(ex);
}
return outdocs;
};
parsedDocs = jsoupXmlParser(doc);
logger.info(" SUBMITTING "+parsedDocs.size()+" to solr index... ****** ");
var solrCloudClient = function(doc){
var client = org.apache.http.client.HttpClient;
var cloudServer = org.apache.solr.client.solrj.impl.CloudSolrClient;
var DefaultHttpClient = org.apache.http.impl.client.DefaultHttpClient;
var ClientConnectionManager = org.apache.http.conn.ClientConnectionManager;
var PoolingClientConnectionManager = org.apache.http.impl.conn.PoolingClientConnectionManager;
var CloudSolrClient = org.apache.solr.client.solrj.impl.CloudSolrClient;
var cm = org.apache.http.impl.conn.PoolingClientConnectionManager;
var String = java.lang.String;
var pdoc = com.lucidworks.apollo.common.pipeline.PipelineDocument;
var ZOOKEEPER_URL = new String("localhost:9983");
var DEFAULT_COLLECTION = new String("cityofsacramento");
var server = ZOOKEEPER_URL;
var collection = DEFAULT_COLLECTION;
var docList = java.util.ArrayList;
var inputDoc = org.apache.solr.common.SolrInputDocument;
var pingResp = org.apache.solr.client.solrj.response.SolrPingResponse;
var res = org.apache.solr.client.solrj.response.UpdateResponse;
var SolrInputDocument = org.apache.solr.common.SolrInputDocument;
var UUID = java.util.UUID;
try{
// PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
cm = new PoolingClientConnectionManager();
client = new DefaultHttpClient(cm);
cloudServer = new CloudSolrClient(server, client);
cloudServer.setDefaultCollection(collection);
logger.info("CLOUD SERVER INIT OK...");
docList = new java.util.ArrayList();
pingResp = cloudServer.ping();
logger.info(pingResp);
docList = new java.util.ArrayList();
for each(pdoc in doc){
inputDoc = new SolrInputDocument();
inputDoc.addField("id", UUID.randomUUID().toString());
inputDoc.addField("q_txt", pdoc.getFirstFieldValue("extracted_text"));
docList.add(inputDoc);
}
logger.info(" DO SUBMIT OF "+docList.size()+" DOCUMENTS TO SOLR **** ");
cloudServer.add(docList);
res = cloudServer.commit();
logger.info(res);
}catch(ex){
logger.error(ex);
}
return doc;
};
solrCloudClient(parsedDocs);
logger.info("RSS CRAWL COMPLETE...");
return doc;
};
// V 1.0
var fusionWebCrawler_1_0 = function(doc){
var Jsoup = org.jsoup.Jsoup;
var jdoc = org.jsoup.nodes.Document;
var ex = java.lang.Exception;
var Parser = org.jsoup.parser.Parser;
var element = org.jsoup.Element;
var xmlstr = java.lang.String;
var docs = java.util.ArrayList;
var outdocs = java.util.ArrayList;
var pipelineDoc = com.lucidworks.apollo.common.pipeline.PipelineDocument;
var docurl = java.lang.String;
var elements = org.jsoup.select.Elements;
var extractedText = java.lang.String;
var ele = org.jsoup.Element;
try{
docs = new java.util.ArrayList();
xmlstr = doc.getFirstFieldValue("body");
jdoc = Jsoup.parse(xmlstr, "", Parser.xmlParser());
for each(element in jdoc.select("loc")) {
docurl = element.ownText();
if(docurl !== null && docurl !== ""){
logger.info("Parsed URL: "+element.ownText());
pipelineDoc = new com.lucidworks.apollo.common.pipeline.PipelineDocument(element.ownText());
docs.add(pipelineDoc);
}
}
outdocs = new java.util.ArrayList;
// now crawl each doc in the feed
for each(pipelineDoc in docs){
docurl = pipelineDoc.getId();
jdoc = Jsoup.connect(docurl).get();
if(jdoc !== null){
logger.info("FOUND a JSoup document for url "+doc.getId());
extractedText = new java.lang.String();
elements = jdoc.select("p");
logger.info("ITERATE OVER ELEMENTS");
// then parse elements and pull just the text
for each (ele in elements) {
if (ele !== null) {
if (ele.ownText() !== null) {
extractedText += ele.ownText();
}
}
}
pipelineDoc.addField('content_text', extractedText);
// pipelineDoc.addField("_raw_content_", jdoc.toString());
pipelineDoc.addMetadata("Content-Type", "text/html");
logger.info("Extracted: "+extractedText);
outdocs.add(pipelineDoc);
} else {
logger.warn("Jsoup Document was NULL **** ");
}
}
}catch(ex){
logger.error(ex);
}
return outdocs;
}
// solr cloud client
var solrCloudClient = function(doc){
var client = org.apache.http.client.HttpClient;
var cloudServer = org.apache.solr.client.solrj.impl.CloudSolrClient;
var DefaultHttpClient = org.apache.http.impl.client.DefaultHttpClient;
var ClientConnectionManager = org.apache.http.conn.ClientConnectionManager;
var PoolingClientConnectionManager = org.apache.http.impl.conn.PoolingClientConnectionManager;
var CloudSolrClient = org.apache.solr.client.solrj.impl.CloudSolrClient;
var cm = org.apache.http.impl.conn.PoolingClientConnectionManager;
var String = java.lang.String;
var ZOOKEEPER_URL = new String("localhost:9983");
var DEFAULT_COLLECTION = new String("cityofsacramento");
var server = ZOOKEEPER_URL;
var collection = DEFAULT_COLLECTION;
var docList = java.util.ArrayList;
var inputDoc = org.apache.solr.common.SolrInputDocument;
var pingResp = org.apache.solr.client.solrj.response.SolrPingResponse;
var res = org.apache.solr.client.solrj.response.UpdateResponse;
try{
// PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
cm = new PoolingClientConnectionManager();
client = new DefaultHttpClient(cm);
cloudServer = new CloudSolrClient(server, client);
cloudServer.setDefaultCollection(collection);
logger.info("CLOUD SERVER INIT OK...");
docList = new java.util.ArrayList();
pingResp = cloudServer.ping();
logger.info(pingResp);
cloudServer.add(docList);
res = cloudServer.commit();
logger.info(res);
}catch(ex){
logger.error(ex);
}
return doc;
};