914
技術社區[雲棲]
Chapter 6. Manual index changes
Chapter 6. Manual index changes
6.1. Adding instances to the index
Example 6.1. Indexing an entity via FullTextSession.index(T entity)
FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction tx = fullTextSession.beginTransaction(); Object customer = fullTextSession.load( Customer.class, 8 );
fullTextSession.index(customer);
tx.commit(); //index only updated at commit time
如果要添加一個類索引的所有實例,或者所有類索引,推薦的方法是MassIndexer.see Section 6.3.2, “Using a MassIndexer” for more details.
Example 6.2. Purging a specific instance of an entity from the index
FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction tx = fullTextSession.beginTransaction(); for (Customer customer : customers) { fullTextSession.purge( Customer.class, customer.getId() ); } tx.commit(); //index is updated at commit time
Example 6.3. Purging all instances of an entity from the index
FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction tx = fullTextSession.beginTransaction(); fullTextSession.purgeAll( Customer.class ); //optionally optimize the index //fullTextSession.getSearchFactory().optimize( Customer.class ); tx.commit(); //index changes are applied at commit time
FullTextSession.index(T
entity)一樣,配置了
EntityIndexingInterceptor的實體將無法進行操作。詳見
Section 4.5,
“Conditional indexing: to index or not based on entity state”.
Note
FullTextEntityManager也有index
, purge
and purgeAll
等方法
Note
所有手動索引方法 (index
, purge
and purgeAll
)
隻影響索引, 然而它們任然有事務性,隻有committed或者flushToindexes才能完成操作請求。
6.3. Rebuilding the whole index
-
定期使用
FullTextSession
.flushToIndexes()進行索引更新,或者使用
FullTextSession
.index()更新實體。
- 使用MassIndexer.
6.3.1. Using flushToIndexes()
Example 6.4. Index rebuilding using index() and flushToIndexes()
fullTextSession.setFlushMode(FlushMode.MANUAL); fullTextSession.setCacheMode(CacheMode.IGNORE); transaction = fullTextSession.beginTransaction(); //Scrollable results will avoid loading too many objects in memory ScrollableResults results = fullTextSession.createCriteria( Email.class ) .setFetchSize(BATCH_SIZE) .scroll( ScrollMode.FORWARD_ONLY ); int index = 0 ; while( results.next() ) { index++; fullTextSession.index( results.get(0) ); //index each element if (index % BATCH_SIZE == 0) { fullTextSession.flushToIndexes(); //apply changes to indexes fullTextSession.clear(); //free memory since the queue is processed } } transaction.commit();
Warning
MassIndexer期間索引將無法被請求,請求結果可能會丟失。
Example 6.6. Using a tuned MassIndexer
fullTextSession .createIndexer( User.class ) .batchSizeToLoadObjects( 25 ) .cacheMode( CacheMode.NORMAL ) .threadsToLoadObjects( 5 ) .idFetchSize( 150 ) .threadsForSubsequentFetching( 20 ) .progressMonitor( monitor ) //a MassIndexerProgressMonitor implementation .startAndWait();
Tip
效率最高的線程數量取決於你係統的整體結構, 數據庫設計甚至數據的類型. 使用profiler可以幫助找到最佳線程數量: all internal thread groups have meaningful names to be easily identified with most tools.
Note
MassIndexer為了速度而生,且與實務無關,無需begin(),commit(). MassIndexer期間不建議用戶查詢,一來無法查詢到結果,二來增加了係統負載.
hibernate.search.[default|<indexname>].exclusive_index_use
hibernate.search.[default|<indexname>].indexwriter.max_buffered_docs
hibernate.search.[default|<indexname>].indexwriter.max_merge_docs
hibernate.search.[default|<indexname>].indexwriter.merge_factor
hibernate.search.[default|<indexname>].indexwriter.merge_min_size
hibernate.search.[default|<indexname>].indexwriter.merge_max_size
hibernate.search.[default|<indexname>].indexwriter.merge_max_optimize_size
hibernate.search.[default|<indexname>].indexwriter.merge_calibrate_by_deletes
hibernate.search.[default|<indexname>].indexwriter.ram_buffer_size
hibernate.search.[default|<indexname>].indexwriter.term_index_interval
.indexwriter參數都是lucene定義的,hibernate
search隻不過傳遞這些參數。、詳見
Section 3.6,
“Tuning Lucene indexing performance”
最後更新:2017-04-03 18:52:02