Getting maximum value of field in solr

旧街凉风 提交于 2020-01-22 19:43:35

问题


I'd like to boost my query by the item's view count; I'd like to use something like view_count / max_view_count for this purpose, to be able to measure how the item's view count relates to the biggest view count in the index. I know how to boost the results with a function query, but how can I easily get the maximum view count? If anybody could provide an example it would be very helpful...


回答1:


There aren't any aggregate functions under solr in the way you might be thinking about them from SQL. The easiest way to do it 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 like:

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

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

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

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

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




回答2:


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



来源:https://stackoverflow.com/questions/6413966/getting-maximum-value-of-field-in-solr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!