2020-07-11 11:48:27 +02:00
|
|
|
var request = null;
|
2020-08-31 13:34:35 +02:00
|
|
|
var timeoutID = null;
|
2021-03-31 16:27:32 +02:00
|
|
|
var display_facets = false;
|
2020-07-11 11:48:27 +02:00
|
|
|
|
2021-01-07 10:15:31 +01:00
|
|
|
$('#query, #filters').on('input', function () {
|
2020-11-14 14:01:41 +01:00
|
|
|
var query = $('#query').val();
|
2021-01-07 10:15:31 +01:00
|
|
|
var filters = $('#filters').val();
|
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({
|
2021-01-07 10:15:31 +01:00
|
|
|
'query': query,
|
|
|
|
'filters': filters,
|
2021-03-31 16:27:32 +02:00
|
|
|
"facetDistribution": display_facets,
|
2020-12-28 19:32:56 +01:00
|
|
|
}),
|
2020-08-31 13:34:35 +02:00
|
|
|
contentType: 'application/json',
|
|
|
|
success: function (data, textStatus, request) {
|
2021-01-20 13:28:34 +01:00
|
|
|
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) {
|
2021-01-20 13:28:34 +01:00
|
|
|
for (value in data.facets[facet_name]) {
|
|
|
|
const elem = document.createElement('span');
|
|
|
|
const count = data.facets[facet_name][value];
|
|
|
|
elem.classList.add("tag");
|
|
|
|
elem.setAttribute('data-name', facet_name);
|
|
|
|
elem.setAttribute('data-value', value);
|
|
|
|
elem.innerHTML = `${facet_name}:${value} (${count})`;
|
|
|
|
facets.appendChild(elem);
|
2020-12-28 19:32:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2021-08-26 19:12:30 +02:00
|
|
|
|
|
|
|
// Stringify Objects and Arrays to avoid [Object object]
|
|
|
|
if (typeof element[prop] === 'object' && element[prop] !== null) {
|
|
|
|
content.innerHTML = JSON.stringify(element[prop]);
|
|
|
|
} else {
|
|
|
|
content.innerHTML = element[prop];
|
|
|
|
}
|
2020-08-31 13:34:35 +02:00
|
|
|
|
|
|
|
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);
|
2021-01-20 13:28:34 +01:00
|
|
|
results.appendChild(elem);
|
2020-08-31 13:34:35 +02:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:28:34 +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 = $('#filters').val().trim();
|
|
|
|
if (facet_query === "") {
|
|
|
|
$('#filters').val(`${name} = "${value}"`).trigger('input');
|
|
|
|
} else {
|
|
|
|
$('#filters').val(`${facet_query} AND ${name} = "${value}"`).trigger('input');
|
|
|
|
}
|
2020-12-29 00:44:42 +01:00
|
|
|
});
|
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
|
|
|
|
2021-01-07 10:15:31 +01:00
|
|
|
function diffArray(arr1, arr2) {
|
|
|
|
return arr1.concat(arr2).filter(function (val) {
|
|
|
|
if (!(arr1.includes(val) && arr2.includes(val)))
|
|
|
|
return val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectedFacetsToArray(facets_obj) {
|
|
|
|
var array = [];
|
|
|
|
for (const facet_name in facets_obj) {
|
|
|
|
var subarray = [];
|
|
|
|
for (const facet_value of facets_obj[facet_name]) {
|
|
|
|
subarray.push(`${facet_name}:${facet_value}`);
|
|
|
|
}
|
|
|
|
array.push(subarray);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:17:34 +02:00
|
|
|
$('#display_facets').click(function() {
|
|
|
|
if (display_facets) {
|
|
|
|
display_facets = false;
|
|
|
|
$('#display_facets').html("Display facets")
|
|
|
|
$('#display_facets').removeClass("is-danger");
|
|
|
|
$('#display_facets').addClass("is-success");
|
|
|
|
$('#facets').hide();
|
|
|
|
} else {
|
|
|
|
display_facets = true;
|
|
|
|
$('#display_facets').html("Hide facets")
|
|
|
|
$('#display_facets').addClass("is-danger");
|
|
|
|
$('#display_facets').removeClass("is-success");
|
|
|
|
$('#facets').show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|