mirror of
				https://github.com/spring-projects/spring-data-elasticsearch.git
				synced 2025-11-04 00:28:58 +00:00 
			
		
		
		
	DATAES-274 - prevent NPE in FacetedPageImpl
This commit is contained in:
		
							parent
							
								
									a343bf7109
								
							
						
					
					
						commit
						a085c064d5
					
				@ -19,8 +19,8 @@ import java.util.ArrayList;
 | 
				
			|||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.elasticsearch.search.aggregations.Aggregation;
 | 
					import org.elasticsearch.search.aggregations.Aggregation;
 | 
				
			||||||
 | 
					import org.elasticsearch.search.aggregations.Aggregations;
 | 
				
			||||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
 | 
					import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
 | 
				
			||||||
import org.elasticsearch.search.aggregations.bucket.range.Range;
 | 
					import org.elasticsearch.search.aggregations.bucket.range.Range;
 | 
				
			||||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 | 
					import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 | 
				
			||||||
@ -33,7 +33,12 @@ import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
 | 
				
			|||||||
import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
 | 
					import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.facet.FacetResult;
 | 
					import org.springframework.data.elasticsearch.core.facet.FacetResult;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.facet.request.RangeFacetRequest;
 | 
					import org.springframework.data.elasticsearch.core.facet.request.RangeFacetRequest;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.facet.result.*;
 | 
					import org.springframework.data.elasticsearch.core.facet.result.HistogramResult;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.IntervalUnit;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.RangeResult;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.StatisticalResult;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.Term;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.TermResult;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Container for query result and facet results
 | 
					 * Container for query result and facet results
 | 
				
			||||||
@ -42,6 +47,7 @@ import org.springframework.data.elasticsearch.core.facet.result.*;
 | 
				
			|||||||
 * @author Mohsin Husen
 | 
					 * @author Mohsin Husen
 | 
				
			||||||
 * @author Artur Konczak
 | 
					 * @author Artur Konczak
 | 
				
			||||||
 * @author Jonathan Yan
 | 
					 * @author Jonathan Yan
 | 
				
			||||||
 | 
					 * @author Philipp Kräutli
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@Deprecated
 | 
					@Deprecated
 | 
				
			||||||
public abstract class FacetedPageImpl<T> extends PageImpl<T> implements FacetedPage<T>, AggregatedPage<T> {
 | 
					public abstract class FacetedPageImpl<T> extends PageImpl<T> implements FacetedPage<T>, AggregatedPage<T> {
 | 
				
			||||||
@ -84,47 +90,76 @@ public abstract class FacetedPageImpl<T> extends PageImpl<T> implements FacetedP
 | 
				
