Skip to content

Commit 6ffac62

Browse files
committed
DATAES-180 - clean up
1 parent fd06f8e commit 6ffac62

36 files changed

+1658
-45
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@
3535
import org.elasticsearch.action.search.SearchResponse;
3636
import org.elasticsearch.search.SearchHit;
3737
import org.elasticsearch.search.SearchHitField;
38+
import org.slf4j.Logger;
39+
import org.slf4j.LoggerFactory;
3840
import org.springframework.data.domain.Page;
3941
import org.springframework.data.domain.Pageable;
4042
import org.springframework.data.elasticsearch.ElasticsearchException;
4143
import org.springframework.data.elasticsearch.annotations.Document;
4244
import org.springframework.data.elasticsearch.annotations.ScriptedField;
43-
import org.springframework.data.elasticsearch.core.domain.impl.AggregatedPageImpl;
45+
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
46+
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
4447
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
4548
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
4649
import org.springframework.data.mapping.PersistentProperty;
@@ -52,6 +55,8 @@
5255
*/
5356
public class DefaultResultMapper extends AbstractResultMapper {
5457

58+
private static final Logger LOG = LoggerFactory.getLogger(DefaultResultMapper.class);
59+
5560
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
5661

5762
public DefaultResultMapper() {
@@ -75,7 +80,7 @@ public DefaultResultMapper(
7580
}
7681

7782
@Override
78-
public <T> Page<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
83+
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
7984
long totalHits = response.getHits().totalHits();
8085
List<T> results = new ArrayList<T>();
8186
for (SearchHit hit : response.getHits()) {
@@ -180,7 +185,7 @@ private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
180185
try {
181186
setter.invoke(result, id);
182187
} catch (Throwable t) {
183-
t.printStackTrace();
188+
LOG.error("Unable to set entity Id", t);
184189
}
185190
}
186191
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@
8181
import org.springframework.data.elasticsearch.annotations.Document;
8282
import org.springframework.data.elasticsearch.annotations.Mapping;
8383
import org.springframework.data.elasticsearch.annotations.Setting;
84+
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
85+
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
8486
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
8587
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
88+
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
8689
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
8790
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
8891
import org.springframework.data.elasticsearch.core.query.*;
@@ -261,12 +264,12 @@ public <T> T queryForObject(StringQuery query, Class<T> clazz) {
261264
}
262265

263266
@Override
264-
public <T> Page<T> queryForPage(SearchQuery query, Class<T> clazz) {
267+
public <T> AggregatedPage<T> queryForPage(SearchQuery query, Class<T> clazz) {
265268
return queryForPage(query, clazz, resultsMapper);
266269
}
267270

268271
@Override
269-
public <T> Page<T> queryForPage(SearchQuery query, Class<T> clazz, SearchResultMapper mapper) {
272+
public <T> AggregatedPage<T> queryForPage(SearchQuery query, Class<T> clazz, SearchResultMapper mapper) {
270273
SearchResponse response = doSearch(prepareSearch(query, clazz), query);
271274
return mapper.mapResults(response, clazz, query.getPageable());
272275
}
@@ -410,6 +413,11 @@ public T next() {
410413
}
411414
throw new NoSuchElementException();
412415
}
416+
417+
@Override
418+
public void remove() {
419+
throw new UnsupportedOperationException("remove");
420+
}
413421
};
414422
}
415423

@@ -664,14 +672,14 @@ public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
664672
while (hasRecords) {
665673
Page<String> page = scroll(scrollId, scrollTimeInMillis, new SearchResultMapper() {
666674
@Override
667-
public <T> Page<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
675+
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
668676
List<String> result = new ArrayList<String>();
669677
for (SearchHit searchHit : response.getHits()) {
670678
String id = searchHit.getId();
671679
result.add(id);
672680
}
673681
if (result.size() > 0) {
674-
return new PageImpl<T>((List<T>) result);
682+
return new AggregatedPageImpl<T>((List<T>) result);
675683
}
676684
return null;
677685
}
@@ -880,6 +888,12 @@ private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery
880888
searchRequest.addAggregation(aggregationBuilder);
881889
}
882890
}
891+
892+
if (!isEmpty(searchQuery.getFacets())) {
893+
for (FacetRequest aggregatedFacet : searchQuery.getFacets()) {
894+
searchRequest.addAggregation(aggregatedFacet.getFacet());
895+
}
896+
}
883897
return getSearchResponse(searchRequest.setQuery(searchQuery.getQuery()).execute());
884898
}
885899

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2014 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;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.domain.Page;
21+
import org.springframework.data.elasticsearch.core.facet.FacetResult;
22+
23+
/**
24+
* @author Rizwan Idrees
25+
* @author Mohsin Husen
26+
* @author Artur Konczak
27+
* @author Jonathan Yan
28+
*/
29+
@Deprecated
30+
public interface FacetedPage<T> extends Page<T> {
31+
32+
boolean hasFacets();
33+
34+
List<FacetResult> getFacets();
35+
36+
FacetResult getFacet(String name);
37+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2014 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;
17+
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.elasticsearch.search.aggregations.Aggregation;
24+
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
25+
import org.elasticsearch.search.aggregations.bucket.range.Range;
26+
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
27+
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
28+
import org.springframework.data.domain.PageImpl;
29+
import org.springframework.data.domain.Pageable;
30+
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
31+
import org.springframework.data.elasticsearch.core.facet.FacetResult;
32+
import org.springframework.data.elasticsearch.core.facet.result.*;
33+
34+
/**
35+
* Container for query result and facet results
36+
*
37+
* @author Rizwan Idrees
38+
* @author Mohsin Husen
39+
* @author Artur Konczak
40+
* @author Jonathan Yan
41+
*/
42+
@Deprecated
43+
public abstract class FacetedPageImpl<T> extends PageImpl<T> implements FacetedPage<T>, AggregatedPage<T> {
44+
45+
private List<FacetResult> facets;
46+
private Map<String, FacetResult> mapOfFacets = new HashMap<String, FacetResult>();
47+
48+
public FacetedPageImpl(List<T> content) {
49+
super(content);
50+
}
51+
52+
public FacetedPageImpl(List<T> content, Pageable pageable, long total) {
53+
super(content, pageable, total);
54+
}
55+
56+
@Override
57+
public boolean hasFacets() {
58+
processAggregations();
59+
return facets != null && facets.size() > 0;
60+
}
61+
62+
@Override
63+
public List<FacetResult> getFacets() {
64+
processAggregations();
65+
return facets;
66+
}
67+
68+
@Override
69+
public FacetResult getFacet(String name) {
70+
processAggregations();
71+
return mapOfFacets.get(name);
72+
}
73+
74+
private void addFacet(FacetResult facetResult) {
75+
facets.add(facetResult);
76+
mapOfFacets.put(facetResult.getName(), facetResult);
77+
}
78+
79+
/**
80+
* Lazy conversion from aggregation to old facets
81+
*/
82+
private void processAggregations() {
83+
if (facets == null) {
84+
facets = new ArrayList<FacetResult>();
85+
for (Aggregation agg : getAggregations()) {
86+
if (agg instanceof Terms) {
87+
List<Term> terms = new ArrayList<Term>();
88+
for (Terms.Bucket t : ((Terms) agg).getBuckets()) {
89+
terms.add(new Term(t.getKeyAsString(), t.getDocCount()));
90+
}
91+
addFacet(new TermResult(agg.getName(), terms, terms.size(), ((Terms) agg).getSumOfOtherDocCounts(), -1));
92+
}
93+
if (agg instanceof Range) {
94+
List<? extends Range.Bucket> buckets = ((Range) agg).getBuckets();
95+
List<org.springframework.data.elasticsearch.core.facet.result.Range> ranges = new ArrayList<org.springframework.data.elasticsearch.core.facet.result.Range>();
96+
for (Range.Bucket b : buckets) {
97+
ranges.add(new org.springframework.data.elasticsearch.core.facet.result.Range((Double) b.getFrom(), (Double) b.getTo(), b.getDocCount(), 0, 0, 0, 0));
98+
}
99+
addFacet(new RangeResult(agg.getName(), ranges));
100+
}
101+
if (agg instanceof Stats) {
102+
Stats stats = (Stats) agg;
103+
addFacet(new StatisticalResult(agg.getName(), stats.getCount(), stats.getMax(), stats.getMin(), stats.getAvg(), -1, -1, stats.getSum(), -1));
104+
}
105+
if (agg instanceof Histogram) {
106+
List<IntervalUnit> intervals = new ArrayList<IntervalUnit>();
107+
for (Histogram.Bucket h : ((Histogram) agg).getBuckets()) {
108+
new IntervalUnit((Long) h.getKey(), h.getDocCount(), h.getDocCount(), -1, -1, -1, -1);
109+
}
110+
addFacet(new HistogramResult(agg.getName(), intervals));
111+
}
112+
}
113+
}
114+
}
115+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
package org.springframework.data.elasticsearch.core;
1717

1818
import org.elasticsearch.action.search.SearchResponse;
19-
import org.springframework.data.domain.Page;
2019
import org.springframework.data.domain.Pageable;
21-
import org.springframework.data.elasticsearch.core.domain.AggregatedPage;
20+
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
2221

2322
/**
2423
* @author Artur Konczak
2524
* @author Petar Tahchiev
2625
*/
2726
public interface SearchResultMapper {
2827

29-
<T> Page<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable);
28+
<T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable);
3029
}

