mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-17 23:41:28 +00:00
Merge pull request #18 from MrSleeps/fix/targeted-library-removal
Avoid full Meilisearch rebuilds for library removal
This commit is contained in:
@@ -521,6 +521,8 @@ The plugin asks the Python service for the libraries visible to the configured J
|
||||
|
||||
The plugin's library selection is authoritative. Saving the plugin settings posts the selected library IDs to Scene Search; any previously indexed Jellyfin library that is no longer selected is removed from canonical SQLite and Meilisearch. Nothing is removed merely because of a background Jellyfin poll. If a library has been deleted from Jellyfin, it disappears from the checkbox list and is removed the next time the plugin posts its selection.
|
||||
|
||||
Library removal is targeted. **Apply selection only** (or saving the plugin settings) deletes deselected libraries from SQLite and deletes only matching `jellyfin_library_id` documents from Meilisearch; it does not clear/re-feed the complete Meilisearch index. **Index selected libraries** remains the action for scanning media. If that scan finds all retained libraries unchanged, Scene Search now skips the final full Meilisearch rebuild as well.
|
||||
|
||||
Use **Apply library selection / index** to apply the same authoritative set and start a backend indexing job. An empty selection is allowed only after an explicit confirmation and removes all Jellyfin-backed Scene Search data. The plugin shows an overall percentage/progress bar across all selected libraries as well as the current library, item and frame progress, and polls until the job completes. When ingestion finishes, the backend automatically rebuilds Meilisearch, so newly indexed vectors are searchable without a separate `sync-meili` command.
|
||||
|
||||
The same page also has **Start / resume captioning** and **Pause**. Pause finishes the currently active routing batch or Ollama frame request, then waits before starting the next unit; Resume continues the same in-memory job without rebuilding its queues. Every successful caption is committed immediately, so a process/container interruption can still be recovered by pressing Start / resume and continuing from uncaptained frames. **Force recaption existing frames** deliberately regenerates captions, but reuses saved routing decisions while the routing policy is still current.
|
||||
|
||||
@@ -46,13 +46,16 @@
|
||||
|
||||
<h2>Indexed Libraries</h2>
|
||||
<div class="fieldDescription" style="margin-bottom:1em;">
|
||||
Choose which Jellyfin libraries Scene Search should keep indexed. Applying this selection removes deselected libraries from Scene Search SQLite and Meilisearch. Selection is stored in this plugin, not in the Python .env file.
|
||||
Choose which Jellyfin libraries Scene Search should keep indexed. Apply selection only removes deselected libraries directly from Scene Search SQLite and Meilisearch without rebuilding the full Meilisearch index. Selection is stored in this plugin, not in the Python .env file.
|
||||
</div>
|
||||
<div id="libraryList">Loading libraries…</div>
|
||||
|
||||
<div style="margin-top:1em; margin-bottom:1em;">
|
||||
<button is="emby-button" type="button" id="applyLibraries" class="raised">
|
||||
<span>Apply selection only</span>
|
||||
</button>
|
||||
<button is="emby-button" type="button" id="indexNow" class="raised">
|
||||
<span>Apply library selection / index</span>
|
||||
<span>Index selected libraries</span>
|
||||
</button>
|
||||
<label class="checkboxContainer" style="margin-top:1em;">
|
||||
<input is="emby-checkbox" type="checkbox" id="forceIndex" />
|
||||
@@ -367,6 +370,45 @@
|
||||
}
|
||||
});
|
||||
|
||||
page.querySelector('#applyLibraries').addEventListener('click', function () {
|
||||
var ids = selectedLibraryIds();
|
||||
if (!ids.length && !window.confirm(
|
||||
'No libraries are selected. This will remove all Jellyfin-backed Scene Search data from SQLite and Meilisearch. Continue?'
|
||||
)) {
|
||||
return;
|
||||
}
|
||||
|
||||
page.querySelector('#indexStatus').textContent = 'Applying library selection...';
|
||||
|
||||
ApiClient.getPluginConfiguration(pluginId).then(function (config) {
|
||||
config.SelectedLibraryIds = ids;
|
||||
currentConfig = config;
|
||||
return ApiClient.updatePluginConfiguration(pluginId, config);
|
||||
}).then(function () {
|
||||
return ApiClient.ajax({
|
||||
type: 'POST',
|
||||
url: ApiClient.getUrl('Plugins/SceneSearch/LibrarySelection'),
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify({
|
||||
library_ids: ids,
|
||||
force: false
|
||||
})
|
||||
});
|
||||
}).then(function (result) {
|
||||
var removedLibraries = Number(result.removed_libraries || 0);
|
||||
var removedMedia = Number(result.removed_media || 0);
|
||||
page.querySelector('#indexStatus').textContent = removedLibraries
|
||||
? 'Selection applied: removed ' + removedLibraries +
|
||||
' librar' + (removedLibraries === 1 ? 'y' : 'ies') +
|
||||
' and ' + removedMedia + ' media items. No full search-index rebuild.'
|
||||
: 'Selection applied: nothing to remove.';
|
||||
loadLibraries();
|
||||
}).catch(function () {
|
||||
page.querySelector('#indexStatus').textContent =
|
||||
'Could not apply library selection. Check Jellyfin logs and Scene Search API status.';
|
||||
});
|
||||
});
|
||||
|
||||
page.querySelector('#indexNow').addEventListener('click', function () {
|
||||
var ids = selectedLibraryIds();
|
||||
if (!ids.length && !window.confirm(
|
||||
|
||||
@@ -254,6 +254,10 @@ class IndexJobManager:
|
||||
force=force,
|
||||
progress=self._progress,
|
||||
)
|
||||
retained_index_changed = (
|
||||
int(summary.get("indexed") or 0) > 0
|
||||
or int(summary.get("pruned") or 0) > 0
|
||||
)
|
||||
summary["pruned"] = (
|
||||
int(summary.get("pruned") or 0)
|
||||
+ int(removed.get("removed_media") or 0)
|
||||
@@ -261,8 +265,16 @@ class IndexJobManager:
|
||||
summary["removed_libraries"] = int(
|
||||
removed.get("removed_libraries") or 0
|
||||
)
|
||||
self._progress({"stage": "syncing", **summary})
|
||||
documents = rebuild_meili(progress=self._progress)
|
||||
|
||||
if retained_index_changed:
|
||||
self._progress({"stage": "syncing", **summary})
|
||||
documents = rebuild_meili(progress=self._progress)
|
||||
else:
|
||||
documents = 0
|
||||
print(
|
||||
"Selected libraries are unchanged; "
|
||||
"skipping full Meilisearch rebuild."
|
||||
)
|
||||
|
||||
self._progress(
|
||||
{
|
||||
@@ -270,6 +282,7 @@ class IndexJobManager:
|
||||
"stage": "complete",
|
||||
"finished_at": datetime.now(timezone.utc).isoformat(),
|
||||
"documents_processed": documents,
|
||||
"full_meili_rebuild": retained_index_changed,
|
||||
**summary,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -553,6 +553,7 @@ def _index_library(
|
||||
indexed = 0
|
||||
skipped = 0
|
||||
failed = 0
|
||||
pruned = 0
|
||||
current_item_ids: set[str] = set()
|
||||
|
||||
if progress is not None:
|
||||
@@ -597,6 +598,7 @@ def _index_library(
|
||||
).fetchone()
|
||||
if existing:
|
||||
con.execute("DELETE FROM media WHERE id = ?", (existing["id"],))
|
||||
pruned += 1
|
||||
shutil.rmtree(
|
||||
settings.thumbs_dir / jellyfin_media_id(item_id),
|
||||
ignore_errors=True,
|
||||
@@ -823,11 +825,11 @@ def _index_library(
|
||||
f"{summary.get('Name') or summary.get('Id')}: {exc}"
|
||||
)
|
||||
|
||||
pruned = 0
|
||||
if limit is None:
|
||||
pruned = _reconcile_library(library_id, current_item_ids)
|
||||
if pruned:
|
||||
print(f" pruned {pruned} stale item(s) no longer present in Jellyfin")
|
||||
reconciled = _reconcile_library(library_id, current_item_ids)
|
||||
pruned += reconciled
|
||||
if reconciled:
|
||||
print(f" pruned {reconciled} stale item(s) no longer present in Jellyfin")
|
||||
else:
|
||||
print(" limited run: stale-item reconciliation skipped")
|
||||
|
||||
|
||||
@@ -72,10 +72,11 @@
|
||||
<button id="refreshLibraries" type="button" class="secondary">Refresh</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="muted" style="margin-bottom:8px">Deselected libraries are removed from Scene Search SQLite and Meilisearch when you apply the selection.</div>
|
||||
<div class="muted" style="margin-bottom:8px">Apply selection removes deselected libraries directly from SQLite and Meilisearch. It does not rebuild the whole search index.</div>
|
||||
<div id="libraries" class="muted">Loading…</div>
|
||||
<div class="row" style="margin-top:14px">
|
||||
<button id="indexNow" type="button">Apply library selection / index</button>
|
||||
<button id="applyLibraries" type="button" class="secondary">Apply selection only</button>
|
||||
<button id="indexNow" type="button">Index selected libraries</button>
|
||||
<label><input id="forceIndex" type="checkbox"> Force rebuild</label>
|
||||
</div>
|
||||
</section>
|
||||
@@ -239,6 +240,37 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function applyLibrarySelection() {
|
||||
const ids = selectedLibraries();
|
||||
if (!ids.length && !window.confirm(
|
||||
'No libraries are selected. This will remove all Jellyfin-backed Scene Search data from SQLite and Meilisearch. Continue?'
|
||||
)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$('indexStatus').className = 'status';
|
||||
$('indexStatus').textContent = 'Applying library selection...';
|
||||
const result = await api('/api/jellyfin/libraries/selection', {
|
||||
method:'POST',
|
||||
body:JSON.stringify({library_ids:ids, force:false})
|
||||
});
|
||||
const removedLibraries = Number(result.removed_libraries || 0);
|
||||
const removedMedia = Number(result.removed_media || 0);
|
||||
$('indexStatus').className = 'status ok';
|
||||
$('indexStatus').textContent = removedLibraries
|
||||
? 'Selection applied: removed ' + removedLibraries + ' librar' +
|
||||
(removedLibraries === 1 ? 'y' : 'ies') + ' and ' + removedMedia + ' media items'
|
||||
: 'Selection applied: nothing to remove';
|
||||
$('indexDetail').textContent = 'No full Meilisearch rebuild was run.';
|
||||
await loadLibraries();
|
||||
refreshHealth();
|
||||
} catch (e) {
|
||||
$('indexStatus').className = 'status error';
|
||||
$('indexStatus').textContent = e.message;
|
||||
}
|
||||
}
|
||||
|
||||
async function startIndex() {
|
||||
const ids = selectedLibraries();
|
||||
if (!ids.length && !window.confirm(
|
||||
@@ -436,6 +468,7 @@
|
||||
};
|
||||
$('refreshLibraries').onclick = loadLibraries;
|
||||
$('selectAll').onclick = () => document.querySelectorAll('.libraryCheck').forEach(x => x.checked = true);
|
||||
$('applyLibraries').onclick = applyLibrarySelection;
|
||||
$('indexNow').onclick = startIndex;
|
||||
$('captionNow').onclick = startCaption;
|
||||
$('captionPause').onclick = toggleCaptionPause;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from scene_search.index_jobs import IndexJobManager
|
||||
|
||||
@@ -52,6 +54,115 @@ class OverallIndexProgressTests(unittest.TestCase):
|
||||
self.assertEqual(status["overall_items_completed"], 10)
|
||||
self.assertEqual(status["overall_percent"], 99.0)
|
||||
|
||||
def test_library_only_removal_skips_full_meili_rebuild(self) -> None:
|
||||
meili = MagicMock()
|
||||
service = SimpleNamespace(
|
||||
model_lock=threading.Lock(),
|
||||
get_embedder=lambda: object(),
|
||||
meili=meili,
|
||||
)
|
||||
manager = IndexJobManager(service) # type: ignore[arg-type]
|
||||
manager._state = {
|
||||
"running": True,
|
||||
"stage": "starting",
|
||||
"library_item_totals": [2],
|
||||
"overall_items_total": 2,
|
||||
}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"scene_search.index_jobs.reconcile_selected_libraries",
|
||||
return_value={
|
||||
"removed_library_ids": ["lib-removed"],
|
||||
"removed_libraries": 1,
|
||||
"removed_media": 50,
|
||||
},
|
||||
),
|
||||
patch(
|
||||
"scene_search.index_jobs.index_libraries",
|
||||
return_value=(
|
||||
object(),
|
||||
{
|
||||
"library_total": 1,
|
||||
"indexed": 0,
|
||||
"skipped": 2,
|
||||
"failed": 0,
|
||||
"pruned": 0,
|
||||
},
|
||||
),
|
||||
),
|
||||
patch("scene_search.index_jobs.build_jellyfin_client"),
|
||||
patch("scene_search.index_jobs.rebuild_meili") as rebuild,
|
||||
):
|
||||
manager._run(
|
||||
[{"Id": "lib-keep", "Name": "Keep"}],
|
||||
{"lib-keep"},
|
||||
False,
|
||||
)
|
||||
|
||||
meili.delete_jellyfin_libraries.assert_called_once_with(
|
||||
["lib-removed"]
|
||||
)
|
||||
rebuild.assert_not_called()
|
||||
status = manager.status()
|
||||
self.assertEqual(status["stage"], "complete")
|
||||
self.assertFalse(status["full_meili_rebuild"])
|
||||
self.assertEqual(status["documents_processed"], 0)
|
||||
self.assertEqual(status["pruned"], 50)
|
||||
|
||||
def test_retained_library_change_still_rebuilds_meili(self) -> None:
|
||||
service = SimpleNamespace(
|
||||
model_lock=threading.Lock(),
|
||||
get_embedder=lambda: object(),
|
||||
meili=MagicMock(),
|
||||
)
|
||||
manager = IndexJobManager(service) # type: ignore[arg-type]
|
||||
manager._state = {
|
||||
"running": True,
|
||||
"stage": "starting",
|
||||
"library_item_totals": [2],
|
||||
"overall_items_total": 2,
|
||||
}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"scene_search.index_jobs.reconcile_selected_libraries",
|
||||
return_value={
|
||||
"removed_library_ids": [],
|
||||
"removed_libraries": 0,
|
||||
"removed_media": 0,
|
||||
},
|
||||
),
|
||||
patch(
|
||||
"scene_search.index_jobs.index_libraries",
|
||||
return_value=(
|
||||
object(),
|
||||
{
|
||||
"library_total": 1,
|
||||
"indexed": 1,
|
||||
"skipped": 1,
|
||||
"failed": 0,
|
||||
"pruned": 0,
|
||||
},
|
||||
),
|
||||
),
|
||||
patch("scene_search.index_jobs.build_jellyfin_client"),
|
||||
patch(
|
||||
"scene_search.index_jobs.rebuild_meili",
|
||||
return_value=123,
|
||||
) as rebuild,
|
||||
):
|
||||
manager._run(
|
||||
[{"Id": "lib-keep", "Name": "Keep"}],
|
||||
{"lib-keep"},
|
||||
False,
|
||||
)
|
||||
|
||||
rebuild.assert_called_once()
|
||||
status = manager.status()
|
||||
self.assertTrue(status["full_meili_rebuild"])
|
||||
self.assertEqual(status["documents_processed"], 123)
|
||||
|
||||
def test_complete_is_one_hundred_percent(self) -> None:
|
||||
manager = self.make_manager()
|
||||
manager._state = {
|
||||
|
||||
Reference in New Issue
Block a user