File size: 8,150 Bytes
8840360 |
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
// DOM elements
const singleModeBtn = document.getElementById('single-mode-btn');
const batchModeBtn = document.getElementById('batch-mode-btn');
const singleInput = document.querySelector('.single-input');
const batchInput = document.querySelector('.batch-input');
const docIdInput = document.getElementById('doc-id');
const batchIdsInput = document.getElementById('batch-ids');
const searchBtn = document.getElementById('search-btn');
const batchSearchBtn = document.getElementById('batch-search-btn');
const loader = document.getElementById('loader');
const resultsContainer = document.getElementById('results-container');
const resultsList = document.getElementById('results-list');
const resultsStats = document.getElementById('results-stats');
const errorMessage = document.getElementById('error-message');
const indexedDocs = document.getElementById('indexed-docs');
const indexedCount = document.getElementById('indexed-count');
const indexedList = document.getElementById('indexed-list');
// Search mode toggle
singleModeBtn.addEventListener('click', () => {
singleModeBtn.classList.add('active');
batchModeBtn.classList.remove('active');
singleInput.style.display = 'block';
batchInput.style.display = 'none';
});
batchModeBtn.addEventListener('click', () => {
batchModeBtn.classList.add('active');
singleModeBtn.classList.remove('active');
batchInput.style.display = 'block';
singleInput.style.display = 'none';
});
// Single document search
searchBtn.addEventListener('click', async () => {
const docId = docIdInput.value.trim();
if (!docId) {
showError('Please enter a document ID');
return;
}
showLoader();
hideError();
try {
const response = await fetch(`/find`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ tsg_doc_id: docId })
});
const data = await response.json();
if (response.ok) {
displaySingleResult(data);
loadIndexedDocuments(); // Refresh indexed docs
} else {
displaySingleNotFound(docId, data.detail);
}
} catch (error) {
showError('Error connecting to the server. Please check if the API is running.');
console.error('Error:', error);
} finally {
hideLoader();
}
});
// Batch document search
batchSearchBtn.addEventListener('click', async () => {
const batchText = batchIdsInput.value.trim();
if (!batchText) {
showError('Please enter at least one document ID');
return;
}
const docIds = batchText.split('\n')
.map(id => id.trim())
.filter(id => id !== '');
if (docIds.length === 0) {
showError('Please enter at least one valid document ID');
return;
}
showLoader();
hideError();
try {
const response = await fetch(`/batch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ tsg_doc_ids: docIds })
});
const data = await response.json();
if (response.ok) {
displayBatchResults(data);
loadIndexedDocuments(); // Refresh indexed docs
} else {
showError('Error processing batch request');
}
} catch (error) {
showError('Error connecting to the server. Please check if the API is running.');
console.error('Error:', error);
} finally {
hideLoader();
}
});
// Display single result
function displaySingleResult(data) {
resultsList.innerHTML = '';
const resultItem = document.createElement('div');
resultItem.className = 'result-item';
resultItem.innerHTML = `
<div class="result-header">
<div class="result-id">${data.tsg_doc_id}</div>
<div class="result-status status-found">Found</div>
</div>
<div class="result-url">
<a href="${data.url}" target="_blank">${data.url}</a>
</div>
`;
resultsList.appendChild(resultItem);
resultsStats.textContent = `Found in ${data.search_time.toFixed(2)} seconds`;
resultsContainer.style.display = 'block';
}
// Display single not found result
function displaySingleNotFound(docId, message) {
resultsList.innerHTML = '';
const resultItem = document.createElement('div');
resultItem.className = 'result-item';
resultItem.innerHTML = `
<div class="result-header">
<div class="result-id">${docId}</div>
<div class="result-status status-not-found">Not Found</div>
</div>
<div>${message}</div>
`;
resultsList.appendChild(resultItem);
resultsStats.textContent = 'Document not found';
resultsContainer.style.display = 'block';
}
// Display batch results
function displayBatchResults(data) {
resultsList.innerHTML = '';
// Found documents
Object.entries(data.results).forEach(([docId, url]) => {
const resultItem = document.createElement('div');
resultItem.className = 'result-item';
resultItem.innerHTML = `
<div class="result-header">
<div class="result-id">${docId}</div>
<div class="result-status status-found">Found</div>
</div>
<div class="result-url">
<a href="${url}" target="_blank">${url}</a>
</div>
`;
resultsList.appendChild(resultItem);
});
// Not found documents
data.missing.forEach(docId => {
const resultItem = document.createElement('div');
resultItem.className = 'result-item';
resultItem.innerHTML = `
<div class="result-header">
<div class="result-id">${docId}</div>
<div class="result-status status-not-found">Not Found</div>
</div>
`;
resultsList.appendChild(resultItem);
});
const foundCount = Object.keys(data.results).length;
const totalCount = foundCount + data.missing.length;
resultsStats.textContent = `Found ${foundCount} of ${totalCount} documents in ${data.search_time.toFixed(2)} seconds`;
resultsContainer.style.display = 'block';
}
// Load indexed documents
async function loadIndexedDocuments() {
try {
const response = await fetch(`/indexed`);
if (response.ok) {
const data = await response.json();
displayIndexedDocuments(data);
}
} catch (error) {
console.error('Error loading indexed documents:', error);
}
}
// Display indexed documents
function displayIndexedDocuments(docIds) {
indexedList.innerHTML = '';
indexedCount.textContent = `${docIds.length} documents`;
if (docIds.length === 0) {
const emptyMessage = document.createElement('p');
emptyMessage.textContent = 'No documents have been indexed yet.';
indexedList.appendChild(emptyMessage);
return;
}
docIds.forEach(docId => {
const docItem = document.createElement('div');
docItem.className = 'indexed-item';
docItem.textContent = docId;
docItem.addEventListener('click', () => {
docIdInput.value = docId;
singleModeBtn.click();
searchBtn.click();
});
indexedList.appendChild(docItem);
});
}
// Show loader
function showLoader() {
loader.style.display = 'block';
}
// Hide loader
function hideLoader() {
loader.style.display = 'none';
}
// Show error message
function showError(message) {
errorMessage.textContent = message;
errorMessage.style.display = 'block';
}
// Hide error message
function hideError() {
errorMessage.style.display = 'none';
}
// Enter key event for single search
docIdInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
searchBtn.click();
}
});
// Load indexed documents on page load
document.addEventListener('DOMContentLoaded', loadIndexedDocuments);
|