src/main/java/org/springframework/data/elasticsearch/core/domain/AggregatedPage.java renamed to src/main/java/org/springframework/data/elasticsearch/core/aggregation/AggregatedPage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
package org.springframework.data.elasticsearch.core.domain;
1+
package org.springframework.data.elasticsearch.core.aggregation;
22

33
import org.elasticsearch.search.aggregations.Aggregation;
44
import org.elasticsearch.search.aggregations.Aggregations;
5+
import org.springframework.data.elasticsearch.core.FacetedPage;
56

67
/**
78
* @author Petar Tahchiev
89
*/
9-
public interface AggregatedPage<T> {
10+
public interface AggregatedPage<T> extends FacetedPage<T> {
1011

1112
boolean hasAggregations();
1213

src/main/java/org/springframework/data/elasticsearch/core/domain/impl/AggregatedPageImpl.java renamed to src/main/java/org/springframework/data/elasticsearch/core/aggregation/impl/AggregatedPageImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
package org.springframework.data.elasticsearch.core.domain.impl;
1+
package org.springframework.data.elasticsearch.core.aggregation.impl;
22

33
import java.util.HashMap;
44
import java.util.List;
55
import java.util.Map;
66

77
import org.elasticsearch.search.aggregations.Aggregation;
88
import org.elasticsearch.search.aggregations.Aggregations;
9-
import org.springframework.data.domain.PageImpl;
109
import org.springframework.data.domain.Pageable;
11-
import org.springframework.data.elasticsearch.core.domain.AggregatedPage;
10+
import org.springframework.data.elasticsearch.core.FacetedPageImpl;
11+
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
1212

1313
/**
1414
* @author Petar Tahchiev
15+
* @author Artur Konczak
16+
* @author Mohsin Husen
1517
*/
16-
public class AggregatedPageImpl<T> extends PageImpl<T> implements AggregatedPage<T> {
18+
public class AggregatedPageImpl<T> extends FacetedPageImpl<T> implements AggregatedPage<T> {
1719

1820
private Aggregations aggregations;
1921
private Map<String, Aggregation> mapOfAggregations = new HashMap<String, Aggregation>();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2014 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+
17+
package org.springframework.data.elasticsearch.core.facet;
18+
19+
import org.springframework.util.Assert;
20+
21+
22+
/**
23+
* @author Artur Konczak
24+
*/
25+
@Deprecated
26+
public abstract class AbstractFacetRequest implements FacetRequest {
27+
28+
private String name;
29+
private boolean applyQueryFilter;
30+
31+
public AbstractFacetRequest(String name) {
32+
Assert.hasText(name, "Facet can't be null or empty !!!");
33+
this.name = name;
34+
}
35+
36+
protected String getName() {
37+
return name;
38+
}
39+
40+
public void setApplyQueryFilter(boolean applyQueryFilter) {
41+
this.applyQueryFilter = applyQueryFilter;
42+
}
43+
44+
@Override
45+
public boolean applyQueryFilter() {
46+
return applyQueryFilter;
47+
}
48+
}

0 commit comments

Comments
 (0)