914
windows
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