Refactored duplicate methods createEntity and createTimeSeriesEntity

This commit is contained in:
Bhanu Agrawal 2025-09-25 14:25:13 +01:00
parent 8a35bf3f28
commit 12b5c28278
2 changed files with 50 additions and 92 deletions

View File

@ -62,28 +62,7 @@ public class ElasticSearchEntityManager implements EntityManagementClient {
@Override
public void createEntity(String indexName, String docId, String doc) {
if (!isClientAvailable) {
LOG.error("ElasticSearch client is not available. Cannot create entity.");
return;
}
try {
client.update(
u ->
u.index(indexName)
.id(docId)
.docAsUpsert(true)
.refresh(Refresh.True)
.doc(toJsonData(doc)),
Map.class);
LOG.info(
"Successfully created entity in ElasticSearch for index: {}, docId: {}",
indexName,
docId);
} catch (Exception e) {
LOG.error(
"Failed to create entity in ElasticSearch for index: {}, docId: {}", indexName, docId, e);
}
upsertDocument(indexName, docId, doc, "create entity");
}
@Override
@ -148,31 +127,7 @@ public class ElasticSearchEntityManager implements EntityManagementClient {
@Override
public void createTimeSeriesEntity(String indexName, String docId, String doc) {
if (!isClientAvailable) {
LOG.error("ElasticSearch client is not available. Cannot create time series entity.");
return;
}
try {
client.update(
u ->
u.index(indexName)
.id(docId)
.docAsUpsert(true)
.refresh(Refresh.True)
.doc(toJsonData(doc)),
Map.class);
LOG.info(
"Successfully created time series entity in ElasticSearch for index: {}, docId: {}",
indexName,
docId);
} catch (Exception e) {
LOG.error(
"Failed to create time series entity in ElasticSearch for index: {}, docId: {}",
indexName,
docId,
e);
}
upsertDocument(indexName, docId, doc, "create time series entity");
}
@Override
@ -629,6 +584,29 @@ public class ElasticSearchEntityManager implements EntityManagementClient {
}
}
private void upsertDocument(String indexName, String docId, String doc, String operation) {
if (!isClientAvailable) {
LOG.error("ElasticSearch client is not available. Cannot {}.", operation);
return;
}
try {
client.update(
u ->
u.index(indexName)
.id(docId)
.docAsUpsert(true)
.refresh(Refresh.True)
.doc(toJsonData(doc)),
Map.class);
LOG.info(
"Successfully {} in ElasticSearch for index: {}, docId: {}", operation, indexName, docId);
} catch (Exception e) {
LOG.error(
"Failed to {} in ElasticSearch for index: {}, docId: {}", operation, indexName, docId, e);
}
}
private Map<String, JsonData> convertToJsonDataMap(Map<String, Object> map) {
return JsonUtils.getMap(map).entrySet().stream()
.filter(entry -> entry.getValue() != null)

View File

@ -63,26 +63,7 @@ public class OpenSearchEntityManager implements EntityManagementClient {
@Override
public void createEntity(String indexName, String docId, String doc) {
if (!isClientAvailable) {
LOG.error("OpenSearch client is not available. Cannot create entity.");
return;
}
try {
client.update(
u ->
u.index(indexName)
.id(docId)
.refresh(Refresh.True)
.docAsUpsert(true)
.doc(toJsonData(doc)),
Map.class);
LOG.info(
"Successfully created entity in OpenSearch for index: {}, docId: {}", indexName, docId);
} catch (Exception e) {
LOG.error(
"Failed to create entity in OpenSearch for index: {}, docId: {}", indexName, docId, e);
}
upsertDocument(indexName, docId, doc, "create entity");
}
@Override
@ -145,31 +126,7 @@ public class OpenSearchEntityManager implements EntityManagementClient {
@Override
public void createTimeSeriesEntity(String indexName, String docId, String doc) {
if (!isClientAvailable) {
LOG.error("OpenSearch client is not available. Cannot create time series entity.");
return;
}
try {
client.update(
u ->
u.index(indexName)
.id(docId)
.refresh(Refresh.True)
.docAsUpsert(true)
.doc(toJsonData(doc)),
Map.class);
LOG.info(
"Successfully created time series entity in OpenSearch for index: {}, docId: {}",
indexName,
docId);
} catch (Exception e) {
LOG.error(
"Failed to create time series entity in OpenSearch for index: {}, docId: {}",
indexName,
docId,
e);
}
upsertDocument(indexName, docId, doc, "create time series entity");
}
@Override
@ -627,6 +584,29 @@ public class OpenSearchEntityManager implements EntityManagementClient {
}
}
private void upsertDocument(String indexName, String docId, String doc, String operation) {
if (!isClientAvailable) {
LOG.error("OpenSearch client is not available. Cannot {}.", operation);
return;
}
try {
client.update(
u ->
u.index(indexName)
.id(docId)
.refresh(Refresh.True)
.docAsUpsert(true)
.doc(toJsonData(doc)),
Map.class);
LOG.info(
"Successfully {} in OpenSearch for index: {}, docId: {}", operation, indexName, docId);
} catch (Exception e) {
LOG.error(
"Failed to {} in OpenSearch for index: {}, docId: {}", operation, indexName, docId, e);
}
}
private Map<String, JsonData> convertToJsonDataMap(Map<String, Object> map) {
return JsonUtils.getMap(map).entrySet().stream()
.filter(entry -> entry.getValue() != null)