Spring-Data-SOLR: Stats result on SimpleQuery won't return me distinct calcs

750 Views Asked by At

I'm trying to use a SimpleQuery to get distinct result from my solr collection, but even after setting my StatsOption with calcDistinct true, I can't get the result I want.

BTW I'm using spring-data-solr-2.1.4.RELEASE.

SampleCode:

Field field = new SimpleField("fieldName");
StatsOptions statsOptions = new StatsOptions().addField(field).setCalcDistinct(true);
SimpleQuery query = new SimpleQuery("*:*").setStatsOptions(statsOptions);
StatsPage<MyClass> statsPage = solrTemplate.queryForStatsPage(query, MyClass.class);
FieldStatsResult statsResult = statsPage.getFieldStatsResult(field);
Collection<Object> distinctValues = statsResult.getDistinctValues();
Set<String> result = distinctValues.stream().map((i) -> i.toString()).collect(Collectors.toSet());
return result;

After trying the above code, all I get is the max, min, count, but no results for distinct totals or distinct values.

Result

What am I doing wrong in this sample?

2

There are 2 best solutions below

1
Zilvinas On

Looks like your disctintValues collection is also an implementation of EmptyList which means that there are no values in the response.

Check if your query returns any result, first.

0
Du.Yang On
StatsOptions statsOptions = new StatsOptions().addField(field).setSelectiveCalcDistinct(true).setCalcDistinct(true);

The processStatsOptions method in org.springframework.data.solr.core.DefaultQueryParser is described as follows :

Boolean selectiveCountDistincts = statsOptions.isSelectiveCalcDistincts(field);

if (selectiveCountDistincts != null) {

  solrQuery.add(selectiveCalcDistinctParam, String.valueOf(selectiveCountDistincts.booleanValue()));

}