diff --git a/solidify-index-controller/src/main/java/ch/unige/solidify/service/IndexResourceService.java b/solidify-index-controller/src/main/java/ch/unige/solidify/service/IndexResourceService.java index a4e20a6598bbdea069c8b42f67e099d20a8df8a6..5ab1d474e7b7a95fa6cc74329c3fc0d67f833f45 100644 --- a/solidify-index-controller/src/main/java/ch/unige/solidify/service/IndexResourceService.java +++ b/solidify-index-controller/src/main/java/ch/unige/solidify/service/IndexResourceService.java @@ -9,12 +9,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-2.0.html>. @@ -32,6 +32,7 @@ import org.springframework.data.domain.Pageable; import ch.unige.solidify.model.index.IndexMetadata; import ch.unige.solidify.rest.FacetPage; import ch.unige.solidify.rest.FacetRequest; +import ch.unige.solidify.rest.FieldsRequest; import ch.unige.solidify.rest.SearchCondition; public abstract class IndexResourceService<I, T extends IndexMetadata> extends AbstractResourceService { @@ -80,6 +81,11 @@ public abstract class IndexResourceService<I, T extends IndexMetadata> extends A return this.findAll(indexName, null, pageable); } + public FacetPage<T> search(I indexName, List<SearchCondition> conditions, List<FacetRequest> facetRequests, Pageable pageable, + FieldsRequest fieldsRequest) { + return this.search(indexName, conditions, facetRequests, pageable); + } + public FacetPage<T> search(I indexName, List<SearchCondition> conditions, List<FacetRequest> facetRequests, Pageable pageable) { return this.search(indexName, null, null, facetRequests, pageable); } diff --git a/solidify-index/src/main/java/ch/unige/solidify/index/indexing/elasticsearch/ElasticsearchService.java b/solidify-index/src/main/java/ch/unige/solidify/index/indexing/elasticsearch/ElasticsearchService.java index 798cc0fb143fddf3e4a6431d290dd7ac46816a8c..374c234e1e6bb6dd679a23357ec0f9d8c5607de9 100644 --- a/solidify-index/src/main/java/ch/unige/solidify/index/indexing/elasticsearch/ElasticsearchService.java +++ b/solidify-index/src/main/java/ch/unige/solidify/index/indexing/elasticsearch/ElasticsearchService.java @@ -77,6 +77,7 @@ import ch.unige.solidify.rest.FacetPage; import ch.unige.solidify.rest.FacetRequest; import ch.unige.solidify.rest.FacetResult; import ch.unige.solidify.rest.FacetResultValue; +import ch.unige.solidify.rest.FieldsRequest; import ch.unige.solidify.rest.RestCollectionPage; import ch.unige.solidify.rest.SearchCondition; import ch.unige.solidify.rest.SearchConditionType; @@ -274,11 +275,18 @@ public abstract class ElasticsearchService<T extends IndexMetadata> extends Inde @Override public FacetPage<T> search(String indexName, List<SearchCondition> conditions, List<FacetRequest> facetRequests, - Pageable pageable) { + Pageable pageable, FieldsRequest fieldsRequest) { final BoolQueryBuilder boolQuery = this.getBooleanQueryBuilder(conditions); - return this.search(indexName, boolQuery, facetRequests, pageable); + return this.search(indexName, boolQuery, facetRequests, pageable, fieldsRequest); + } + + @Override + public FacetPage<T> search(String indexName, List<SearchCondition> conditions, List<FacetRequest> facetRequests, + Pageable pageable) { + + return this.search(indexName, conditions, facetRequests, pageable, null); } private BoolQueryBuilder getBooleanQueryBuilder(List<SearchCondition> conditions) { @@ -481,7 +489,21 @@ public abstract class ElasticsearchService<T extends IndexMetadata> extends Inde } private FacetPage<T> search(String indexName, AbstractQueryBuilder<?> q, List<FacetRequest> facetRequests, Pageable pageable) { + return this.search(indexName, q, facetRequests, pageable, null); + } + + private FacetPage<T> search(String indexName, AbstractQueryBuilder<?> q, List<FacetRequest> facetRequests, Pageable pageable, + FieldsRequest fieldsRequest) { + final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(q); + + // Configure which fields must be included in ES response + if (fieldsRequest != null && (!fieldsRequest.getIncludes().isEmpty() || !fieldsRequest.getExcludes().isEmpty())) { + String[] includes = fieldsRequest.getIncludes().toArray(new String[0]); + String[] excludes = fieldsRequest.getExcludes().toArray(new String[0]); + searchSourceBuilder.fetchSource(includes, excludes); + } + final SearchRequest searchRequest = new SearchRequest(indexName.split(SolidifyConstants.FIELD_SEP)).source(searchSourceBuilder); // Aggregations diff --git a/solidify-model/src/main/java/ch/unige/solidify/rest/FieldsRequest.java b/solidify-model/src/main/java/ch/unige/solidify/rest/FieldsRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..36793ec6bc1c035292afad5f824d26283d502d5b --- /dev/null +++ b/solidify-model/src/main/java/ch/unige/solidify/rest/FieldsRequest.java @@ -0,0 +1,51 @@ +/*- + * %%---------------------------------------------------------------------------------------------- + * Solidify Framework - Solidify Model - FieldsRequest.java + * SPDX-License-Identifier: GPL-2.0-or-later + * %----------------------------------------------------------------------------------------------% + * Copyright (C) 2017 - 2022 University of Geneva + * %----------------------------------------------------------------------------------------------% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-2.0.html>. + * ----------------------------------------------------------------------------------------------%% + */ + +package ch.unige.solidify.rest; + +import java.util.ArrayList; +import java.util.List; + +public class FieldsRequest { + // List of fields that must be included in response + private List<String> includes = new ArrayList<>(); + + // List of fields that must be excluded from response + private List<String> excludes = new ArrayList<>(); + + public List<String> getIncludes() { + return this.includes; + } + + public void setIncludes(List<String> includes) { + this.includes = includes; + } + + public List<String> getExcludes() { + return this.excludes; + } + + public void setExcludes(List<String> excludes) { + this.excludes = excludes; + } +}