			|||||||
	 * Lazy conversion from aggregation to old facets
 | 
						 * Lazy conversion from aggregation to old facets
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	private void processAggregations() {
 | 
						private void processAggregations() {
 | 
				
			||||||
		if (facets == null) {
 | 
							if (facets != null) {
 | 
				
			||||||
			facets = new ArrayList<>();
 | 
								return;
 | 
				
			||||||
			for (Aggregation agg : getAggregations()) {
 | 
							}
 | 
				
			||||||
				if (agg instanceof Terms) {
 | 
							facets = new ArrayList<>();
 | 
				
			||||||
					List<Term> terms = new ArrayList<>();
 | 
							Aggregations aggregations = getAggregations();
 | 
				
			||||||
					for (Terms.Bucket t : ((Terms) agg).getBuckets()) {
 | 
							if (aggregations == null) {
 | 
				
			||||||
						terms.add(new Term(t.getKeyAsString(), t.getDocCount()));
 | 
								return;
 | 
				
			||||||
					}
 | 
							}
 | 
				
			||||||
					addFacet(new TermResult(agg.getName(), terms, terms.size(), ((Terms) agg).getSumOfOtherDocCounts(), 0));
 | 
							for (Aggregation agg : aggregations) {
 | 
				
			||||||
				}
 | 
								processAggregation(agg);
 | 
				
			||||||
				if (agg instanceof Range) {
 | 
					 | 
				
			||||||
					List<? extends Range.Bucket> buckets = ((Range) agg).getBuckets();
 | 
					 | 
				
			||||||
					List<org.springframework.data.elasticsearch.core.facet.result.Range> ranges = new ArrayList<>();
 | 
					 | 
				
			||||||
					for (Range.Bucket b : buckets) {
 | 
					 | 
				
			||||||
						ExtendedStats rStats = (ExtendedStats) b.getAggregations().get(AbstractFacetRequest.INTERNAL_STATS);
 | 
					 | 
				
			||||||
						if (rStats != null) {
 | 
					 | 
				
			||||||
							Sum sum = (Sum) b.getAggregations().get(RangeFacetRequest.RANGE_INTERNAL_SUM);
 | 
					 | 
				
			||||||
							ranges.add(new org.springframework.data.elasticsearch.core.facet.result.Range((Double) b.getFrom(), (Double) b.getTo(), b.getDocCount(), sum != null ? sum.getValue() : rStats.getSum(), rStats.getCount(), rStats.getMin(), rStats.getMax()));
 | 
					 | 
				
			||||||
						} else {
 | 
					 | 
				
			||||||
							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 ExtendedStats) {
 | 
					 | 
				
			||||||
					ExtendedStats stats = (ExtendedStats) agg;
 | 
					 | 
				
			||||||
					addFacet(new StatisticalResult(agg.getName(), stats.getCount(), stats.getMax(), stats.getMin(), stats.getAvg(), stats.getStdDeviation(), stats.getSumOfSquares(), stats.getSum(), stats.getVariance()));
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				if (agg instanceof Histogram) {
 | 
					 | 
				
			||||||
					List<IntervalUnit> intervals = new ArrayList<>();
 | 
					 | 
				
			||||||
					for (Histogram.Bucket h : ((Histogram) agg).getBuckets()) {
 | 
					 | 
				
			||||||
						ExtendedStats hStats = (ExtendedStats) h.getAggregations().get(AbstractFacetRequest.INTERNAL_STATS);
 | 
					 | 
				
			||||||
						if (hStats != null) {
 | 
					 | 
				
			||||||
							intervals.add(new IntervalUnit(((DateTime) h.getKey()).getMillis(), h.getDocCount(), h.getDocCount(), hStats.getSum(), hStats.getAvg(), hStats.getMin(), hStats.getMax()));
 | 
					 | 
				
			||||||
						} else {
 | 
					 | 
				
			||||||
							intervals.add(new IntervalUnit(((DateTime) h.getKey()).getMillis(), h.getDocCount(), h.getDocCount(), 0, 0, 0, 0));
 | 
					 | 
				
			||||||
						}
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
					addFacet(new HistogramResult(agg.getName(), intervals));
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private void processAggregation(Aggregation agg)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							if (agg instanceof Terms) {
 | 
				
			||||||
 | 
								processTermAggregation((Terms) agg);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if (agg instanceof Range) {
 | 
				
			||||||
 | 
								processRangeAggregation((Range) agg);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if (agg instanceof ExtendedStats) {
 | 
				
			||||||
 | 
								processExtendedStatsAggregation((ExtendedStats) agg);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if (agg instanceof Histogram) {
 | 
				
			||||||
 | 
								processHistogramAggregation((Histogram) agg);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private void processTermAggregation(Terms agg)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							List<Term> terms = new ArrayList<>();
 | 
				
			||||||
 | 
							for (Terms.Bucket t : agg.getBuckets()) {
 | 
				
			||||||
 | 
								terms.add(new Term(t.getKeyAsString(), t.getDocCount()));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							addFacet(new TermResult(agg.getName(), terms, terms.size(), agg.getSumOfOtherDocCounts(), 0));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private void processRangeAggregation(Range agg)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							List<? extends Range.Bucket> buckets = ((Range) agg).getBuckets();
 | 
				
			||||||
 | 
							List<org.springframework.data.elasticsearch.core.facet.result.Range> ranges = new ArrayList<>();
 | 
				
			||||||
 | 
							for (Range.Bucket b : buckets) {
 | 
				
			||||||
 | 
								ExtendedStats rStats = b.getAggregations().get(AbstractFacetRequest.INTERNAL_STATS);
 | 
				
			||||||
 | 
								if (rStats != null) {
 | 
				
			||||||
 | 
									Sum sum = b.getAggregations().get(RangeFacetRequest.RANGE_INTERNAL_SUM);
 | 
				
			||||||
 | 
									ranges.add(new org.springframework.data.elasticsearch.core.facet.result.Range((Double) b.getFrom(), (Double) b.getTo(), b.getDocCount(), sum != null ? sum.getValue() : rStats.getSum(), rStats.getCount(), rStats.getMin(), rStats.getMax()));
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									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));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private void processExtendedStatsAggregation(ExtendedStats agg)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							addFacet(new StatisticalResult(agg.getName(), agg.getCount(), agg.getMax(), agg.getMin(), agg.getAvg(), agg.getStdDeviation(), agg.getSumOfSquares(), agg.getSum(), agg.getVariance()));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private void processHistogramAggregation(Histogram agg)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							List<IntervalUnit> intervals = new ArrayList<>();
 | 
				
			||||||
 | 
							for (Histogram.Bucket h : agg.getBuckets()) {
 | 
				
			||||||
 | 
								ExtendedStats hStats = h.getAggregations().get(AbstractFacetRequest.INTERNAL_STATS);
 | 
				
			||||||
 | 
								if (hStats != null) {
 | 
				
			||||||
 | 
									intervals.add(new IntervalUnit(((DateTime) h.getKey()).getMillis(), h.getDocCount(), h.getDocCount(), hStats.getSum(), hStats.getAvg(), hStats.getMin(), hStats.getMax()));
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									intervals.add(new IntervalUnit(((DateTime) h.getKey()).getMillis(), h.getDocCount(), h.getDocCount(), 0, 0, 0, 0));
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							addFacet(new HistogramResult(agg.getName(), intervals));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -16,26 +16,33 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package org.springframework.data.elasticsearch.core.facet;
 | 
					package org.springframework.data.elasticsearch.core.facet;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static org.elasticsearch.index.query.QueryBuilders.*;
 | 
					 | 
				
			||||||
import static org.hamcrest.Matchers.*;
 | 
					 | 
				
			||||||
import static org.junit.Assert.*;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.elasticsearch.index.query.QueryBuilder;
 | 
					 | 
				
			||||||
import org.elasticsearch.index.query.QueryBuilders;
 | 
					 | 
				
			||||||
import org.junit.Before;
 | 
					import org.junit.Before;
 | 
				
			||||||
import org.junit.Test;
 | 
					import org.junit.Test;
 | 
				
			||||||
import org.junit.runner.RunWith;
 | 
					import org.junit.runner.RunWith;
 | 
				
			||||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
 | 
					import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.FacetedPage;
 | 
					import org.springframework.data.elasticsearch.core.FacetedPage;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.facet.request.*;
 | 
					import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.facet.result.*;
 | 
					import org.springframework.data.elasticsearch.core.facet.request.HistogramFacetRequestBuilder;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
 | 
					import org.springframework.data.elasticsearch.core.facet.request.NativeFacetRequest;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.request.RangeFacetRequestBuilder;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.request.StatisticalFacetRequestBuilder;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.request.TermFacetRequestBuilder;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.HistogramResult;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.IntervalUnit;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.Range;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.RangeResult;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.StatisticalResult;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.Term;
 | 
				
			||||||
 | 
					import org.springframework.data.elasticsearch.core.facet.result.TermResult;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
 | 
					import org.springframework.data.elasticsearch.core.query.IndexQuery;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
 | 
					import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
 | 
				
			||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
 | 
					import org.springframework.data.elasticsearch.core.query.SearchQuery;
 | 
				
			||||||
import org.springframework.test.context.ContextConfiguration;
 | 
					import org.springframework.test.context.ContextConfiguration;
 | 
				
			||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 | 
					import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 | 
				
			||||||
 | 
					import static org.elasticsearch.index.query.QueryBuilders.*;
 | 
				
			||||||
 | 
					import static org.hamcrest.Matchers.*;
 | 
				
			||||||
 | 
					import static org.junit.Assert.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @author Rizwan Idrees
 | 
					 * @author Rizwan Idrees
 | 
				
			||||||
@ -48,14 +55,14 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 | 
				
			|||||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
 | 
					@ContextConfiguration("classpath:elasticsearch-template-test.xml")
 | 
				
			||||||
public class ElasticsearchTemplateFacetTests {
 | 
					public class ElasticsearchTemplateFacetTests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public static final String RIZWAN_IDREES = "Rizwan Idrees";
 | 
						private static final String RIZWAN_IDREES = "Rizwan Idrees";
 | 
				
			||||||
	public static final String MOHSIN_HUSEN = "Mohsin Husen";
 | 
						private static final String MOHSIN_HUSEN = "Mohsin Husen";
 | 
				
			||||||
	public static final String JONATHAN_YAN = "Jonathan Yan";
 | 
						private static final String JONATHAN_YAN = "Jonathan Yan";
 | 
				
			||||||
	public static final String ARTUR_KONCZAK = "Artur Konczak";
 | 
						private static final String ARTUR_KONCZAK = "Artur Konczak";
 | 
				
			||||||
	public static final int YEAR_2002 = 2002;
 | 
						private static final int YEAR_2002 = 2002;
 | 
				
			||||||
	public static final int YEAR_2001 = 2001;
 | 
						private static final int YEAR_2001 = 2001;
 | 
				
			||||||
	public static final int YEAR_2000 = 2000;
 | 
						private static final int YEAR_2000 = 2000;
 | 
				
			||||||
	public static final String PUBLISHED_YEARS = "publishedYears";
 | 
						private static final String PUBLISHED_YEARS = "publishedYears";
 | 
				
			||||||
	@Autowired
 | 
						@Autowired
 | 
				
			||||||
	private ElasticsearchTemplate elasticsearchTemplate;
 | 
						private ElasticsearchTemplate elasticsearchTemplate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -598,5 +605,14 @@ public class ElasticsearchTemplateFacetTests {
 | 
				
			|||||||
		assertThat(unit.getKey(), is(Long.valueOf(YEAR_2002)));
 | 
							assertThat(unit.getKey(), is(Long.valueOf(YEAR_2002)));
 | 
				
			||||||
		assertThat(unit.getCount(), is(1L));
 | 
							assertThat(unit.getCount(), is(1L));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Test
 | 
				
			||||||
 | 
						public void shouldNotThrowExceptionForNoFacets()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
 | 
				
			||||||
 | 
							AggregatedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assertThat(result.hasFacets(), is(false));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user