Commit 9ca05d84 authored by Matija Obreza's avatar Matija Obreza

Workaround for DATAES-127: Include total, other and missing count in

TermResult
https://jira.spring.io/browse/DATAES-127		
parent 6447f83d
......@@ -424,7 +424,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>1.0.2.RELEASE</version>
<version>1.0.4.RELEASE</version>
</dependency>
<dependency>
......
......@@ -25,6 +25,7 @@ import org.genesys2.server.service.impl.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.facet.result.Term;
import org.springframework.data.elasticsearch.core.facet.result.TermResult;
public interface ElasticService {
......@@ -42,6 +43,6 @@ public interface ElasticService {
Page<AccessionDetails> filter(AppliedFilters appliedFilters, Pageable pageable) throws SearchException;
List<Term> termStatistics(AppliedFilters appliedFilters, String term, int size) throws SearchException;
TermResult termStatistics(AppliedFilters appliedFilters, String term, int size) throws SearchException;
}
......@@ -44,7 +44,6 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.FacetedPage;
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
import org.springframework.data.elasticsearch.core.facet.request.TermFacetRequestBuilder;
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.NativeSearchQueryBuilder;
......@@ -114,17 +113,16 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
@Override
public List<Term> termStatistics(AppliedFilters appliedFilters, String term, int size) throws SearchException {
public TermResult termStatistics(AppliedFilters appliedFilters, String term, int size) throws SearchException {
AndFilterBuilder filterBuilder = getFilterBuilder(appliedFilters);
FacetRequest termFacetRequest = new TermFacetRequestBuilder("f").applyQueryFilter().fields(term).size(10).build();
FacetRequest termFacetRequest = new TermFacetRequestBuilder("f").applyQueryFilter().fields(term).size(size).build();
SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(filterBuilder).withFacet(termFacetRequest).build();
try {
FacetedPage<AccessionDetails> page = elasticsearchTemplate.queryForPage(searchQuery, AccessionDetails.class);
TermResult facet = (TermResult) page.getFacet("f");
return facet.getTerms();
return (TermResult) page.getFacet("f");
} catch (Throwable e) {
throw new SearchException(e.getMessage(), e);
......
/*
* 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
* @author Matija Obreza
*/
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<Term> entries = new ArrayList<Term>();
for (TermsFacet.Entry entry : facet.getEntries()) {
entries.add(new Term(entry.getTerm().toString(), entry.getCount()));
}
// https://jira.spring.io/browse/DATAES-127 Include total, other and missing count in TermResult
return new TermResult(facet.getName(), entries, facet.getTotalCount(), facet.getOtherCount(), facet.getMissingCount());
}
private static FacetResult parseRange(RangeFacet facet) {
List<Range> entries = new ArrayList<Range>();
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<IntervalUnit> entries = new ArrayList<IntervalUnit>();
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);
}
}
/*
* 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
*/
public class TermResult extends AbstractFacetResult {
private List<Term> terms;
private long totalCount;
private long otherCount;
private long missingCount;
// https://jira.spring.io/browse/DATAES-127 Include total, other and missing count in TermResult
public TermResult(String name, List<Term> terms, long totalCount, long otherCount, long missingCount) {
super(name, FacetType.term);
this.terms = terms;
this.totalCount = totalCount;
this.otherCount = otherCount;
this.missingCount = missingCount;
}
public List<Term> getTerms() {
return terms;
}
public long getOtherCount() {
return otherCount;
}
public long getMissingCount() {
return missingCount;
}
public long getTotalCount() {
return totalCount;
}
}
......@@ -62,6 +62,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.facet.result.Term;
import org.springframework.data.elasticsearch.core.facet.result.TermResult;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
......@@ -330,10 +331,13 @@ public class ElasticsearchTest {
.addFilterValue(new FilterHandler.LiteralValueFilter(40)));
LOG.info(filters);
List<Term> terms = elasticService.termStatistics(filters, FilterConstants.STORAGE, 10);
for (Term t : terms) {
TermResult termResult = elasticService.termStatistics(filters, FilterConstants.STORAGE, 3);
for (Term t : termResult.getTerms()) {
LOG.info(t.getTerm() + " = " + t.getCount());
}
LOG.info("Other=" + termResult.getOtherCount());
LOG.info("Missing=" + termResult.getMissingCount());
LOG.info("Total=" + termResult.getTotalCount());
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment