2020-07-11 17:48:27 +08:00
|
|
|
var request = null;
|
2020-08-31 19:34:35 +08:00
|
|
|
var timeoutID = null;
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-11-14 21:01:41 +08:00
|
|
|
$('#query, #facet').on('input', function () {
|
|
|
|
var query = $('#query').val();
|
|
|
|
var facet = $('#facet').val();
|
2020-12-29 02:32:56 +08:00
|
|
|
let fetchFacetDistribution = query.trim() !== "" || facet.trim() !== "";
|
2020-08-31 19:34:35 +08:00
|
|
|
var timeoutMs = 100;
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
if (timeoutID !== null) {
|
|
|
|
window.clearTimeout(timeoutID);
|
|
|
|
}
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
timeoutID = window.setTimeout(function () {
|
|
|
|
request = $.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: "query",
|
|
|
|
contentType: 'application/json',
|
2020-12-29 02:32:56 +08:00
|
|
|
data: JSON.stringify({
|
|
|
|
'query': query, 'facetCondition': facet, "facetDistribution": fetchFacetDistribution
|
|
|
|
}),
|
2020-08-31 19:34:35 +08:00
|
|
|
contentType: 'application/json',
|
|
|
|
success: function (data, textStatus, request) {
|
|
|
|
results.innerHTML = '';
|
2020-12-29 02:32:56 +08:00
|
|
|
facets.innerHTML = '';
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
let timeSpent = request.getResponseHeader('Time-Ms');
|
2020-12-29 02:08:53 +08:00
|
|
|
let numberOfDocuments = data.documents.length;
|
2020-08-31 19:34:35 +08:00
|
|
|
count.innerHTML = `${numberOfDocuments}`;
|
|
|
|
time.innerHTML = `${timeSpent}ms`;
|
|
|
|
time.classList.remove('fade-in-out');
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-12-29 02:32:56 +08:00
|
|
|
for (facet_name in data.facets) {
|
|
|
|
for (value of data.facets[facet_name]) {
|
|
|
|
const elem = document.createElement('span');
|
|
|
|
elem.classList.add("tag");
|
|
|
|
elem.innerHTML = `${facet_name}:${value}`;
|
|
|
|
facets.appendChild(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 02:08:53 +08:00
|
|
|
for (element of data.documents) {
|
2020-08-31 19:34:35 +08:00
|
|
|
const elem = document.createElement('li');
|
|
|
|
elem.classList.add("document");
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
const ol = document.createElement('ol');
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
for (const prop in element) {
|
|
|
|
const field = document.createElement('li');
|
|
|
|
field.classList.add("field");
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
const attribute = document.createElement('div');
|
|
|
|
attribute.classList.add("attribute");
|
|
|
|
attribute.innerHTML = prop;
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
const content = document.createElement('div');
|
|
|
|
content.classList.add("content");
|
|
|
|
content.innerHTML = element[prop];
|
|
|
|
|
|
|
|
field.appendChild(attribute);
|
|
|
|
field.appendChild(content);
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
ol.appendChild(field);
|
|
|
|
}
|
2020-07-11 17:48:27 +08:00
|
|
|
|
2020-08-31 19:34:35 +08:00
|
|
|
elem.appendChild(ol);
|
2020-10-20 01:57:15 +08:00
|
|
|
results.appendChild(elem);
|
2020-08-31 19:34:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
beforeSend: function () {
|
|
|
|
if (request !== null) {
|
|
|
|
request.abort();
|
|
|
|
time.classList.add('fade-in-out');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}, timeoutMs);
|
2020-07-11 17:48:27 +08:00
|
|
|
});
|
2020-07-11 20:17:37 +08:00
|
|
|
|
|
|
|
// Make the number of document a little bit prettier
|
|
|
|
$('#docs-count').text(function(index, text) {
|
|
|
|
return parseInt(text).toLocaleString()
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the database a little bit easier to read
|
|
|
|
$('#db-size').text(function(index, text) {
|
2020-08-28 21:38:05 +08:00
|
|
|
return filesize(parseInt(text))
|
2020-07-11 20:17:37 +08:00
|
|
|
});
|
2020-10-06 20:52:05 +08:00
|
|
|
|
|
|
|
// We trigger the input when we load the script, this way
|
|
|
|
// we execute a placeholder search when the input is empty.
|
|
|
|
$(window).on('load', function () {
|
2020-11-14 21:01:41 +08:00
|
|
|
$('#query').trigger('input');
|
2020-10-06 20:52:05 +08:00
|
|
|
});
|