From 6ffac62b3ee9d0cf00b2f3c476ea5fa06e9c08d0 Mon Sep 17 00:00:00 2001 From: Artur Konczak Date: Fri, 27 May 2016 17:33:56 +0100 Subject: [PATCH] DATAES-180 - clean up --- .../core/DefaultResultMapper.java | 11 +- .../core/ElasticsearchTemplate.java | 22 +++- .../data/elasticsearch/core/FacetedPage.java | 37 ++++++ .../elasticsearch/core/FacetedPageImpl.java | 115 ++++++++++++++++ .../core/SearchResultMapper.java | 5 +- .../AggregatedPage.java | 5 +- .../impl/AggregatedPageImpl.java | 10 +- .../core/facet/AbstractFacetRequest.java | 48 +++++++ .../core/facet/AbstractFacetResult.java | 47 +++++++ .../core/facet/DefaultFacetMapper.java | 81 ++++++++++++ .../core/facet/FacetRequest.java | 33 +++++ .../elasticsearch/core/facet/FacetResult.java | 32 +++++ .../elasticsearch/core/facet/FacetType.java | 27 ++++ .../facet/request/HistogramFacetRequest.java | 71 ++++++++++ .../request/HistogramFacetRequestBuilder.java | 58 ++++++++ .../facet/request/NativeFacetRequest.java | 43 ++++++ .../core/facet/request/RangeFacetRequest.java | 124 ++++++++++++++++++ .../request/RangeFacetRequestBuilder.java | 85 ++++++++++++ .../request/StatisticalFacetRequest.java | 53 ++++++++ .../StatisticalFacetRequestBuilder.java | 52 ++++++++ .../core/facet/request/TermFacetOrder.java | 27 ++++ .../core/facet/request/TermFacetRequest.java | 107 +++++++++++++++ .../request/TermFacetRequestBuilder.java | 88 +++++++++++++ .../core/facet/result/HistogramResult.java | 39 ++++++ .../core/facet/result/IntervalUnit.java | 92 +++++++++++++ .../core/facet/result/Range.java | 79 +++++++++++ .../core/facet/result/RangeResult.java | 44 +++++++ .../core/facet/result/StatisticalResult.java | 86 ++++++++++++ .../elasticsearch/core/facet/result/Term.java | 44 +++++++ .../core/facet/result/TermResult.java | 63 +++++++++ .../core/query/NativeSearchQuery.java | 7 +- .../core/query/NativeSearchQueryBuilder.java | 10 +- .../elasticsearch/core/query/SearchQuery.java | 4 +- .../core/CustomResultMapper.java | 3 +- .../core/DefaultResultMapperTests.java | 8 +- .../core/ElasticsearchTemplateTests.java | 43 +++--- 36 files changed, 1658 insertions(+), 45 deletions(-) create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/FacetedPage.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/FacetedPageImpl.java rename src/main/java/org/springframework/data/elasticsearch/core/{domain => aggregation}/AggregatedPage.java (58%) rename src/main/java/org/springframework/data/elasticsearch/core/{domain => aggregation}/impl/AggregatedPageImpl.java (77%) create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetRequest.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetResult.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/DefaultFacetMapper.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/FacetRequest.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/FacetResult.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/FacetType.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequest.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequestBuilder.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/NativeFacetRequest.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequest.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequestBuilder.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequest.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequestBuilder.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetOrder.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequest.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequestBuilder.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/result/HistogramResult.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/result/IntervalUnit.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/result/Range.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/result/RangeResult.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/result/StatisticalResult.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/result/Term.java create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/facet/result/TermResult.java diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java index 99d6fbb38..9e5c3ce62 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java @@ -35,12 +35,15 @@ import org.elasticsearch.action.get.MultiGetResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHitField; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.ElasticsearchException; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.ScriptedField; -import org.springframework.data.elasticsearch.core.domain.impl.AggregatedPageImpl; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; +import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.mapping.PersistentProperty; @@ -52,6 +55,8 @@ import org.springframework.data.mapping.context.MappingContext; */ public class DefaultResultMapper extends AbstractResultMapper { + private static final Logger LOG = LoggerFactory.getLogger(DefaultResultMapper.class); + private MappingContext, ElasticsearchPersistentProperty> mappingContext; public DefaultResultMapper() { @@ -75,7 +80,7 @@ public class DefaultResultMapper extends AbstractResultMapper { } @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { long totalHits = response.getHits().totalHits(); List results = new ArrayList(); for (SearchHit hit : response.getHits()) { @@ -180,7 +185,7 @@ public class DefaultResultMapper extends AbstractResultMapper { try { setter.invoke(result, id); } catch (Throwable t) { - t.printStackTrace(); + LOG.error("Unable to set entity Id", t); } } } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java index 9bb081e16..673273daf 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java @@ -81,8 +81,11 @@ import org.springframework.data.elasticsearch.ElasticsearchException; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Mapping; import org.springframework.data.elasticsearch.annotations.Setting; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; +import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter; +import org.springframework.data.elasticsearch.core.facet.FacetRequest; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; import org.springframework.data.elasticsearch.core.query.*; @@ -261,12 +264,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati } @Override - public Page queryForPage(SearchQuery query, Class clazz) { + public AggregatedPage queryForPage(SearchQuery query, Class clazz) { return queryForPage(query, clazz, resultsMapper); } @Override - public Page queryForPage(SearchQuery query, Class clazz, SearchResultMapper mapper) { + public AggregatedPage queryForPage(SearchQuery query, Class clazz, SearchResultMapper mapper) { SearchResponse response = doSearch(prepareSearch(query, clazz), query); return mapper.mapResults(response, clazz, query.getPageable()); } @@ -410,6 +413,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati } throw new NoSuchElementException(); } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } }; } @@ -664,14 +672,14 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati while (hasRecords) { Page page = scroll(scrollId, scrollTimeInMillis, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List result = new ArrayList(); for (SearchHit searchHit : response.getHits()) { String id = searchHit.getId(); result.add(id); } if (result.size() > 0) { - return new PageImpl((List) result); + return new AggregatedPageImpl((List) result); } return null; } @@ -880,6 +888,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati searchRequest.addAggregation(aggregationBuilder); } } + + if (!isEmpty(searchQuery.getFacets())) { + for (FacetRequest aggregatedFacet : searchQuery.getFacets()) { + searchRequest.addAggregation(aggregatedFacet.getFacet()); + } + } return getSearchResponse(searchRequest.setQuery(searchQuery.getQuery()).execute()); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/FacetedPage.java b/src/main/java/org/springframework/data/elasticsearch/core/FacetedPage.java new file mode 100644 index 000000000..d1fad4756 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/FacetedPage.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core; + +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.elasticsearch.core.facet.FacetResult; + +/** + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + */ +@Deprecated +public interface FacetedPage extends Page { + + boolean hasFacets(); + + List getFacets(); + + FacetResult getFacet(String name); +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/FacetedPageImpl.java b/src/main/java/org/springframework/data/elasticsearch/core/FacetedPageImpl.java new file mode 100644 index 000000000..79d6d1fde --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/FacetedPageImpl.java @@ -0,0 +1,115 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; +import org.elasticsearch.search.aggregations.bucket.range.Range; +import org.elasticsearch.search.aggregations.bucket.terms.Terms; +import org.elasticsearch.search.aggregations.metrics.stats.Stats; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; +import org.springframework.data.elasticsearch.core.facet.FacetResult; +import org.springframework.data.elasticsearch.core.facet.result.*; + +/** + * Container for query result and facet results + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + */ +@Deprecated +public abstract class FacetedPageImpl extends PageImpl implements FacetedPage, AggregatedPage { + + private List facets; + private Map mapOfFacets = new HashMap(); + + public FacetedPageImpl(List content) { + super(content); + } + + public FacetedPageImpl(List content, Pageable pageable, long total) { + super(content, pageable, total); + } + + @Override + public boolean hasFacets() { + processAggregations(); + return facets != null && facets.size() > 0; + } + + @Override + public List getFacets() { + processAggregations(); + return facets; + } + + @Override + public FacetResult getFacet(String name) { + processAggregations(); + return mapOfFacets.get(name); + } + + private void addFacet(FacetResult facetResult) { + facets.add(facetResult); + mapOfFacets.put(facetResult.getName(), facetResult); + } + + /** + * Lazy conversion from aggregation to old facets + */ + private void processAggregations() { + if (facets == null) { + facets = new ArrayList(); + for (Aggregation agg : getAggregations()) { + if (agg instanceof Terms) { + List terms = new ArrayList(); + for (Terms.Bucket t : ((Terms) agg).getBuckets()) { + terms.add(new Term(t.getKeyAsString(), t.getDocCount())); + } + addFacet(new TermResult(agg.getName(), terms, terms.size(), ((Terms) agg).getSumOfOtherDocCounts(), -1)); + } + if (agg instanceof Range) { + List buckets = ((Range) agg).getBuckets(); + List ranges = new ArrayList(); + for (Range.Bucket b : buckets) { + ranges.add(new org.springframework.data.elasticsearch.core.facet.result.Range((Double) b.getFrom(), (Double) b.getTo(), b.getDocCount(), 0, 0, 0, 0)); + } + addFacet(new RangeResult(agg.getName(), ranges)); + } + if (agg instanceof Stats) { + Stats stats = (Stats) agg; + addFacet(new StatisticalResult(agg.getName(), stats.getCount(), stats.getMax(), stats.getMin(), stats.getAvg(), -1, -1, stats.getSum(), -1)); + } + if (agg instanceof Histogram) { + List intervals = new ArrayList(); + for (Histogram.Bucket h : ((Histogram) agg).getBuckets()) { + new IntervalUnit((Long) h.getKey(), h.getDocCount(), h.getDocCount(), -1, -1, -1, -1); + } + addFacet(new HistogramResult(agg.getName(), intervals)); + } + } + } + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/SearchResultMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/SearchResultMapper.java index ed315284e..4e63f53fd 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/SearchResultMapper.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/SearchResultMapper.java @@ -16,9 +16,8 @@ package org.springframework.data.elasticsearch.core; import org.elasticsearch.action.search.SearchResponse; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.data.elasticsearch.core.domain.AggregatedPage; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; /** * @author Artur Konczak @@ -26,5 +25,5 @@ import org.springframework.data.elasticsearch.core.domain.AggregatedPage; */ public interface SearchResultMapper { - Page mapResults(SearchResponse response, Class clazz, Pageable pageable); + AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/domain/AggregatedPage.java b/src/main/java/org/springframework/data/elasticsearch/core/aggregation/AggregatedPage.java similarity index 58% rename from src/main/java/org/springframework/data/elasticsearch/core/domain/AggregatedPage.java rename to src/main/java/org/springframework/data/elasticsearch/core/aggregation/AggregatedPage.java index 2d7a00c93..77a8c740e 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/domain/AggregatedPage.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/aggregation/AggregatedPage.java @@ -1,12 +1,13 @@ -package org.springframework.data.elasticsearch.core.domain; +package org.springframework.data.elasticsearch.core.aggregation; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; +import org.springframework.data.elasticsearch.core.FacetedPage; /** * @author Petar Tahchiev */ -public interface AggregatedPage { +public interface AggregatedPage extends FacetedPage { boolean hasAggregations(); diff --git a/src/main/java/org/springframework/data/elasticsearch/core/domain/impl/AggregatedPageImpl.java b/src/main/java/org/springframework/data/elasticsearch/core/aggregation/impl/AggregatedPageImpl.java similarity index 77% rename from src/main/java/org/springframework/data/elasticsearch/core/domain/impl/AggregatedPageImpl.java rename to src/main/java/org/springframework/data/elasticsearch/core/aggregation/impl/AggregatedPageImpl.java index fba63ebf5..dbf7e369c 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/domain/impl/AggregatedPageImpl.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/aggregation/impl/AggregatedPageImpl.java @@ -1,4 +1,4 @@ -package org.springframework.data.elasticsearch.core.domain.impl; +package org.springframework.data.elasticsearch.core.aggregation.impl; import java.util.HashMap; import java.util.List; @@ -6,14 +6,16 @@ import java.util.Map; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; -import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; -import org.springframework.data.elasticsearch.core.domain.AggregatedPage; +import org.springframework.data.elasticsearch.core.FacetedPageImpl; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; /** * @author Petar Tahchiev + * @author Artur Konczak + * @author Mohsin Husen */ -public class AggregatedPageImpl extends PageImpl implements AggregatedPage { +public class AggregatedPageImpl extends FacetedPageImpl implements AggregatedPage { private Aggregations aggregations; private Map mapOfAggregations = new HashMap(); diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetRequest.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetRequest.java new file mode 100644 index 000000000..7008f8f14 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetRequest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet; + +import org.springframework.util.Assert; + + +/** + * @author Artur Konczak + */ +@Deprecated +public abstract class AbstractFacetRequest implements FacetRequest { + + private String name; + private boolean applyQueryFilter; + + public AbstractFacetRequest(String name) { + Assert.hasText(name, "Facet can't be null or empty !!!"); + this.name = name; + } + + protected String getName() { + return name; + } + + public void setApplyQueryFilter(boolean applyQueryFilter) { + this.applyQueryFilter = applyQueryFilter; + } + + @Override + public boolean applyQueryFilter() { + return applyQueryFilter; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetResult.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetResult.java new file mode 100644 index 000000000..0dd050dc5 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetResult.java @@ -0,0 +1,47 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet; + +import org.springframework.util.Assert; + +/** + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + */ +@Deprecated +public class AbstractFacetResult implements FacetResult { + + private final String name; + private final FacetType type; + + protected AbstractFacetResult(String name, FacetType type) { + Assert.hasText(name, "Facet name can't be null and should have a value"); + this.name = name; + this.type = type; + } + + @Override + public String getName() { + return name; + } + + @Override + public FacetType getType() { + return type; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/DefaultFacetMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/DefaultFacetMapper.java new file mode 100644 index 000000000..0195e02b1 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/DefaultFacetMapper.java @@ -0,0 +1,81 @@ +///* +// * Copyright 2014 the original author or authors. +// * +// * Licensed under the Apache License, Version 2.0 (the "License"); +// * you may not use this file except in compliance with the License. +// * You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +//package org.springframework.data.elasticsearch.core.facet; +// +//import java.util.ArrayList; +//import java.util.List; +// +//import org.elasticsearch.search.facet.Facet; +//import org.elasticsearch.search.facet.histogram.HistogramFacet; +//import org.elasticsearch.search.facet.range.RangeFacet; +//import org.elasticsearch.search.facet.statistical.StatisticalFacet; +//import org.elasticsearch.search.facet.terms.TermsFacet; +//import org.springframework.data.elasticsearch.core.facet.result.*; +// +///** +// * @author Artur Konczak +// * @author Petar Tahchiev +// */ +//public class DefaultFacetMapper { +// +// public static FacetResult parse(Facet facet) { +// if (facet instanceof TermsFacet) { +// return parseTerm((TermsFacet) facet); +// } +// +// if (facet instanceof RangeFacet) { +// return parseRange((RangeFacet) facet); +// } +// +// if (facet instanceof StatisticalFacet) { +// return parseStatistical((StatisticalFacet) facet); +// } +// +// if (facet instanceof HistogramFacet) { +// return parseHistogram((HistogramFacet) facet); +// } +// +// return null; +// } +// +// private static FacetResult parseTerm(TermsFacet facet) { +// List entries = new ArrayList(); +// for (TermsFacet.Entry entry : facet.getEntries()) { +// entries.add(new Term(entry.getTerm().toString(), entry.getCount())); +// } +// return new TermResult(facet.getName(), entries, facet.getTotalCount(), facet.getOtherCount(), facet.getMissingCount()); +// } +// +// private static FacetResult parseRange(RangeFacet facet) { +// List entries = new ArrayList(); +// for (RangeFacet.Entry entry : facet.getEntries()) { +// entries.add(new Range(entry.getFrom() == Double.NEGATIVE_INFINITY ? null : entry.getFrom(), entry.getTo() == Double.POSITIVE_INFINITY ? null : entry.getTo(), entry.getCount(), entry.getTotal(), entry.getTotalCount(), entry.getMin(), entry.getMax())); +// } +// return new RangeResult(facet.getName(), entries); +// } +// +// private static FacetResult parseStatistical(StatisticalFacet facet) { +// return new StatisticalResult(facet.getName(), facet.getCount(), facet.getMax(), facet.getMin(), facet.getMean(), facet.getStdDeviation(), facet.getSumOfSquares(), facet.getTotal(), facet.getVariance()); +// } +// +// private static FacetResult parseHistogram(HistogramFacet facet) { +// List entries = new ArrayList(); +// for (HistogramFacet.Entry entry : facet.getEntries()) { +// entries.add(new IntervalUnit(entry.getKey(), entry.getCount(), entry.getTotalCount(), entry.getTotal(), entry.getMean(), entry.getMin(), entry.getMax())); +// } +// return new HistogramResult(facet.getName(), entries); +// } +//} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetRequest.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetRequest.java new file mode 100644 index 000000000..8827d719e --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetRequest.java @@ -0,0 +1,33 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet; + +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; + +/** + * @author Artur Koczak + */ +@Deprecated +public interface FacetRequest { + + public static final String FIELD_UNTOUCHED = "untouched"; + public static final String FIELD_SORT = "sort"; + + AbstractAggregationBuilder getFacet(); + + boolean applyQueryFilter(); +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetResult.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetResult.java new file mode 100644 index 000000000..84cd55930 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetResult.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet; + +/** + * Generic interface for all facets + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + */ +@Deprecated +public interface FacetResult { + + String getName(); + + FacetType getType(); +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetType.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetType.java new file mode 100644 index 000000000..4a6914e0a --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/FacetType.java @@ -0,0 +1,27 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet; + +/** + * @author Artur Konczak + * @author Petar Tahchiev + */ +@Deprecated +public enum FacetType { + + term, range, histogram, statistical + +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequest.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequest.java new file mode 100644 index 000000000..83d7e78b6 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import org.apache.commons.lang.StringUtils; +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramBuilder; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; +import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest; +import org.springframework.util.Assert; + + +/** + * @author Artur Konczak + * @author Mohsin Husen + */ +@Deprecated +public class HistogramFacetRequest extends AbstractFacetRequest { + + private String field; + private long interval; + private DateHistogramInterval timeUnit; + + public HistogramFacetRequest(String name) { + super(name); + } + + public void setField(String field) { + this.field = field; + } + + public void setInterval(long interval) { + this.interval = interval; + } + + public void setTimeUnit(DateHistogramInterval timeUnit) { + this.timeUnit = timeUnit; + } + + public AbstractAggregationBuilder getFacet() { + Assert.notNull(getName(), "Facet name can't be a null !!!"); + Assert.isTrue(StringUtils.isNotBlank(field), "Please select field on which to build the facet !!!"); + Assert.isTrue(interval > 0, "Please provide interval as positive value greater them zero !!!"); + + DateHistogramBuilder dateHistogramBuilder = AggregationBuilders.dateHistogram(getName()); + dateHistogramBuilder.field(field); + + if (timeUnit != null) { + dateHistogramBuilder.interval(timeUnit); + } else { + dateHistogramBuilder.interval(interval); + } + + return dateHistogramBuilder; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequestBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequestBuilder.java new file mode 100644 index 000000000..0c339cc29 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequestBuilder.java @@ -0,0 +1,58 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; +import org.springframework.data.elasticsearch.core.facet.FacetRequest; + + +/** + * @author Artur Konczak + */ +@Deprecated +public class HistogramFacetRequestBuilder { + + HistogramFacetRequest result; + + public HistogramFacetRequestBuilder(String name) { + result = new HistogramFacetRequest(name); + } + + public HistogramFacetRequestBuilder field(String field) { + result.setField(field); + return this; + } + + public HistogramFacetRequestBuilder interval(long interval) { + result.setInterval(interval); + return this; + } + + public HistogramFacetRequestBuilder timeUnit(DateHistogramInterval timeUnit) { + result.setTimeUnit(timeUnit); + return this; + } + + public FacetRequest build() { + return result; + } + + public HistogramFacetRequestBuilder applyQueryFilter() { + result.setApplyQueryFilter(true); + return this; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/NativeFacetRequest.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/NativeFacetRequest.java new file mode 100644 index 000000000..efba14a0c --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/NativeFacetRequest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; +import org.springframework.data.elasticsearch.core.facet.FacetRequest; + + +/** + * @author Artur Konczak + * @author Mohsin Husen + */ +@Deprecated +public class NativeFacetRequest implements FacetRequest { + + public NativeFacetRequest() { + throw new UnsupportedOperationException("Native Facet are not supported in Elasticsearch 2.x - use Aggregation"); + } + + @Override + public AbstractAggregationBuilder getFacet() { + return null; + } + + @Override + public boolean applyQueryFilter() { + return false; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequest.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequest.java new file mode 100644 index 000000000..063bf7ea5 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequest.java @@ -0,0 +1,124 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import java.util.ArrayList; +import java.util.List; + +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.bucket.range.RangeBuilder; +import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest; +import org.springframework.util.Assert; + + +/** + * Range facet for numeric fields + * + * @author Artur Konczak + * @author Akos Bordas + */ +@Deprecated +public class RangeFacetRequest extends AbstractFacetRequest { + + private String field; + private String keyField; + private String valueField; + + private List entries = new ArrayList(); + + public RangeFacetRequest(String name) { + super(name); + } + + public void setField(String field) { + this.field = field; + } + + public void setFields(String keyField, String valueField) { + throw new UnsupportedOperationException("Native Facet are not supported in Elasticsearch 2.x - use Aggregation"); + } + + public void range(Double from, Double to) { + entries.add(new DoubleEntry(from, to)); + } + + public void range(String from, String to) { + throw new UnsupportedOperationException("Native Facet are not supported in Elasticsearch 2.x - use Aggregation"); + } + + public void addRange(Double from, Double to) { + entries.add(new DoubleEntry(from, to)); + } + + public void addRange(String from, String to) { + throw new UnsupportedOperationException("Native Facet are not supported in Elasticsearch 2.x - use Aggregation"); + } + + @Override + public AbstractAggregationBuilder getFacet() { + Assert.notNull(getName(), "Facet name can't be a null !!!"); + + RangeBuilder rangeBuilder = AggregationBuilders.range(getName()); + rangeBuilder.field(field); + + for (Entry entry : entries) { + DoubleEntry doubleEntry = (DoubleEntry) entry; + rangeBuilder.addRange(validateValue(doubleEntry.getFrom(), Double.NEGATIVE_INFINITY), validateValue(doubleEntry.getTo(), Double.POSITIVE_INFINITY)); + } + + return rangeBuilder; + } + + private double validateValue(Double value, double defaultValue) { + return value == null ? defaultValue : value; + } + + static class DoubleEntry extends Entry { + + DoubleEntry(Double from, Double to) { + super(from, to); + } + } + + static class StringEntry extends Entry { + + StringEntry(String from, String to) { + super(from, to); + } + } + + static class Entry { + + T from; + T to; + + Entry(T from, T to) { + this.from = from; + this.to = to; + } + + public T getFrom() { + return from; + } + + public T getTo() { + return to; + } + } +} + diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequestBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequestBuilder.java new file mode 100644 index 000000000..63b8ce916 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequestBuilder.java @@ -0,0 +1,85 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import org.springframework.data.elasticsearch.core.facet.FacetRequest; + + +/** + * Basic range facet + * + * @author Artur Konczak + */ +@Deprecated +public class RangeFacetRequestBuilder { + + RangeFacetRequest result; + + public RangeFacetRequestBuilder(String name) { + result = new RangeFacetRequest(name); + } + + public RangeFacetRequestBuilder field(String field) { + result.setField(field); + return this; + } + + public RangeFacetRequestBuilder fields(String keyField, String valueField) { + result.setFields(keyField, valueField); + return this; + } + + + public RangeFacetRequestBuilder range(double from, double to) { + result.range(from, to); + return this; + } + + public RangeFacetRequestBuilder range(String from, String to) { + result.range(from, to); + return this; + } + + public RangeFacetRequestBuilder from(double from) { + result.range(from, null); + return this; + } + + public RangeFacetRequestBuilder to(double to) { + result.range(null, to); + return this; + } + + public RangeFacetRequestBuilder from(String from) { + result.range(from, null); + return this; + } + + public RangeFacetRequestBuilder to(String to) { + result.range(null, to); + return this; + } + + public RangeFacetRequestBuilder applyQueryFilter() { + result.setApplyQueryFilter(true); + return this; + } + + public FacetRequest build() { + return result; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequest.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequest.java new file mode 100644 index 000000000..f7170222a --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import org.apache.commons.lang.StringUtils; +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest; +import org.springframework.util.Assert; + + +/** + * @author Petar Tahchiev + */ +@Deprecated +public class StatisticalFacetRequest extends AbstractFacetRequest { + + private String field; + + private String[] fields; + + public StatisticalFacetRequest(String name) { + super(name); + } + + public void setField(String field) { + this.field = field; + } + + public void setFields(String... fields) { + throw new UnsupportedOperationException("Native Facet are not supported in Elasticsearch 2.x - use Aggregation"); + } + + public AbstractAggregationBuilder getFacet() { + Assert.notNull(getName(), "Facet name can't be a null !!!"); + Assert.isTrue(StringUtils.isNotBlank(field) && fields == null, "Please select field or fields on which to build the facets !!!"); + return AggregationBuilders.stats(getName()).field(field); + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequestBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequestBuilder.java new file mode 100644 index 000000000..8baa91027 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequestBuilder.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import org.springframework.data.elasticsearch.core.facet.FacetRequest; + + +/** + * @author Petar Tahchiev + */ +@Deprecated +public class StatisticalFacetRequestBuilder { + + StatisticalFacetRequest result; + + public StatisticalFacetRequestBuilder(String name) { + result = new StatisticalFacetRequest(name); + } + + public StatisticalFacetRequestBuilder field(String field) { + result.setField(field); + return this; + } + + public StatisticalFacetRequestBuilder fields(String... fields) { + result.setFields(fields); + return this; + } + + public StatisticalFacetRequestBuilder applyQueryFilter() { + result.setApplyQueryFilter(true); + return this; + } + + public FacetRequest build() { + return result; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetOrder.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetOrder.java new file mode 100644 index 000000000..064c40247 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetOrder.java @@ -0,0 +1,27 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet.request; + +/** + * @author Artur Konczak + * @author Mohsin Husen + */ +@Deprecated +public enum TermFacetOrder { + + ascTerm, descTerm, ascCount, descCount; + +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequest.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequest.java new file mode 100644 index 000000000..8a67ab2c5 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequest.java @@ -0,0 +1,107 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.bucket.terms.Terms; +import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; +import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest; +import org.springframework.util.Assert; + + +/** + * Term facet + * + * @author Artur Konczak + */ +@Deprecated +public class TermFacetRequest extends AbstractFacetRequest { + + private String[] fields; + private String[] excludeTerms; + private int size = 10; + private TermFacetOrder order = TermFacetOrder.descCount; + private boolean allTerms = false; + private String regex = null; + + public TermFacetRequest(String name) { + super(name); + } + + public void setFields(String... fields) { + Assert.isTrue(ArrayUtils.isNotEmpty(fields), "Term agg need one field"); + Assert.isTrue(ArrayUtils.getLength(fields) == 1, "Term agg need one field"); + this.fields = fields; + } + + public void setSize(int size) { + Assert.isTrue(size >= 0, "Size should be bigger then zero !!!"); + this.size = size; + } + + public void setOrder(TermFacetOrder order) { + this.order = order; + } + + public void setExcludeTerms(String... excludeTerms) { + this.excludeTerms = excludeTerms; + } + + public void setAllTerms(boolean allTerms) { + this.allTerms = allTerms; + } + + public void setRegex(String regex) { + this.regex = regex; + } + + @Override + public AbstractAggregationBuilder getFacet() { + Assert.notEmpty(fields, "Please select at last one field !!!"); + TermsBuilder termsBuilder = AggregationBuilders.terms(getName()).field(fields[0]).size(this.size); + + switch (order) { + case descTerm: + termsBuilder.order(Terms.Order.term(false)); + break; + case ascTerm: + termsBuilder.order(Terms.Order.term(true)); + break; + case ascCount: + termsBuilder.order(Terms.Order.count(true)); + break; + default: + termsBuilder.order(Terms.Order.count(false)); + } + if (ArrayUtils.isNotEmpty(excludeTerms)) { + termsBuilder.exclude(excludeTerms); + } + + if (allTerms) { + termsBuilder.size(Integer.MAX_VALUE); + } + + if (StringUtils.isNotBlank(regex)) { + termsBuilder.include(regex); + } + + return termsBuilder; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequestBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequestBuilder.java new file mode 100644 index 000000000..343db81aa --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequestBuilder.java @@ -0,0 +1,88 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.elasticsearch.core.facet.request; + +import org.springframework.data.elasticsearch.core.facet.FacetRequest; + +/** + * Basic term facet + * + * @author Artur Konczak + */ +@Deprecated +public class TermFacetRequestBuilder { + + private TermFacetRequest result; + + public TermFacetRequestBuilder(String name) { + result = new TermFacetRequest(name); + } + + public TermFacetRequestBuilder fields(String... fields) { + result.setFields(fields); + return this; + } + + public TermFacetRequestBuilder size(int size) { + result.setSize(size); + return this; + } + + public TermFacetRequestBuilder excludeTerms(String... terms) { + result.setExcludeTerms(terms); + return this; + } + + public TermFacetRequestBuilder allTerms() { + result.setAllTerms(true); + return this; + } + + public TermFacetRequestBuilder regex(String regex) { + result.setRegex(regex); + return this; + } + + public TermFacetRequestBuilder ascTerm() { + result.setOrder(TermFacetOrder.ascTerm); + return this; + } + + public TermFacetRequestBuilder descTerm() { + result.setOrder(TermFacetOrder.descTerm); + return this; + } + + public TermFacetRequestBuilder ascCount() { + result.setOrder(TermFacetOrder.ascCount); + return this; + } + + public TermFacetRequestBuilder descCount() { + result.setOrder(TermFacetOrder.descCount); + return this; + } + + public TermFacetRequestBuilder applyQueryFilter() { + result.setApplyQueryFilter(true); + return this; + } + + public FacetRequest build() { + return result; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/result/HistogramResult.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/HistogramResult.java new file mode 100644 index 000000000..1114158a4 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/HistogramResult.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet.result; + +import java.util.List; + +import org.springframework.data.elasticsearch.core.facet.AbstractFacetResult; +import org.springframework.data.elasticsearch.core.facet.FacetType; + +/** + * @author Artur Konczak + */ +@Deprecated +public class HistogramResult extends AbstractFacetResult { + + private List terms; + + public HistogramResult(String name, List terms) { + super(name, FacetType.term); + this.terms = terms; + } + + public List getIntervalUnit() { + return terms; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/result/IntervalUnit.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/IntervalUnit.java new file mode 100644 index 000000000..b439a881e --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/IntervalUnit.java @@ -0,0 +1,92 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet.result; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Single term + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + */ +@Deprecated +public class IntervalUnit { + + private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + + long key; + long count; + long totalCount; + double total; + double mean; + double min; + double max; + + public IntervalUnit(long key, long count, long totalCount, double total, double mean, double min, double max) { + this.key = key; + this.count = count; + this.totalCount = totalCount; + this.total = total; + this.mean = mean; + this.min = min; + this.max = max; + } + + public long getKey() { + return key; + } + + public long getCount() { + return count; + } + + public long getTotalCount() { + return totalCount; + } + + public double getTotal() { + return total; + } + + public double getMean() { + return mean; + } + + public double getMin() { + return min; + } + + public double getMax() { + return max; + } + + @Override + public String toString() { + return "IntervalUnit{" + + "key=" + format.format(new Date(key)) + + ", count=" + count + + ", totalCount=" + totalCount + + ", total=" + total + + ", mean=" + mean + + ", min=" + min + + ", max=" + max + + '}'; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/result/Range.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/Range.java new file mode 100644 index 000000000..166e97e58 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/Range.java @@ -0,0 +1,79 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet.result; + +/** + * Single range + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + */ +@Deprecated +public class Range { + + private Double from; + private Double to; + private long count; + private double total; + private double totalCount; + private double min = Double.POSITIVE_INFINITY; + private double max = Double.NEGATIVE_INFINITY; + + public Range(Double from, Double to, long count, double total, double totalCount, double min, double max) { + this.from = from; + this.to = to; + this.count = count; + this.total = total; + this.totalCount = totalCount; + this.min = min; + this.max = max; + } + + public Double getFrom() { + return from; + } + + public Double getTo() { + return to; + } + + /** + * Return number of documents in range + * + * @return + */ + public long getCount() { + return count; + } + + public double getTotal() { + return total; + } + + public double getTotalCount() { + return totalCount; + } + + public double getMin() { + return min; + } + + public double getMax() { + return max; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/result/RangeResult.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/RangeResult.java new file mode 100644 index 000000000..db2b2e2cb --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/RangeResult.java @@ -0,0 +1,44 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet.result; + +import java.util.List; + +import org.springframework.data.elasticsearch.core.facet.AbstractFacetResult; +import org.springframework.data.elasticsearch.core.facet.FacetType; + +/** + * Basic term facet result + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + */ +@Deprecated +public class RangeResult extends AbstractFacetResult { + + private List ranges; + + public RangeResult(String name, List ranges) { + super(name, FacetType.range); + this.ranges = ranges; + } + + public List getRanges() { + return ranges; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/result/StatisticalResult.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/StatisticalResult.java new file mode 100644 index 000000000..28f188686 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/StatisticalResult.java @@ -0,0 +1,86 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet.result; + +import org.springframework.data.elasticsearch.core.facet.AbstractFacetResult; +import org.springframework.data.elasticsearch.core.facet.FacetType; + +/** + * @author Petar Tahchiev + */ +@Deprecated +public class StatisticalResult extends AbstractFacetResult { + + private long count; + + private double max; + + private double min; + + private double mean; + + private double stdDeviation; + + private double sumOfSquares; + + private double total; + + private double variance; + + public StatisticalResult(String name, long count, double max, double min, double mean, double stdDeviation, double sumOfSquares, double total, double variance) { + super(name, FacetType.statistical); + this.count = count; + this.max = max; + this.min = min; + this.mean = mean; + this.stdDeviation = stdDeviation; + this.sumOfSquares = sumOfSquares; + this.total = total; + this.variance = variance; + } + + public long getCount() { + return count; + } + + public double getMax() { + return max; + } + + public double getMin() { + return min; + } + + public double getMean() { + return mean; + } + + public double getStdDeviation() { + return stdDeviation; + } + + public double getSumOfSquares() { + return sumOfSquares; + } + + public double getTotal() { + return total; + } + + public double getVariance() { + return variance; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/result/Term.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/Term.java new file mode 100644 index 000000000..6bc87bfd4 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/Term.java @@ -0,0 +1,44 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet.result; + +/** + * Single term + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + */ +@Deprecated +public class Term { + + private String term; + private long count; + + public Term(String term, long count) { + this.term = term; + this.count = count; + } + + public String getTerm() { + return term; + } + + public long getCount() { + return count; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/facet/result/TermResult.java b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/TermResult.java new file mode 100644 index 000000000..12653abba --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/facet/result/TermResult.java @@ -0,0 +1,63 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core.facet.result; + +import java.util.List; + +import org.springframework.data.elasticsearch.core.facet.AbstractFacetResult; +import org.springframework.data.elasticsearch.core.facet.FacetType; + +/** + * Basic term facet result + * + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + * @author Jonathan Yan + * @author Matija Obreza + */ +@Deprecated +public class TermResult extends AbstractFacetResult { + + private List terms; + private long total; + private long other; + private long missing; + + public TermResult(String name, List terms, long total, long other, long missing) { + super(name, FacetType.term); + this.terms = terms; + this.total = total; + this.other = other; + this.missing = missing; + } + + public List getTerms() { + return terms; + } + + public long getTotal() { + return total; + } + + public long getOther() { + return other; + } + + public long getMissing() { + return missing; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java index 8822c0412..265468bcf 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java @@ -22,6 +22,7 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.highlight.HighlightBuilder; import org.elasticsearch.search.sort.SortBuilder; +import org.springframework.data.elasticsearch.core.facet.FacetRequest; import java.util.Arrays; @@ -38,7 +39,7 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery { private QueryBuilder filter; private List sorts; private final List scriptFields = new ArrayList(); - /*private List facets;*/ + private List facets; private List aggregations; private HighlightBuilder.Field[] highlightFields; private List indicesBoost; @@ -94,7 +95,7 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery { scriptFields.addAll(Arrays.asList(scriptField)); } -/* public void addFacet(FacetRequest facetRequest) { + public void addFacet(FacetRequest facetRequest) { if (facets == null) { facets = new ArrayList(); } @@ -108,7 +109,7 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery { @Override public List getFacets() { return facets; - }*/ + } @Override public List getAggregations() { diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java index 3fd4cbfd7..1e159c345 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java @@ -25,6 +25,7 @@ import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.highlight.HighlightBuilder; import org.elasticsearch.search.sort.SortBuilder; import org.springframework.data.domain.Pageable; +import org.springframework.data.elasticsearch.core.facet.FacetRequest; /** * NativeSearchQuery @@ -40,6 +41,7 @@ public class NativeSearchQueryBuilder { private QueryBuilder filterBuilder; private List scriptFields = new ArrayList(); private List sortBuilders = new ArrayList(); + private List facetRequests = new ArrayList(); private List aggregationBuilders = new ArrayList(); private HighlightBuilder.Field[] highlightFields; private Pageable pageable; @@ -78,10 +80,10 @@ public class NativeSearchQueryBuilder { return this; } -/* public NativeSearchQueryBuilder withFacet(FacetRequest facetRequest) { + public NativeSearchQueryBuilder withFacet(FacetRequest facetRequest) { facetRequests.add(facetRequest); return this; - }*/ + } public NativeSearchQueryBuilder withHighlightFields(HighlightBuilder.Field... highlightFields) { this.highlightFields = highlightFields; @@ -168,6 +170,10 @@ public class NativeSearchQueryBuilder { nativeSearchQuery.setScriptFields(scriptFields); } + if (!isEmpty(facetRequests)) { + nativeSearchQuery.setFacets(facetRequests); + } + if (!isEmpty(aggregationBuilders)) { nativeSearchQuery.setAggregations(aggregationBuilders); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java index 8a1d11786..f3bfa9fa5 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java @@ -21,6 +21,7 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.highlight.HighlightBuilder; import org.elasticsearch.search.sort.SortBuilder; +import org.springframework.data.elasticsearch.core.facet.FacetRequest; /** * NativeSearchQuery @@ -37,7 +38,8 @@ public interface SearchQuery extends Query { List getElasticsearchSorts(); - /*List getFacets();*/ + @Deprecated + List getFacets(); List getAggregations(); diff --git a/src/test/java/org/springframework/data/elasticsearch/core/CustomResultMapper.java b/src/test/java/org/springframework/data/elasticsearch/core/CustomResultMapper.java index 4a981d54a..e7900258d 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/CustomResultMapper.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/CustomResultMapper.java @@ -22,6 +22,7 @@ import org.elasticsearch.action.get.MultiGetResponse; import org.elasticsearch.action.search.SearchResponse; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; /** * @author Artur Konczak @@ -47,7 +48,7 @@ public class CustomResultMapper implements ResultsMapper { } @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { return null; //To change body of implemented methods use File | Settings | File Templates. } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/DefaultResultMapperTests.java b/src/test/java/org/springframework/data/elasticsearch/core/DefaultResultMapperTests.java index a00fba7a1..e3f371600 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/DefaultResultMapperTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/DefaultResultMapperTests.java @@ -29,13 +29,14 @@ import org.elasticsearch.search.SearchHitField; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.internal.InternalSearchHitField; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.data.domain.Page; -import org.springframework.data.elasticsearch.core.domain.AggregatedPage; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.entities.Car; /** @@ -68,7 +69,7 @@ public class DefaultResultMapperTests { Aggregations aggregations = mock(Aggregations.class); Iterator iter = Collections.singletonList(aggregationToReturn).iterator(); - when(aggregations.iterator()).thenReturn(iter); + when(aggregations.iterator()).thenReturn(iter).thenReturn(iter); when(aggregations.get("engine")).thenReturn(aggregationToReturn); when(response.getAggregations()).thenReturn(aggregations); @@ -76,6 +77,7 @@ public class DefaultResultMapperTests { AggregatedPage page = (AggregatedPage) resultMapper.mapResults(response, Car.class, null); //Then + page.hasFacets(); assertThat(page.hasAggregations(), is(true)); assertThat(page.getAggregation("engine").getName(), is("Diesel")); } @@ -132,7 +134,7 @@ public class DefaultResultMapperTests { } private Aggregation createCarAggregation() { - Aggregation aggregation = mock(Aggregation.class); + Aggregation aggregation = mock(Terms.class); when(aggregation.getName()).thenReturn("Diesel"); return aggregation; } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java index 94a132209..6fe00e009 100755 --- a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java @@ -41,9 +41,14 @@ import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.*; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.ElasticsearchException; import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; +import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; import org.springframework.data.elasticsearch.core.query.*; import org.springframework.data.elasticsearch.entities.*; import org.springframework.data.util.CloseableIterator; @@ -632,12 +637,12 @@ public class ElasticsearchTemplateTests { // when Page page = elasticsearchTemplate.queryForPage(searchQuery, String.class, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List values = new ArrayList(); for (SearchHit searchHit : response.getHits()) { values.add((String) searchHit.field("message").value()); } - return new PageImpl((List) values); + return new AggregatedPageImpl((List) values); } }); // then @@ -791,7 +796,7 @@ public class ElasticsearchTemplateTests { while (hasRecords) { Page page = elasticsearchTemplate.scroll(scrollId, 5000L, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List result = new ArrayList(); for (SearchHit searchHit : response.getHits()) { String message = searchHit.getFields().get("message").getValue(); @@ -802,7 +807,7 @@ public class ElasticsearchTemplateTests { } if (result.size() > 0) { - return new PageImpl((List) result); + return new AggregatedPageImpl((List) result); } return null; } @@ -843,7 +848,7 @@ public class ElasticsearchTemplateTests { while (hasRecords) { Page page = elasticsearchTemplate.scroll(scrollId, 10000L, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List result = new ArrayList(); for (SearchHit searchHit : response.getHits()) { String message = searchHit.getFields().get("message").getValue(); @@ -854,7 +859,7 @@ public class ElasticsearchTemplateTests { } if (result.size() > 0) { - return new PageImpl((List) result); + return new AggregatedPageImpl((List) result); } return null; } @@ -892,7 +897,7 @@ public class ElasticsearchTemplateTests { while (hasRecords) { Page page = elasticsearchTemplate.scroll(scrollId, 5000L, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List chunk = new ArrayList(); for (SearchHit searchHit : response.getHits()) { if (response.getHits().getHits().length <= 0) { @@ -904,7 +909,7 @@ public class ElasticsearchTemplateTests { chunk.add(user); } if (chunk.size() > 0) { - return new PageImpl((List) chunk); + return new AggregatedPageImpl((List) chunk); } return null; } @@ -937,7 +942,7 @@ public class ElasticsearchTemplateTests { while (hasRecords) { Page page = elasticsearchTemplate.scroll(scrollId, 5000L, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List chunk = new ArrayList(); for (SearchHit searchHit : response.getHits()) { if (response.getHits().getHits().length <= 0) { @@ -949,7 +954,7 @@ public class ElasticsearchTemplateTests { chunk.add(user); } if (chunk.size() > 0) { - return new PageImpl((List) chunk); + return new AggregatedPageImpl((List) chunk); } return null; } @@ -1243,7 +1248,7 @@ public class ElasticsearchTemplateTests { Page sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List chunk = new ArrayList(); for (SearchHit searchHit : response.getHits()) { if (response.getHits().getHits().length <= 0) { @@ -1256,7 +1261,7 @@ public class ElasticsearchTemplateTests { chunk.add(user); } if (chunk.size() > 0) { - return new PageImpl((List) chunk); + return new AggregatedPageImpl((List) chunk); } return null; } @@ -1310,7 +1315,7 @@ public class ElasticsearchTemplateTests { // then Page page = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List values = new ArrayList(); for (SearchHit searchHit : response.getHits()) { SampleEntity sampleEntity = new SampleEntity(); @@ -1318,7 +1323,7 @@ public class ElasticsearchTemplateTests { sampleEntity.setMessage((String) searchHit.getSource().get("message")); values.add(sampleEntity); } - return new PageImpl((List) values); + return new AggregatedPageImpl((List) values); } }); assertThat(page, is(notNullValue())); @@ -1480,7 +1485,7 @@ public class ElasticsearchTemplateTests { .withTypes(TYPE_NAME).withQuery(matchAllQuery()).build(); Page sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, Map.class, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List chunk = new ArrayList(); for (SearchHit searchHit : response.getHits()) { if (response.getHits().getHits().length <= 0) { @@ -1495,7 +1500,7 @@ public class ElasticsearchTemplateTests { chunk.add(person); } if (chunk.size() > 0) { - return new PageImpl((List) chunk); + return new AggregatedPageImpl((List) chunk); } return null; } @@ -1994,7 +1999,7 @@ public class ElasticsearchTemplateTests { SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withTypes("hetro").withIndices(INDEX_1_NAME, INDEX_2_NAME).build(); Page page = elasticsearchTemplate.queryForPage(searchQuery, ResultAggregator.class, new SearchResultMapper() { @Override - public Page mapResults(SearchResponse response, Class clazz, Pageable pageable) { + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { List values = new ArrayList(); for (SearchHit searchHit : response.getHits()) { String id = String.valueOf(searchHit.getSource().get("id")); @@ -2002,7 +2007,7 @@ public class ElasticsearchTemplateTests { String lastName = StringUtils.isNotEmpty((String) searchHit.getSource().get("lastName")) ? (String) searchHit.getSource().get("lastName") : ""; values.add(new ResultAggregator(id, firstName, lastName)); } - return new PageImpl((List) values); + return new AggregatedPageImpl((List) values); } });