Skip to content
Snippets Groups Projects
Commit bfa61553 authored by Nicolas.Rod's avatar Nicolas.Rod Committed by Mathieu.Vonlanthen
Browse files

feat(ElasticsearchService): allow to configure which fields must be included...

feat(ElasticsearchService): allow to configure which fields must be included in ElasticSearch response
parent 970d8230
No related branches found
No related tags found
1 merge request!345feat(ElasticsearchService): allow to configure which fields must be included...
......@@ -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);
}
......
......@@ -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
......
/*-
* %%----------------------------------------------------------------------------------------------
* 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;
}
}
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