meilisearch-java
meilisearch-java copied to clipboard
There is an asynchronous issue with updating the index in meisearch
@SuppressWarnings("unchecked")
@PostConstruct
private void initMeiLiSearch() {
Class<? extends MeiLiSearchService<T>> cla = (Class<? extends MeiLiSearchService<T>>) this.getClass();
Type type = cla.getGenericSuperclass();
if (type instanceof ParameterizedType) {
clazz = (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[0];
MSIndex msIndex = clazz.getAnnotation(MSIndex.class);
String uid = msIndex.uid();
String primaryKey = msIndex.primaryKey();
if (StringUtils.isEmpty(uid)) {
uid = clazz.getSimpleName();
}
****Client client = MeiLiSearchHelper.getInstance().getMeiLiClient();
Index[] indexs = client.getIndexes().getResults();
boolean uidFlg = false;
String primaryKeyOld = null;
Index oldIndex = null;
if (ArrayUtils.isNotEmpty(indexs)) {
for (Index index : indexs) {
if (StringUtils.equals(uid, index.getUid())) {
uidFlg = true;
primaryKeyOld = index.getPrimaryKey();
oldIndex=index;
break;
}
}**
if (uidFlg && StringUtils.notEquals(primaryKeyOld, primaryKey)) {
TaskInfo taskInfo = oldIndex.deleteAllDocuments();
client.updateIndex(uid, primaryKey);
}
}**
if (BooleanUtils.isFalse(uidFlg)) {
client.createIndex(uid, primaryKey);
}
index = client.getIndex(uid);
logger.info("index==>{}", GsonUtils.toJson(index));
}
}
The above is the code I wrote to obtain the new entity document through generics. I obtained the primary key setting through notification annotations, and I checked whether the UID was the same. If they were the same, I judged the previous primary key ID field and then modified the index's primary key field at different times. However, it did not take effect after the first startup until the ID was successfully displayed during the second startup. This issue can be fixed