Test cases are working fine with below configuration and before every test clear index data and recreate it.
The problem here is if I run test cases more than one place it start failing as indexes are clear because of “before method” and index data is inconsistent .
import org.hibernate.search.backend.elasticsearch.index.layout.IndexLayoutStrategy;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
-
Configuration for Hibernate Search. This class defines the format of indexes being created in elastic server.
-
@author Abhishek Swarnkar
-
@version 1.0
-
@since 25-09-2020
*/
public class CustomLayoutStrategy implements IndexLayoutStrategy {private static final DateTimeFormatter INDEX_SUFFIX_FORMATTER =
DateTimeFormatter.ofPattern(“uuuuMMdd-HHmmss-SSSSSSSSS”, Locale.ROOT)
.withZone(ZoneOffset.UTC);
private static final Pattern UNIQUE_KEY_PATTERN =
Pattern.compile("(.*)-\d±\d±\d+");@Override
public String createInitialElasticsearchIndexName(String hibernateSearchIndexName) {
return hibernateSearchIndexName + “-”
+ INDEX_SUFFIX_FORMATTER.format(Instant.now());
}@Override
public String createWriteAlias(String hibernateSearchIndexName) {
return hibernateSearchIndexName + “-write”;
}@Override
public String createReadAlias(String hibernateSearchIndexName) {
return hibernateSearchIndexName;
}@Override
public String extractUniqueKeyFromHibernateSearchIndexName(
String hibernateSearchIndexName) {
return hibernateSearchIndexName;
}@Override
public String extractUniqueKeyFromElasticsearchIndexName(
String elasticsearchIndexName) {
Matcher matcher = UNIQUE_KEY_PATTERN.matcher(elasticsearchIndexName);
if (!matcher.matches()) {
throw new IllegalArgumentException(
"Unrecognized index name: " + elasticsearchIndexName
);
}
return matcher.group(1);
}
}
@Before
public void before() throws Exception {
deleteData();
saveSaleSaga();
SearchSession searchSession = org.hibernate.search.mapper.orm.Search.session(managerFactory.createEntityManager());
searchSession.massIndexer().startAndWait();
}