2020-07-11 11:48:27 +02:00
|
|
|
var request = null;
|
2020-08-31 13:34:35 +02:00
|
|
|
var timeoutID = null;
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-11-14 14:01:41 +01:00
|
|
|
$('#query, #facet').on('input', function () {
|
|
|
|
var query = $('#query').val();
|
|
|
|
var facet = $('#facet').val();
|
2020-12-28 19:32:56 +01:00
|
|
|
let fetchFacetDistribution = query.trim() !== "" || facet.trim() !== "";
|
2020-08-31 13:34:35 +02:00
|
|
|
var timeoutMs = 100;
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
if (timeoutID !== null) {
|
|
|
|
window.clearTimeout(timeoutID);
|
|
|
|
}
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
timeoutID = window.setTimeout(function () {
|
|
|
|
request = $.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: "query",
|
|
|
|
contentType: 'application/json',
|
2020-12-28 19:32:56 +01:00
|
|
|
data: JSON.stringify({
|
|
|
|
'query': query, 'facetCondition': facet, "facetDistribution": fetchFacetDistribution
|
|
|
|
}),
|
2020-08-31 13:34:35 +02:00
|
|
|
contentType: 'application/json',
|
|
|
|
success: function (data, textStatus, request) {
|
|
|
|
results.innerHTML = '';
|
2020-12-28 19:32:56 +01:00
|
|
|
facets.innerHTML = '';
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
let timeSpent = request.getResponseHeader('Time-Ms');
|
2020-12-28 19:08:53 +01:00
|
|
|
let numberOfDocuments = data.documents.length;
|
2020-12-29 00:50:06 +01:00
|
|
|
count.innerHTML = data.numberOfCandidates.toLocaleString();
|
2020-08-31 13:34:35 +02:00
|
|
|
time.innerHTML = `${timeSpent}ms`;
|
|
|
|
time.classList.remove('fade-in-out');
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-12-28 19:32:56 +01:00
|
|
|
for (facet_name in data.facets) {
|
|
|
|
for (value of data.facets[facet_name]) {
|
|
|
|
const elem = document.createElement('span');
|
|
|
|
elem.classList.add("tag");
|
2020-12-29 00:44:42 +01:00
|
|
|
elem.setAttribute('data-name', facet_name);
|
|
|
|
elem.setAttribute('data-value', value);
|
2020-12-28 19:32:56 +01:00
|
|
|
elem.innerHTML = `${facet_name}:${value}`;
|
|
|
|
facets.appendChild(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:08:53 +01:00
|
|
|
for (element of data.documents) {
|
2020-08-31 13:34:35 +02:00
|
|
|
const elem = document.createElement('li');
|
|
|
|
elem.classList.add("document");
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
const ol = document.createElement('ol');
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
for (const prop in element) {
|
|
|
|
const field = document.createElement('li');
|
|
|
|
field.classList.add("field");
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
const attribute = document.createElement('div');
|
|
|
|
attribute.classList.add("attribute");
|
|
|
|
attribute.innerHTML = prop;
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
const content = document.createElement('div');
|
|
|
|
content.classList.add("content");
|
|
|
|
content.innerHTML = element[prop];
|
|
|
|
|
|
|
|
field.appendChild(attribute);
|
|
|
|
field.appendChild(content);
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
ol.appendChild(field);
|
|
|
|
}
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2020-08-31 13:34:35 +02:00
|
|
|
elem.appendChild(ol);
|
2020-10-19 19:57:15 +02:00
|
|
|
results.appendChild(elem);
|
2020-08-31 13:34:35 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 00:44:42 +01:00
|
|
|
// When we click on a tag we append the facet value
|
|
|
|
// at the end of the facet query.
|
|
|
|
$('#facets .tag').on('click', function () {
|
|
|
|
let name = $(this).attr("data-name");
|
|
|
|
let value = $(this).attr("data-value");
|
|
|
|
|
|
|
|
let facet_query = $('#facet').val().trim();
|
|
|
|
if (facet_query === "") {
|
|
|
|
$('#facet').val(`${name} = "${value}"`).trigger('input');
|
|
|
|
} else {
|
|
|
|
$('#facet').val(`${facet_query} AND ${name} = "${value}"`).trigger('input');
|
|
|
|
}
|
|
|
|
});
|
2020-08-31 13:34:35 +02:00
|
|
|
},
|
|
|
|
beforeSend: function () {
|
|
|
|
if (request !== null) {
|
|
|
|
request.abort();
|
|
|
|
time.classList.add('fade-in-out');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}, timeoutMs);
|
2020-07-11 11:48:27 +02:00
|
|
|
});
|
2020-07-11 14:17:37 +02: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 15:38:05 +02:00
|
|
|
return filesize(parseInt(text))
|
2020-07-11 14:17:37 +02:00
|
|
|
});
|
2020-10-06 14:52:05 +02:00
|
|
|
|
2020-12-29 00:44:42 +01:00
|
|
|
// We trigger the input when we load the script.
|
2020-10-06 14:52:05 +02:00
|
|
|
$(window).on('load', function () {
|
2020-12-29 00:44:42 +01:00
|
|
|
// We execute a placeholder search when the input is empty.
|
2020-11-14 14:01:41 +01:00
|
|
|
$('#query').trigger('input');
|
2020-10-06 14:52:05 +02:00
|
|
|
});
|