Skip to content

Commit

Permalink
Issue #SB-28950 chore: Fixed the Sonar code smells.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmiableAnil committed Apr 13, 2022
1 parent 2b56977 commit 8a9cb74
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public String getTimeZone() {
public static boolean isIndexExists(String indexName) {
Response response;
try {
response = getClient(indexName).getLowLevelClient().performRequest("HEAD", "/" + indexName);
response = getClient(indexName).getLowLevelClient().performRequest(new Request("HEAD", "/" + indexName));
return (200 == response.getStatusLine().getStatusCode());
} catch (IOException e) {
return false;
Expand Down Expand Up @@ -203,7 +203,7 @@ public static void updateDocument(String indexName, String documentType, String
Map<String, Object> updatedDoc = checkDocStringLength(doc);
IndexRequest indexRequest = new IndexRequest(indexName, documentType, documentId).source(updatedDoc);
UpdateRequest request = new UpdateRequest().index(indexName).type(documentType).id(documentId).doc(updatedDoc).upsert(indexRequest);
UpdateResponse response = getClient(indexName).update(request);
UpdateResponse response = getClient(indexName).update(request, RequestOptions.DEFAULT);
TelemetryManager.log("Updated " + response.getId() + " to index " + response.getIndex());
} catch (IOException e) {
TelemetryManager.error("Error while updating document to index :" + indexName, e);
Expand All @@ -219,8 +219,8 @@ public static void deleteDocument(String indexName, String documentType, String

public static void deleteDocumentsByQuery(QueryBuilder query, String indexName, String indexType)
throws IOException {
Response response = getClient(indexName).getLowLevelClient().performRequest("POST",
indexName + "/_delete_by_query" + query);
Response response = getClient(indexName).getLowLevelClient().performRequest(new Request("POST",
indexName + "/_delete_by_query" + query));

TelemetryManager.log("Deleted Documents by Query" + EntityUtils.toString(response.getEntity()));
}
Expand All @@ -233,7 +233,7 @@ public static void deleteIndex(String indexName) throws IOException {

public static String getDocumentAsStringById(String indexName, String documentType, String documentId)
throws IOException {
GetResponse response = getClient(indexName).get(new GetRequest(indexName, documentType, documentId));
GetResponse response = getClient(indexName).get(new GetRequest(indexName, documentType, documentId), RequestOptions.DEFAULT);
return response.getSourceAsString();
}

Expand All @@ -242,7 +242,7 @@ public static List<String> getMultiDocumentAsStringByIdList(String indexName, St
List<String> finalResult = new ArrayList<String>();
MultiGetRequest request = new MultiGetRequest();
documentIdList.forEach(docId -> request.add(indexName, documentType, docId));
MultiGetResponse multiGetItemResponses = getClient(indexName).multiGet(request);
MultiGetResponse multiGetItemResponses = getClient(indexName).mget(request, RequestOptions.DEFAULT);
for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
GetResponse response = itemResponse.getResponse();
if (response.isExists()) {
Expand All @@ -265,7 +265,7 @@ public static void bulkIndexWithIndexId(String indexName, String documentType, M
request.add(new IndexRequest(indexName, documentType, key)
.source((Map<String, Object>) jsonObjects.get(key)));
if (count % BATCH_SIZE == 0 || (count % BATCH_SIZE < BATCH_SIZE && count == jsonObjects.size())) {
BulkResponse bulkResponse = client.bulk(request);
BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
if (bulkResponse.hasFailures()) {
TelemetryManager
.log("Failures in Elasticsearch bulkIndex : " + bulkResponse.buildFailureMessage());
Expand All @@ -290,7 +290,7 @@ public static void bulkIndexWithAutoGenerateIndexId(String indexName, String doc
count++;
request.add(new IndexRequest(indexName, documentType).source(json));
if (count % BATCH_SIZE == 0 || (count % BATCH_SIZE < BATCH_SIZE && count == jsonObjects.size())) {
BulkResponse bulkResponse = client.bulk(request);
BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
if (bulkResponse.hasFailures()) {
TelemetryManager
.log("Failures in Elasticsearch bulkIndex : " + bulkResponse.buildFailureMessage());
Expand Down Expand Up @@ -432,15 +432,15 @@ public static SearchResponse search(Map<String, Object> matchCriterias, Map<Stri

public static SearchResponse search(String indexName, String indexType, SearchSourceBuilder query)
throws Exception {
return getClient(indexName).search(new SearchRequest(indexName).source(query));
return getClient(indexName).search(new SearchRequest(indexName).source(query), RequestOptions.DEFAULT);
}

public static Future<SearchResponse> search(String indexName, SearchSourceBuilder searchSourceBuilder)
throws IOException {
TelemetryManager.log("searching in ES index: " + indexName);
Promise<SearchResponse> promise = Futures.promise();
getClient(indexName).searchAsync(new SearchRequest().indices(indexName).source(searchSourceBuilder),
new ActionListener<SearchResponse>() {
RequestOptions.DEFAULT, new ActionListener<SearchResponse>() {

@Override
public void onResponse(SearchResponse response) {
Expand All @@ -457,7 +457,7 @@ public void onFailure(Exception e) {

public static int count(String indexName, SearchSourceBuilder searchSourceBuilder) throws IOException {
SearchResponse response = getClient(indexName)
.search(new SearchRequest().indices(indexName).source(searchSourceBuilder));
.search(new SearchRequest().indices(indexName).source(searchSourceBuilder), RequestOptions.DEFAULT);
return (int) response.getHits().getTotalHits();

}
Expand Down Expand Up @@ -688,7 +688,7 @@ public static void bulkDeleteDocumentById(String indexName, String documentType,
count++;
request.add(new DeleteRequest(indexName, documentType, documentId));
if (count % BATCH_SIZE == 0 || (count % BATCH_SIZE < BATCH_SIZE && count == identifiers.size())) {
BulkResponse bulkResponse = getClient(indexName).bulk(request);
BulkResponse bulkResponse = getClient(indexName).bulk(request, RequestOptions.DEFAULT);
List<String> failedIds = Arrays.stream(bulkResponse.getItems()).filter(
itemResp -> !StringUtils.equals(itemResp.getResponse().getResult().getLowercase(),"deleted")
).map(r -> r.getResponse().getId()).collect(Collectors.toList());
Expand Down

0 comments on commit 8a9cb74

Please sign in to comment.