Skip to content

Commit

Permalink
fix(mod-search): fix export of cql files
Browse files Browse the repository at this point in the history
- try to not die

Closes: MSEARCH-762
  • Loading branch information
viacheslavpoliakov committed Jun 7, 2024
1 parent ef65301 commit 19bbc7f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/folio/search/cql/CqlSearchQueryConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,19 @@ public SearchSourceBuilder convert(String query, String resource) {
* @return search source as {@link SearchSourceBuilder} object with query and sorting conditions
*/
public SearchSourceBuilder convertForConsortia(String query, String resource) {
return convertForConsortia(query, resource, false);
return convertForConsortia(true, query, resource, false);
}

public SearchSourceBuilder convertForConsortia(boolean shouldBeShared, String query, String resource) {
return convertForConsortia(shouldBeShared, query, resource, false);
}

public SearchSourceBuilder convertForConsortia(String query, String resource, boolean consortiumConsolidated) {
return convertForConsortia(true, query, resource, consortiumConsolidated);
}

public SearchSourceBuilder convertForConsortia(boolean shouldBeShared, String query, String resource,
boolean consortiumConsolidated) {
var sourceBuilder = convert(query, resource);
if (consortiumConsolidated) {
return sourceBuilder;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/folio/search/service/ResourceIdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ private void streamResourceIds(CqlResourceIdsRequest request, Consumer<List<Stri
log.info("[SLY] *** streamResourceIds:: by [query: {}, TENANT: {}]", request.getQuery(), request.getTenantId());
boolean isConsortium = request.getTenantId().equalsIgnoreCase("consortium");
boolean isShared = request.getQuery().contains("shared==\"true\"");
log.info("[SLY] *** tenantIsConsortium: {}, shared: {}]", isConsortium, isShared);
boolean shouldBeShared = isConsortium || isShared;
log.info("[SLY] *** tenantIsConsortium: {}, shared: {}, shouldBeShared: {}]",
isConsortium, isShared, shouldBeShared);

var searchSource = queryConverter.convertForConsortia(request.getQuery(), request.getResource())
var searchSource = queryConverter.convertForConsortia(shouldBeShared, request.getQuery(), request.getResource())
.size(streamIdsProperties.getScrollQuerySize())
.fetchSource(new String[] {request.getSourceFieldPath()}, null)
.sort(fieldSort("_doc"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public class ConsortiumSearchHelper {
private final ConsortiumTenantService consortiumTenantService;

public QueryBuilder filterQueryForActiveAffiliation(QueryBuilder query, String resource) {
return filterQueryForActiveAffiliation(query, resource, true);
}

public QueryBuilder filterQueryForActiveAffiliation(QueryBuilder query, String resource, boolean shouldBeShared) {
var contextTenantId = folioExecutionContext.getTenantId();
var centralTenantId = consortiumTenantService.getCentralTenant(contextTenantId);
logger.info("[SLY] *** CONTEXT: {}, CENTRAL: {}, RESOURCE: {}",
Expand All @@ -50,13 +54,21 @@ public QueryBuilder filterQueryForActiveAffiliation(QueryBuilder query, String r
return query;
}

return filterQueryForActiveAffiliation(query, contextTenantId, centralTenantId.get(), resource);
QueryBuilder builder = filterQueryForActiveAffiliation(query, contextTenantId, centralTenantId.get(), resource,
shouldBeShared);
logger.info("[SLY] *** QueryBuilder: {}", builder.toString());
return builder;
}

public QueryBuilder filterQueryForActiveAffiliation(QueryBuilder query, String tenantId,
String centralTenantId, String resource) {
return filterQueryForActiveAffiliation(query, tenantId, centralTenantId, resource, true);
}

public QueryBuilder filterQueryForActiveAffiliation(QueryBuilder query, String tenantId,
String centralTenantId, String resource, boolean shouldBeShared) {
var boolQuery = prepareBoolQueryForActiveAffiliation(query);
addActiveAffiliationClauses(boolQuery, tenantId, centralTenantId, resource);
addActiveAffiliationClauses(boolQuery, tenantId, centralTenantId, resource, shouldBeShared);

return boolQuery;
}
Expand Down Expand Up @@ -150,8 +162,9 @@ private BoolQueryBuilder prepareBoolQueryForActiveAffiliation(QueryBuilder query
}

private void addActiveAffiliationClauses(BoolQueryBuilder boolQuery, String contextTenantId,
String centralTenantId, String resource) {
var affiliationShouldClauses = getAffiliationShouldClauses(contextTenantId, centralTenantId, resource);
String centralTenantId, String resource, boolean shouldBeShared) {
var affiliationShouldClauses = getAffiliationShouldClauses(contextTenantId, centralTenantId, resource,
shouldBeShared);
if (boolQuery.should().isEmpty()) {
affiliationShouldClauses.forEach(boolQuery::should);
} else {
Expand All @@ -162,11 +175,11 @@ private void addActiveAffiliationClauses(BoolQueryBuilder boolQuery, String cont
}

private LinkedList<QueryBuilder> getAffiliationShouldClauses(String contextTenantId, String centralTenantId,
String resource) {
String resource, boolean shouldBeShared) {
var affiliationShouldClauses = new LinkedList<QueryBuilder>();
addTenantIdAffiliationShouldClause(contextTenantId, centralTenantId, affiliationShouldClauses,
resource);
addSharedAffiliationShouldClause(affiliationShouldClauses, resource, contextTenantId.equals(centralTenantId));
addSharedAffiliationShouldClause(affiliationShouldClauses, resource, shouldBeShared);
return affiliationShouldClauses;
}

Expand Down

0 comments on commit 19bbc7f

Please sign in to comment.