Skip to content

Commit c4b4a8c

Browse files
committed
DATAES-188 - Source filtering feature Implementation
1 parent 1cf18a4 commit c4b4a8c

File tree

8 files changed

+186
-8
lines changed

8 files changed

+186
-8
lines changed

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,11 @@ private SearchRequestBuilder prepareSearch(Query query) {
954954
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(toArray(query.getIndices()))
955955
.setSearchType(query.getSearchType()).setTypes(toArray(query.getTypes()));
956956

957+
if (query.getSourceFilter() != null) {
958+
SourceFilter sourceFilter = query.getSourceFilter();
959+
searchRequestBuilder.setFetchSource(sourceFilter.getIncludes(), sourceFilter.getExcludes());
960+
}
961+
957962
if (query.getPageable() != null) {
958963
startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
959964
searchRequestBuilder.setSize(query.getPageable().getPageSize());

src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ abstract class AbstractQuery implements Query {
3939
protected List<String> indices = new ArrayList<String>();
4040
protected List<String> types = new ArrayList<String>();
4141
protected List<String> fields = new ArrayList<String>();
42+
protected SourceFilter sourceFilter;
4243
protected float minScore;
4344
protected Collection<String> ids;
4445
protected String route;
@@ -91,6 +92,16 @@ public List<String> getTypes() {
9192
return types;
9293
}
9394

95+
@Override
96+
public void addSourceFilter(SourceFilter sourceFilter) {
97+
this.sourceFilter = sourceFilter;
98+
}
99+
100+
@Override
101+
public SourceFilter getSourceFilter() {
102+
return sourceFilter;
103+
}
104+
94105
@SuppressWarnings("unchecked")
95106
public final <T extends Query> T addSort(Sort sort) {
96107
if (sort == null) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core.query;
17+
18+
/**
19+
* SourceFilter implementation for providing includes and excludes.
20+
*
21+
* @Author Jon Tsiros
22+
*/
23+
public class FetchSourceFilter implements SourceFilter {
24+
25+
private final String[] includes;
26+
private final String[] excludes;
27+
28+
public FetchSourceFilter(final String[] includes, final String[] excludes) {
29+
this.includes = includes;
30+
this.excludes = excludes;
31+
}
32+
33+
@Override
34+
public String[] getIncludes() {
35+
return includes;
36+
}
37+
38+
@Override
39+
public String[] getExcludes() {
40+
return excludes;
41+
}
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core.query;
17+
18+
/**
19+
* SourceFilter builder for providing includes and excludes.
20+
*
21+
* @Author Jon Tsiros
22+
*/
23+
public class FetchSourceFilterBuilder {
24+
25+
private String[] includes;
26+
private String[] excludes;
27+
28+
public FetchSourceFilterBuilder withIncludes(String... includes) {
29+
this.includes = includes;
30+
return this;
31+
}
32+
33+
public FetchSourceFilterBuilder withExcludes(String... excludes) {
34+
this.excludes = excludes;
35+
return this;
36+
}
37+
38+
public SourceFilter build() {
39+
if (includes == null) includes = new String[0];
40+
if (excludes == null) excludes = new String[0];
41+
42+
SourceFilter sourceFilter = new FetchSourceFilter(includes, excludes);
43+
return sourceFilter;
44+
}
45+
}

src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public class NativeSearchQueryBuilder {
4141
private QueryBuilder filterBuilder;
4242
private List<ScriptField> scriptFields = new ArrayList<ScriptField>();
4343
private List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
44-
/*private List<FacetRequest> facetRequests = new ArrayList<FacetRequest>();*/
4544
private List<AbstractAggregationBuilder> aggregationBuilders = new ArrayList<AbstractAggregationBuilder>();
4645
private HighlightBuilder.Field[] highlightFields;
4746
private Pageable pageable;
4847
private String[] indices;
4948
private String[] types;
5049
private String[] fields;
50+
private SourceFilter sourceFilter;
5151
private List<IndexBoost> indicesBoost;
5252
private float minScore;
5353
private Collection<String> ids;
@@ -114,6 +114,11 @@ public NativeSearchQueryBuilder withFields(String... fields) {
114114
return this;
115115
}
116116

117+
public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) {
118+
this.sourceFilter = sourceFilter;
119+
return this;
120+
}
121+
117122
public NativeSearchQueryBuilder withMinScore(float minScore) {
118123
this.minScore = minScore;
119124
return this;
@@ -151,6 +156,10 @@ public NativeSearchQuery build() {
151156
if (fields != null) {
152157
nativeSearchQuery.addFields(fields);
153158
}
159+
160+
if (sourceFilter != null) {
161+
nativeSearchQuery.addSourceFilter(sourceFilter);
162+
}
154163

155164
if(indicesBoost != null) {
156165
nativeSearchQuery.setIndicesBoost(indicesBoost);
@@ -160,10 +169,6 @@ public NativeSearchQuery build() {
160169
nativeSearchQuery.setScriptFields(scriptFields);
161170
}
162171

163-
/* if (CollectionUtils.isNotEmpty(facetRequests)) {
164-
nativeSearchQuery.setFacets(facetRequests);
165-
}*/
166-
167172
if (CollectionUtils.isNotEmpty(aggregationBuilders)) {
168173
nativeSearchQuery.setAggregations(aggregationBuilders);
169174
}

src/main/java/org/springframework/data/elasticsearch/core/query/Query.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -111,6 +111,21 @@ public interface Query {
111111
*/
112112
List<String> getFields();
113113

114+
/**
115+
* Add source filter to be added as part of search request
116+
*
117+
* @param sourceFilter
118+
*/
119+
void addSourceFilter(SourceFilter sourceFilter);
120+
121+
/**
122+
* Get SourceFilter to be returned to get include and exclude source
123+
* fields as part of search request.
124+
*
125+
* @return SourceFilter
126+
*/
127+
SourceFilter getSourceFilter();
128+
114129
/**
115130
* Get minimum score
116131
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core.query;
17+
18+
/**
19+
* SourceFilter for providing includes and excludes.
20+
*
21+
* @Author Jon Tsiros
22+
*/
23+
public interface SourceFilter {
24+
25+
String[] getIncludes();
26+
27+
String[] getExcludes();
28+
}

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,8 @@ public void shouldUseScriptedFields() {
489489
SearchQuery searchQuery = new NativeSearchQueryBuilder()
490490
.withQuery(matchAllQuery())
491491
.withScriptField(new ScriptField("scriptedRate",
492-
new Script("doc['rate'].value * factor", ScriptService.ScriptType.INLINE, null , params)))
493-
.build();
492+
new Script("doc['rate'].value * factor", ScriptService.ScriptType.INLINE, null, params)))
493+
.build();
494494
Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
495495
// then
496496
assertThat(sampleEntities.getTotalElements(), equalTo(1L));
@@ -646,6 +646,33 @@ public <T> Page<T> mapResults(SearchResponse response, Class<T> clazz, Pageable
646646
assertThat(page.getContent().get(0), is(message));
647647
}
648648

649+
@Test
650+
public void shouldReturnFieldsBasedOnSourceFilter() {
651+
// given
652+
String documentId = randomNumeric(5);
653+
String message = "some test message";
654+
SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message(message)
655+
.version(System.currentTimeMillis()).build();
656+
657+
IndexQuery indexQuery = getIndexQuery(sampleEntity);
658+
659+
elasticsearchTemplate.index(indexQuery);
660+
elasticsearchTemplate.refresh(SampleEntity.class);
661+
662+
FetchSourceFilterBuilder sourceFilter = new FetchSourceFilterBuilder();
663+
sourceFilter.withIncludes("message");
664+
665+
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withIndices(INDEX_NAME)
666+
.withTypes(TYPE_NAME).withSourceFilter(sourceFilter.build()).build();
667+
// when
668+
Page<SampleEntity> page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
669+
// then
670+
assertThat(page, is(notNullValue()));
671+
assertThat(page.getTotalElements(), is(equalTo(1L)));
672+
assertThat(page.getContent().get(0).getMessage(), is(message));
673+
}
674+
675+
649676
@Test
650677
public void shouldReturnSimilarResultsGivenMoreLikeThisQuery() {
651678
// given

0 commit comments

Comments
 (0)