阅读更多
当重建索引时,先建备份索引,然后通过操作替换原索引文件.compass的一个解决方案是
把备份索引replace操作原索引文件,方法是
getSearchEngineIndexManager().replaceIndex(SearchEngineIndexManager,SearchEngineIndexManager)
简单code
indexCompass = compass.clone(
new CompassSettings().setSetting(CompassEnvironment.CONNECTION_SUB_CONTEXT, "gpsindex"));
indexCompass.getSearchEngineIndexManager().deleteIndex();
indexCompass.getSearchEngineIndexManager().createIndex();
indexCompassTemplate = new CompassTemplate(indexCompass);
indexCompass.getSearchEngineIndexManager().clearCache();
compass.getSearchEngineIndexManager().replaceIndex(
indexCompass.getSearchEngineIndexManager(), new SearchEngineIndexManager.ReplaceIndexCallback() {
public void buildIndexIfNeeded() throws SearchEngineException {
for (Iterator it = devices.values().iterator(); it.hasNext();) {
// Here we build the new index (based on indexCompass)
CompassGpsDevice device = (CompassGpsDevice) it.next();
device.index();
}
}
});
indexCompass.getSearchEngineIndexManager().deleteIndex();
indexCompass.close();
indexCompass是创建备份索引,compass是原索引的compass
还有根据DualCompassGps的code
boolean stoppedMirrorCompassOptimizer = false;
boolean stoppedIndexCompassOptimizer = false;
if (mirrorCompass != null && mirrorCompass.getSearchEngineOptimizer().isRunning()) {
mirrorCompass.getSearchEngineOptimizer().stop();
stoppedMirrorCompassOptimizer = true;
}
if (indexCompass.getSearchEngineOptimizer().isRunning()) {
indexCompass.getSearchEngineOptimizer().stop();
stoppedIndexCompassOptimizer = true;
}
indexCompass.getSearchEngineIndexManager().clearCache();
indexCompass.getSearchEngineIndexManager().deleteIndex();
indexCompass.getSearchEngineIndexManager().createIndex();
mirrorCompass.getSearchEngineIndexManager().replaceIndex(
indexCompass.getSearchEngineIndexManager(), new SearchEngineIndexManager.ReplaceIndexCallback() {
public void buildIndexIfNeeded() throws SearchEngineException {
// Here, we build the index (into the index compass)
for (Iterator it = devices.values().iterator(); it.hasNext();) {
CompassGpsDevice device = (CompassGpsDevice) it.next();
device.index();
}
}
});
if (stoppedMirrorCompassOptimizer) {
mirrorCompass.getSearchEngineOptimizer().start();
}
if (stoppedIndexCompassOptimizer) {
indexCompass.getSearchEngineOptimizer().start();
}
indexcompass是备份索引的Compass, mirrorCompass是当前索引的Compass
根据实现这code 在替换(红色code)是会产生异常现象,主要和搜索时的异常
建议删除原索引,然后重命名为原索引