-
Notifications
You must be signed in to change notification settings - Fork 3
/
solr.js
170 lines (148 loc) · 5.49 KB
/
solr.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
// Requires cors.js to be loaded first
"use strict";
var SOLR_CONFIG = {
"server": "https://arcticdata.io/metacat/d1/mn/v2/query/solr?", // Solr server
"filter": "knb-lter-bnz", // Filter results for an organization or user
"limit": 10, // Max number of results to retrieve per page
"resultsElementId": "searchResults", // Element to contain results
"urlElementId": "searchUrl", // Element to display search URL
"countElementId": "resultCount", // Element showing number of results
"pagesElementId": "pagination", // Element to display result page links
"showPages": 5 // MUST BE ODD NUMBER! Max number of page links to show
};
// Get URL arguments
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Parse Solr search results into HTML
function parseSolrResults(resultJson) {
var docs = resultJson["response"]["docs"];
var html = [];
for (var i = 0; i < docs.length; i++) {
var doc = docs[i];
var names = doc["origin"].join(", ") + " ";
var date = "(Published " + doc["datePublished"].slice(0, 10) + ")";
var link = doc["resourceMap"][0];
if (link.slice(0, 4) === "doi:") {
link = "http://dx.doi.org/" + link.slice(4);
}
var title = '<a rel="external" href="' + link + '" target="_blank">' +
doc["title"].trim() + '</a>';
var row = '<p><span class="dataset-title">' + title +
'</span><br><span class="dataset-author">' + names + date +
'</span></p>';
html.push(row);
}
if (html.length) {
return html.join("\n");
}
else {
return "<p>Your search returned no results.</p>";
}
}
function show_loading(isLoading) {
var x = document.getElementById("loading-div");
if (isLoading) {
document.body.style.cursor = "wait";
x.style.display = "block";
}
else {
document.body.style.cursor = "default";
x.style.display = "none";
}
}
// Function to call if CORS request is successful
function successCallback(headers, response) {
show_loading(false);
// Write results to page
document.getElementById("searchResults").innerHTML = response;
var data = JSON.parse(response);
var resultHtml = parseSolrResults(data);
var elementId = SOLR_CONFIG["resultsElementId"];
document.getElementById(elementId).innerHTML = resultHtml;
// Add links to additional search result pages if necessary
var currentStart = getParameterByName("start");
if (!currentStart) {
currentStart = 0;
}
else {
currentStart = parseInt(currentStart);
}
var count = parseInt(data["response"]["numFound"]);
var limit = parseInt(SOLR_CONFIG["limit"]);
var showPages = parseInt(SOLR_CONFIG["showPages"]);
var pageElementId = SOLR_CONFIG["pagesElementId"];
showPageLinks(count, limit, showPages, currentStart, pageElementId);
var query = getParameterByName("q");
if (query) query = query.trim();
var coreArea = getParameterByName("coreArea");
if (coreArea && coreArea !== "any") {
if (!(query && query.trim())) {
query = coreArea + " core area"
}
else {
query += " in " + coreArea + " core area";
}
}
showResultCount(query, count, limit, currentStart, SOLR_CONFIG["countElementId"]);
}
// Function to call if CORS request fails
function errorCallback() {
show_loading(false);
alert("There was an error making the request.");
}
// Writes CORS request URL to the page so user can see it
function showUrl(url) {
url = encodeURI(url);
var txt = '<a href="' + url + '" target="_blank">' + url + '</a>';
var element = document.getElementById(SOLR_CONFIG["urlElementId"]);
element.innerHTML = txt;
}
// Passes search URL and callbacks to CORS function
function searchSolr(query, coreArea, start) {
var base = SOLR_CONFIG["server"];
var fields = ["title",
"origin",
"datePublished",
"resourceMap"].toString();
var params = "fl=" + fields + "&defType=edismax&wt=json";
var limit = "&rows=" + SOLR_CONFIG["limit"];
if (start === undefined) start = "0";
start = "&start=" + start;
query = "&q=" + SOLR_CONFIG["filter"] + " " + query;
if (coreArea && coreArea !== "any") {
params += '&fq=keywords:"' + coreArea + '"';
}
var url = base + params + limit + start + query;
showUrl(url);
show_loading(true);
makeCorsRequest(url, successCallback, errorCallback);
}
// When the window loads, read query parameters and perform search
window.onload = function() {
var query = getParameterByName("q");
if (query) query = query.trim();
var start = getParameterByName("start");
var coreArea = getParameterByName("coreArea");
document.forms.dataSearchForm.q.value = query;
if (!(query && query.trim())) {
query = ""; // default for empty query
}
var areas = document.getElementById("coreArea");
for (var i=0; i < areas.length; i++) {
if (coreArea == areas[i].value) {
areas[i].selected = true;
break;
}
}
if (!start) {
start = 0;
}
searchSolr(query, coreArea, start);
};