Skip to content
Snippets Groups Projects
Commit 39668e66 authored by Rustam Lotsmanenko (EPAM)'s avatar Rustam Lotsmanenko (EPAM)
Browse files

elastic v7 remove deprecated elastic lib calls

parent 5cbd62eb
No related branches found
No related tags found
1 merge request!68Elastic 7
...@@ -23,14 +23,14 @@ import java.util.Set; ...@@ -23,14 +23,14 @@ import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Request; import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.BulkByScrollResponse;
...@@ -183,12 +183,10 @@ public class IndexerMappingServiceImpl extends MappingServiceImpl implements Ind ...@@ -183,12 +183,10 @@ public class IndexerMappingServiceImpl extends MappingServiceImpl implements Ind
try { try {
GetFieldMappingsResponse response = client.indices().getFieldMapping(request, RequestOptions.DEFAULT); GetFieldMappingsResponse response = client.indices().getFieldMapping(request, RequestOptions.DEFAULT);
if (response != null && !response.mappings().isEmpty()) { if (response != null && !response.mappings().isEmpty()) {
final Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>>> mappings = response.mappings(); final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = response.mappings();
for (String index : indices) { for (String index : indices) {
//extract mapping of each index if (mappings != null && !mappings.isEmpty()) {
final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> indexMapping = mappings.get(index); indexMappingMap.put(index, mappings);
if (indexMapping != null && !indexMapping.isEmpty()) {
indexMappingMap.put(index, indexMapping);
} }
} }
} }
...@@ -207,9 +205,8 @@ public class IndexerMappingServiceImpl extends MappingServiceImpl implements Ind ...@@ -207,9 +205,8 @@ public class IndexerMappingServiceImpl extends MappingServiceImpl implements Ind
log.error(String.format("Could not find type of the mappings for index: %s.", index)); log.error(String.format("Could not find type of the mappings for index: %s.", index));
return false; return false;
} }
request.type(type); request.setTimeout(REQUEST_TIMEOUT);
request.timeout(REQUEST_TIMEOUT);
Map<String, GetFieldMappingsResponse.FieldMappingMetadata> metaData = indexMapping.get(type); Map<String, GetFieldMappingsResponse.FieldMappingMetadata> metaData = indexMapping.get(type);
if(metaData==null || metaData.get("data." + fieldName)==null) { if(metaData==null || metaData.get("data." + fieldName)==null) {
log.error(String.format("Could not find field: %s in the mapping of index: %s.", fieldName, index)); log.error(String.format("Could not find field: %s in the mapping of index: %s.", fieldName, index));
...@@ -316,9 +313,8 @@ public class IndexerMappingServiceImpl extends MappingServiceImpl implements Ind ...@@ -316,9 +313,8 @@ public class IndexerMappingServiceImpl extends MappingServiceImpl implements Ind
try { try {
if (mapping != null) { if (mapping != null) {
PutMappingRequest request = new PutMappingRequest(index); PutMappingRequest request = new PutMappingRequest(index);
request.type(type);
request.source(mapping, XContentType.JSON); request.source(mapping, XContentType.JSON);
request.timeout(REQUEST_TIMEOUT); request.setTimeout(REQUEST_TIMEOUT);
AcknowledgedResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT); AcknowledgedResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT);
return response.isAcknowledged(); return response.isAcknowledged();
} }
......
...@@ -368,10 +368,10 @@ public class IndexerServiceImpl implements IndexerService { ...@@ -368,10 +368,10 @@ public class IndexerServiceImpl implements IndexerService {
String index = this.elasticIndexNameResolver.getIndexNameFromKind(record.getKind()); String index = this.elasticIndexNameResolver.getIndexNameFromKind(record.getKind());
if (operation == OperationType.create) { if (operation == OperationType.create) {
IndexRequest indexRequest = new IndexRequest(index, record.getType(), record.getId()).source(this.gson.toJson(sourceMap), XContentType.JSON); IndexRequest indexRequest = new IndexRequest(index).id(record.getId()).source(this.gson.toJson(sourceMap), XContentType.JSON);
bulkRequest.add(indexRequest); bulkRequest.add(indexRequest);
} else if (operation == OperationType.update) { } else if (operation == OperationType.update) {
UpdateRequest updateRequest = new UpdateRequest(index, record.getType(), record.getId()).upsert(this.gson.toJson(sourceMap), XContentType.JSON); UpdateRequest updateRequest = new UpdateRequest(index, record.getId()).upsert(this.gson.toJson(sourceMap), XContentType.JSON);
bulkRequest.add(updateRequest); bulkRequest.add(updateRequest);
} }
} }
...@@ -385,13 +385,10 @@ public class IndexerServiceImpl implements IndexerService { ...@@ -385,13 +385,10 @@ public class IndexerServiceImpl implements IndexerService {
for (Map.Entry<String, List<String>> record : deleteRecordMap.entrySet()) { for (Map.Entry<String, List<String>> record : deleteRecordMap.entrySet()) {
String[] kindParts = record.getKey().split(":");
String type = kindParts[2];
String index = this.elasticIndexNameResolver.getIndexNameFromKind(record.getKey()); String index = this.elasticIndexNameResolver.getIndexNameFromKind(record.getKey());
for (String id : record.getValue()) { for (String id : record.getValue()) {
DeleteRequest deleteRequest = new DeleteRequest(index, type, id); DeleteRequest deleteRequest = new DeleteRequest(index, id);
bulkRequest.add(deleteRequest); bulkRequest.add(deleteRequest);
} }
} }
...@@ -406,8 +403,6 @@ public class IndexerServiceImpl implements IndexerService { ...@@ -406,8 +403,6 @@ public class IndexerServiceImpl implements IndexerService {
List<String> failureRecordIds = new LinkedList<>(); List<String> failureRecordIds = new LinkedList<>();
if (bulkRequest.numberOfActions() == 0) return failureRecordIds; if (bulkRequest.numberOfActions() == 0) return failureRecordIds;
try { try {
BulkResponse bulkResponse = restClient.bulk(bulkRequest, RequestOptions.DEFAULT); BulkResponse bulkResponse = restClient.bulk(bulkRequest, RequestOptions.DEFAULT);
......
...@@ -22,9 +22,7 @@ import org.apache.http.HttpStatus; ...@@ -22,9 +22,7 @@ import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Request; import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
...@@ -32,6 +30,7 @@ import org.elasticsearch.client.Response; ...@@ -32,6 +30,7 @@ import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
...@@ -96,6 +95,7 @@ public class IndicesServiceImpl implements IndicesService { ...@@ -96,6 +95,7 @@ public class IndicesServiceImpl implements IndicesService {
String mappingJsonString = new Gson().toJson(mapping, Map.class); String mappingJsonString = new Gson().toJson(mapping, Map.class);
request.mapping(mappingJsonString,XContentType.JSON); request.mapping(mappingJsonString,XContentType.JSON);
} }
request.setTimeout(REQUEST_TIMEOUT);
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
// cache the index status // cache the index status
boolean indexStatus = response.isAcknowledged() && response.isShardsAcknowledged(); boolean indexStatus = response.isAcknowledged() && response.isShardsAcknowledged();
...@@ -129,8 +129,7 @@ public class IndicesServiceImpl implements IndicesService { ...@@ -129,8 +129,7 @@ public class IndicesServiceImpl implements IndicesService {
//In case the format of cache changes then clean the cache //In case the format of cache changes then clean the cache
this.indicesExistCache.delete(index); this.indicesExistCache.delete(index);
} }
GetIndexRequest request = new GetIndexRequest(); GetIndexRequest request = new GetIndexRequest(index);
request.indices(index);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
if (exists) this.indicesExistCache.put(index, true); if (exists) this.indicesExistCache.put(index, true);
return exists; return exists;
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
package org.opengroup.osdu.indexer.service; package org.opengroup.osdu.indexer.service;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver; import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import org.opengroup.osdu.core.common.search.IndicesService; import org.opengroup.osdu.core.common.search.IndicesService;
......
...@@ -8,7 +8,6 @@ import com.sun.jersey.api.client.ClientResponse; ...@@ -8,7 +8,6 @@ import com.sun.jersey.api.client.ClientResponse;
import cucumber.api.DataTable; import cucumber.api.DataTable;
import lombok.extern.java.Log; import lombok.extern.java.Log;
import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.opengroup.osdu.core.common.model.entitlements.Acl; import org.opengroup.osdu.core.common.model.entitlements.Acl;
import org.opengroup.osdu.models.Setup; import org.opengroup.osdu.models.Setup;
import org.opengroup.osdu.models.TestIndex; import org.opengroup.osdu.models.TestIndex;
...@@ -118,7 +117,7 @@ public class RecordSteps extends TestsBase { ...@@ -118,7 +117,7 @@ public class RecordSteps extends TestsBase {
public void i_should_get_the_elastic_for_the_tenant_testindex_timestamp_well_in_the_Elastic_Search(String expectedMapping, String type, String index) throws Throwable { public void i_should_get_the_elastic_for_the_tenant_testindex_timestamp_well_in_the_Elastic_Search(String expectedMapping, String type, String index) throws Throwable {
index = generateActualName(index, timeStamp); index = generateActualName(index, timeStamp);
ImmutableOpenMap<String, MappingMetadata> elasticMapping = elasticUtils.getMapping(index); Map<String, MappingMetadata> elasticMapping = elasticUtils.getMapping(index);
assertNotNull(elasticMapping); assertNotNull(elasticMapping);
MappingMetadata typeMapping = elasticMapping.get(type); MappingMetadata typeMapping = elasticMapping.get(type);
......
...@@ -33,12 +33,7 @@ import org.apache.http.ssl.SSLContextBuilder; ...@@ -33,12 +33,7 @@ import org.apache.http.ssl.SSLContextBuilder;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.client.indices.CloseIndexRequest; import org.elasticsearch.client.indices.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkRequest;
...@@ -48,6 +43,11 @@ import org.elasticsearch.action.search.SearchRequest; ...@@ -48,6 +43,11 @@ import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.*; import org.elasticsearch.client.*;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
...@@ -99,9 +99,9 @@ public class ElasticUtils { ...@@ -99,9 +99,9 @@ public class ElasticUtils {
// creating index + add mapping to the index // creating index + add mapping to the index
log.info("Creating index with name: " + index); log.info("Creating index with name: " + index);
CreateIndexRequest request = new CreateIndexRequest(index, settings); CreateIndexRequest request = new CreateIndexRequest(index).settings(settings);
request.source("{\"mappings\":" + mapping + "}", XContentType.JSON); request.source("{\"mappings\":" + mapping + "}", XContentType.JSON);
request.timeout(REQUEST_TIMEOUT); request.setTimeout(REQUEST_TIMEOUT);
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
//wait for ack //wait for ack
...@@ -234,13 +234,13 @@ public class ElasticUtils { ...@@ -234,13 +234,13 @@ public class ElasticUtils {
} }
} }
public ImmutableOpenMap<String, MappingMetadata> getMapping(String index) throws IOException { public Map<String, MappingMetadata> getMapping(String index) throws IOException {
try (RestHighLevelClient client = this.createClient(username, password, host)) { try (RestHighLevelClient client = this.createClient(username, password, host)) {
GetMappingsRequest request = new GetMappingsRequest(); GetMappingsRequest request = new GetMappingsRequest();
request.indices(index); request.indices(index);
GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT); GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT);
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> allMappings = response.mappings(); Map<String, MappingMetadata> mappings = response.mappings();
return allMappings.get(index); return mappings;
} }
} }
...@@ -276,7 +276,7 @@ public class ElasticUtils { ...@@ -276,7 +276,7 @@ public class ElasticUtils {
for (Map<String, Object> record : testRecords) { for (Map<String, Object> record : testRecords) {
String id = (String) record.get("id"); String id = (String) record.get("id");
Map<String, Object> mapData = gson.fromJson(gson.toJson(record), Map.class); Map<String, Object> mapData = gson.fromJson(gson.toJson(record), Map.class);
IndexRequest indexRequest = new IndexRequest(index, kind.split(":")[2], id).source(mapData); IndexRequest indexRequest = new IndexRequest(index).id(id).source(mapData);
dataList.add(indexRequest); dataList.add(indexRequest);
} }
} catch (Exception e) { } catch (Exception e) {
...@@ -362,8 +362,7 @@ public class ElasticUtils { ...@@ -362,8 +362,7 @@ public class ElasticUtils {
private boolean createRestClientAndCheckIndexExist(String index) { private boolean createRestClientAndCheckIndexExist(String index) {
try (RestHighLevelClient client = this.createClient(username, password, host)) { try (RestHighLevelClient client = this.createClient(username, password, host)) {
GetIndexRequest request = new GetIndexRequest(); GetIndexRequest request = new GetIndexRequest(index);
request.indices(index);
return client.indices().exists(request, RequestOptions.DEFAULT); return client.indices().exists(request, RequestOptions.DEFAULT);
} catch (IOException e) { } catch (IOException e) {
log.log(Level.INFO, String.format("Error getting index: %s %s", index, e.getMessage())); log.log(Level.INFO, String.format("Error getting index: %s %s", index, e.getMessage()));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment