-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSpellcheck.js
More file actions
46 lines (38 loc) · 1.52 KB
/
Spellcheck.js
File metadata and controls
46 lines (38 loc) · 1.52 KB
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
/*
* A Simple JavaScript stage that allows you to leverage embedded spellcheck.
*/
var spellcheck = function(request) {
var IOUtils = org.apache.commons.io.IOUtils;
var JSONObject = org.json.JSONObject;
var JSONArray = org.json.JSONArray;
var URL = java.net.URL;
var String = java.lang.String;
var Base64 = java.util.Base64;
var host = "http://localhost:8764";
var collection = "[COLLECTION_NAME]";
var user = "[USER_NAME]";
var pwd = "[USER_PWD]";
var checkurl = host + "/api/apollo/solr/" + collection + "/spell/?q=";
var query = request.getFirstFieldValue("q");
var auth = new String(user + ":" + pwd);
var encstr = new String(Base64.getEncoder().encodeToString(auth.getBytes()));
var spellurl = checkurl + query;
var url = new URL(spellurl);
var connection = url.openConnection();
connection.setRequestProperty("Authorization", "Basic " + encstr);
connection.connect();
var jsonstr = IOUtils.toString(connection.getInputStream());
var json = JSON.parse(jsonstr);
if (json && json.spellcheck && json.spellcheck.suggestions.length > 0) {
var suggestions = json.spellcheck.suggestions[1].suggestion;
if (suggestions && suggestions.length > 0) {
var newquery = query;
for (var i = 0; i < suggestions.length; i++) {
newquery += " OR " + suggestions[i].word;
}
request.removeParam("q");
request.putSingleParam("q", newquery);
}
}
return request;
}