[ACCEPTED]-Getting maximum value of field in solr-solr

Accepted answer
Score: 14

There aren't any aggregate functions under 14 solr in the way you might be thinking about 13 them from SQL. The easiest way to do it 12 is to have a two-step process:

  • Get the max value via an appropriate query with a sort
  • use it with the max() function

So, something 11 like:

q=*:*&sort=view_count desc&rows=1&fl=view_count

...to get an item with the max view_count, which 10 you record somewhere, and then

q=whatever&bq=div(view_count, max(the_max_view_count, 1))

Note that 9 that max() function isn't doing an aggregate 8 max; just getting the maximum of the max-view-count 7 you pass in or 1 (to avoid divide-by-zero 6 errors).

If you have a multiValued field 5 (which you can't sort on) you could also 4 use the StatsComponent to get the max. Either way, you 3 would probably want to do this once, not 2 for every query (say, every night at midnight 1 or whatever once your data set settles down).

Score: 12

You can add just: &stats=true&stats.field=view_count You 2 will see a small statistics on that specified 1 field. More documentation here

More Related questions