5

I have a number of docs indexed by Solr 3.5, which contain date fields (solr.DateField) among others. Now I do request to Solr component which should return no results:

http://example.com/solr/select?fq=sis_field_int:1000&
stats=true&stats.field=ds_field_date

and get error

HTTP Status 500 - / by zero java.lang.ArithmeticException: / by zero at
org.apache.solr.handler.component.DateStatsValues.addTypeSpecificStats
(StatsValuesFactory.java:384) at ...

If I send request without stats part or specify any non-date stats field instead, I get expected response with no results. It looks like a bug of Solr which tries e.g. to calculate mean value in this case. Unfortunately I've found no references on this problem. Is there some way to bypass or solve the problem?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
dev4
  • 195
  • 1
  • 7

1 Answers1

6

You're right, the problem is computing the mean value:

res.add("mean", new Date(sum / count));

sum and count are both long. When count is zero, of course you get an ArithmeticException. You're actually making stats on a date field which never has a value in your index. The easiest workaround would be making stats on a field which has at least one value, so the count variable would be greather than zero, the division will work, and the stats would be even more meaningful I guess.

You don't get the same error with the same situation using a numeric field, because in that case the sum variable is double, thus the division don't raise error and the result is NaN. In fact, there are different StatsValues implementations based on the field type.

UPDATE
I've opened the SOLR-3160 jira issue and provided a patch which has just been committed. The next release of Solr will contain the fix!

javanna
  • 59,145
  • 14
  • 144
  • 125
  • Is there no check whether count is equal to zero? Is there any way to set another handler of this error in request to Solr other that HTTP 500 answer? – dev4 Feb 21 '12 at 16:58
  • @dev4 There is no check! I think the best thing to do is open a jira issue. If you want you can even provide a patch, otherwise I'm sure someone will have a look at it, I can do that too. – javanna Feb 21 '12 at 17:37
  • 1
    @dev4 Look at my updated answer! The fix has already been committed, it will be available with the next Solr release! – javanna Feb 24 '12 at 14:46
  • @javanna hi, can you check this please? http://stackoverflow.com/questions/14955165/solr-get-the-sum-of-all-filemetadata-filesize-field-for-a-given-user It may be related according to the tests I see in StatsComponentTest – Sebastien Lorber Feb 19 '13 at 10:36