Skip to content

Commit

Permalink
Simplify and optimize Elasticsearch retrieval by fetching only requir…
Browse files Browse the repository at this point in the history
…ed fields
  • Loading branch information
BartChris committed Aug 29, 2024
1 parent bad7449 commit 266cce3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -40,6 +41,7 @@
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.sort.SortBuilder;
import org.kitodo.data.elasticsearch.KitodoRestClient;
import org.kitodo.data.elasticsearch.exceptions.CustomResponseException;
Expand Down Expand Up @@ -198,37 +200,39 @@ SearchHits getDocument(String type, QueryBuilder query, SortBuilder sort, Intege
}

/**
* Get document list for given ids.
* Retrieves a map of document IDs to their corresponding base type for the given list of IDs.
*
* @param type
* for which request is performed
* @param ids
* of searched documents
* @return a list of documents, each represented as a map with document ID under the "id" key.
* @param type the type of documents being requested, used to determine the index.
* @param ids the list of document IDs to search for.
* @return a map where each key is a document ID and the value is the corresponding base type of the document.
*/
public List<Map<String, Object>> getDocuments(String type, List<Integer> ids) throws CustomResponseException, DataException {
List<Map<String, Object>> documents = new ArrayList<>();
public Map<Integer, String> getIdTypeMap(String type, List<Integer> ids) throws CustomResponseException, DataException {
Map<Integer, String> idToBaseTypeMap = new HashMap<>();

try {
// Create a MultiGetRequest to fetch multiple documents
// Create a MultiGetRequest to fetch multiple documents with only baseType field
MultiGetRequest multiGetRequest = new MultiGetRequest();
for (Integer id : ids) {
multiGetRequest.add(new MultiGetRequest.Item(this.indexBase + "_" + type, String.valueOf(id)));
MultiGetRequest.Item item = new MultiGetRequest.Item(this.indexBase + "_" + type, String.valueOf(id));
// Only fetch baseType field
item.fetchSourceContext(new FetchSourceContext(true, new String[]{"baseType"}, null));
multiGetRequest.add(item);
}
MultiGetResponse multiGetResponse = highLevelClient.mget(multiGetRequest, RequestOptions.DEFAULT);
for (MultiGetItemResponse itemResponse : multiGetResponse.getResponses()) {
if (!itemResponse.isFailed() && itemResponse.getResponse().isExists()) {
Map<String, Object> document = itemResponse.getResponse().getSourceAsMap();
document.put("id", itemResponse.getResponse().getId());
documents.add(document);
String baseType = (String) itemResponse.getResponse().getSourceAsMap().get("baseType");
Integer id = Integer.parseInt(itemResponse.getResponse().getId());

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
idToBaseTypeMap.put(id, baseType);
}
}
} catch (ResponseException e) {
handleResponseException(e);
} catch (IOException e) {
throw new DataException(e);
}
return documents;

return idToBaseTypeMap;
}

private String performRequest(String type, HttpEntity entity, String httpMethod, String urlRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ public Aggregations aggregateDocuments(QueryBuilder query, AggregationBuilder ag
}

/**
* Get documents by id.
* Retrieves a mapping of document IDs to their corresponding base type for the given list of IDs.
* Delegates to the `SearchRestClient`.
*
* @param ids
* of searched document as List
* @return JSONObject
* the list of document IDs to search for.
* @return a map where each key is a document ID and the value is the corresponding base type of the document.
*/
public List<Map<String, Object>> getDocuments(List<Integer> ids) throws CustomResponseException, DataException {
public Map<Integer, String> getIdTypeMap(List<Integer> ids) throws CustomResponseException, DataException {
SearchRestClient restClient = initiateRestClient();
return restClient.getDocuments(this.type,ids);
return restClient.getIdTypeMap(this.type,ids);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,23 +460,15 @@ public S findById(Integer id, boolean related) throws DataException {
}

/**
* Retrieves documents for the given list of IDs, extracts the "id" and "baseType" fields,
* and maps each ID to its corresponding base type.
* Retrieves a mapping of document IDs to their corresponding base types for the given list of IDs.
*
* @param ids
* list of document IDs to retrieve and process.
* @return a map where the keys are document IDs and the values are their associated base types.
*/
public Map<Integer, String> getIdTypeMap(List<Integer> ids) throws DataException {
try {
List<Map<String, Object>> documents = searcher.getDocuments(ids);
Map<Integer, String> idToBaseTypeMap = new HashMap<>();
for (Map<String, Object> document : documents) {
Integer id = Integer.parseInt((String) document.get("id"));
String baseType = (String) document.get("baseType");
idToBaseTypeMap.put(id, baseType);
}
return idToBaseTypeMap;
return searcher.getIdTypeMap(ids);
} catch (CustomResponseException e) {
throw new DataException(e);
}
Expand Down

0 comments on commit 266cce3

Please sign in to comment.