我們想自己處理我們的 Hibernate Search 物體之一的索引。
是否可以在 Hibernate Search 6 中為特定物體禁用自動索引?類似于舊的 Hibernate Search 全域設定: hibernate.search.indexing_strategy = manual
我已經搜索了檔案,但沒有看到提到這一點。
uj5u.com熱心網友回復:
hibernate.search.indexing_strategy = manual適用于所有物體型別,而不是特定的物體型別。
您正在尋找的功能已經歸檔為HSEARCH-168,目前計劃用于 Hibernate Search 6.2。
與此同時,我認為你能做的最好的事情就是依靠黑客。它不會像我們為HSEARCH-168設想的那樣有效,但這是一個開始:
- 實作一個基于開關的RoutingBridge,它將完全禁用索引(從索引中添加和洗掉物體)或正常運行,就好像沒有路由橋一樣:
public class ManualIndexingRoutingBinder implements RoutingBinder { private static volatile boolean indexingEnabled = false; public static synchronized void withIndexingEnabled(Runnable runnable) { indexingEnabled = true; try { runnable.run(); } finally { indexingEnabled = false; } } @Override public void bind(RoutingBindingContext context) { context.dependencies() .useRootOnly(); context.bridge( Book.class, new Bridge() ); } public static class Bridge implements RoutingBridge<Book> { @Override public void route(DocumentRoutes routes, Object entityIdentifier, Book indexedEntity, RoutingBridgeRouteContext context) { if ( indexingEnabled ) { routes.addRoute(); } else { routes.notIndexed(); } } @Override public void previousRoutes(DocumentRoutes routes, Object entityIdentifier, Book indexedEntity, RoutingBridgeRouteContext context) { if ( indexingEnabled ) { // So that Hibernate Search will correctly delete entities if necessary. // Only useful if you use SearchIndexingPlan for manual indexing, // as the MassIndexer never deletes documents. routes.addRoute(); } else { routes.notIndexed(); } } } } - 將該路由橋應用于您要控制的物體型別:
@Entity @Indexed(routingBinder = @RoutingBinderRef(type = ManualIndexingRoutingBinder.class)) public class Book { // ... } - 路由橋將有效地禁用自動索引。
- 要手動索引,請執行以下操作:
ManualIndexingRoutingBinder.withIndexingEnabled(() -> { Search.mapping(entityManagerFactory).scope(Book.class) .massIndexer() .startAndWait(); });
我沒有完全測驗這個,所以請報告,但原則上這應該有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515515.html
標籤:爪哇休眠休眠搜索
上一篇:休眠。具有兩個連接的謂詞
