Skip to content

Commit

Permalink
Issue #3737: [API] Add support to export faceted search result with t…
Browse files Browse the repository at this point in the history
…emplate file (#3747)
  • Loading branch information
ekazachkova authored Oct 16, 2024
1 parent 682e2ff commit b2c59d9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ public class FacetedSearchRequest {
private List<String> metadataFields;
private ScrollingParameters scrollingParameters;
private List<SearchRequestSort> sorts;
private List<SearchStorageFilesRequest> files;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 EPAM Systems, Inc. (https://www.epam.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package com.epam.pipeline.controller.vo.search;

import lombok.Data;

@Data
public class SearchStorageFilesRequest {
private String path;
private Long storageId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.epam.pipeline.manager.datastorage.DataStorageManager;
import com.epam.pipeline.manager.preference.PreferenceManager;
import com.epam.pipeline.manager.preference.SystemPreferences;
import com.epam.pipeline.manager.security.AuthManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class SearchExportManager {
private static final String EXPORT_DATE = "Export_Date";
private static final String EXPORT_DATE_PLACEHOLDER = String.format("{%s}", EXPORT_DATE);
private static final String EXPORT_DATE_REGEX = String.format("\\{%s:([^}]+)\\}", EXPORT_DATE);
private static final String CURRENT_USER_PLACEHOLDER = "{User}";
private static final String S3_SCHEMA = "s3://";
private static final String CP_SCHEMA = "cp://";
private static final String AZ_SCHEMA = "az://";
Expand All @@ -67,6 +69,7 @@ public class SearchExportManager {
private final MessageHelper messageHelper;
private final PreferenceManager preferenceManager;
private final SearchResultExportManager resultExportManager;
private final AuthManager authManager;

public byte[] export(final FacetedSearchExportRequest request) {
final FacetedSearchRequest facetedSearchRequest = request.getFacetedSearchRequest();
Expand Down Expand Up @@ -124,6 +127,9 @@ private String getCloudExportPath(final String pathToSave, final String template
LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateTimePattern)));
}
}
if (pathToSave.contains(CURRENT_USER_PLACEHOLDER)) {
resolvedPath = resolvedPath.replace(CURRENT_USER_PLACEHOLDER, authManager.getAuthorizedUser());
}
return resolvedPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.epam.pipeline.controller.vo.search.ScrollingParameters;
import com.epam.pipeline.controller.vo.search.SearchRequestSort;
import com.epam.pipeline.controller.vo.search.SearchRequestSortOrder;
import com.epam.pipeline.controller.vo.search.SearchStorageFilesRequest;
import com.epam.pipeline.entity.datastorage.DataStorageType;
import com.epam.pipeline.entity.search.SearchDocumentType;
import com.epam.pipeline.entity.user.DefaultRoles;
Expand Down Expand Up @@ -108,6 +109,7 @@ public class SearchRequestBuilder {
private static final String ES_STORAGE_ID_FIELD = "storage_id";
private static final String ES_STORAGE_NAME_FIELD = "storage_name";
private static final String STORAGE_CLASS_FIELD = "storage_class";
private static final String ES_PATH_FIELD = "path";

private final PreferenceManager preferenceManager;
private final AuthManager authManager;
Expand Down Expand Up @@ -211,7 +213,9 @@ public SearchRequest buildFacetedRequest(final FacetedSearchRequest searchReques
final String typeFieldName,
final Set<String> metadataSourceFields) {
final SearchSourceBuilder searchSource = new SearchSourceBuilder()
.query(getFacetedQuery(searchRequest.getQuery(), searchRequest.getFilters()))
.query(CollectionUtils.isEmpty(searchRequest.getFiles())
? getFacetedQuery(searchRequest.getQuery(), searchRequest.getFilters())
: getStorageFilesQuery(searchRequest.getFiles()))
.fetchSource(buildSourceFields(typeFieldName, metadataSourceFields), Strings.EMPTY_ARRAY)
.size(searchRequest.getPageSize());

Expand Down Expand Up @@ -413,6 +417,20 @@ private QueryBuilder getQuery(final ElasticSearchRequest queryRequest) {
return prepareAclFiltersOrAdmin(queryBuilder);
}

private QueryBuilder getStorageFilesQuery(final List<SearchStorageFilesRequest> filesRequests) {
final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.mustNot(QueryBuilders.termsQuery(SEARCH_HIDDEN, Boolean.TRUE));
if (preferenceManager.getPreference(SystemPreferences.SEARCH_HIDE_DELETED)) {
boolQueryBuilder.mustNot(QueryBuilders.termsQuery(SEARCH_DELETED, Boolean.TRUE));
}
ListUtils.emptyIfNull(filesRequests)
.forEach(file -> boolQueryBuilder.should(QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery(buildKeywordName(ES_PATH_FIELD), file.getPath()))
.filter(QueryBuilders.termQuery(ES_STORAGE_ID_FIELD, file.getStorageId()))
));
return boolQueryBuilder;
}

private QueryBuilder getFacetedQuery(final String query, final Map<String, List<String>> filters) {
final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(prepareFacetedQuery(query));
Expand Down

0 comments on commit b2c59d9

Please sign in to comment.