简体   繁体   English

Solr 中计算列的平均值

[英]Average of a Calculated column in Solr

I have a solr collection and I need some calculation done based on data.我有一个 solr 集合,我需要根据数据进行一些计算。 I have TotalLapTime and DrivingTime, completion rate for a driver is DrivingTime*100/TotalLapTime.我有 TotalLapTime 和 DrivingTime,司机的完成率是 DrivingTime*100/TotalLapTime。 I would like to find out average completion time for each team.我想知道每个团队的平均完成时间。 How can I query the Solr for this?我该如何查询 Solr 这个? I have been able only able to get sum of DrivingTime using stats.我只能使用统计数据获得驾驶时间的总和。 I am on Solr5我在 Solr5

Solr collection: Solr 集合:

    ID |DrivingTime|TotalLapTime| Team
    1  |   50    |    100       |  A
    2  |   25    |    100       |  A
    3  |   30    |    60        |  B
    4  |   50    |    60        |  B
    5  |   25    |    200       |  C

Expected output:预期 output:

TEAM | AvgCompletionRate
  A  |      37%
  B  |      66.5%
  C  |      12.5%

First - one of the things with document based search indexes and analytics is that you should usually do as much pre-processing as possible, since your data usually is read once, then analyze in many different contexts.首先 - 基于文档的搜索索引和分析的一件事是您通常应该尽可能多地进行预处理,因为您的数据通常被读取一次,然后在许多不同的上下文中进行分析。 So in this case, adding completion_time as a separate field and calculate it when submitting the document to the index would be the best thing to do.所以在这种情况下,将completion_time作为一个单独的字段添加并在将文档提交到索引时计算它是最好的做法。

That makes it far easier to do any further analysis on the field, and we can then use the JSON Facet API to create buckets for each time, and then calculate the average completion time for those entries that fall into that bucket.这使得对该字段进行任何进一步分析变得容易得多,然后我们可以使用 JSON Facet API 为每次创建存储桶,然后计算落入该存储桶的那些条目的平均完成时间。

Adopted from Solr's example for sorting facets by nested functions example:Solr 的示例中采用,用于通过嵌套函数对方面进行排序示例:

{
  "query": "*:*",
  "facet": {
    "categories":{
      "type": "terms",
      "field": "TEAM",
      "limit": 3,
      "sort": "avg_completion_time asc",
      "facet": {
        "avg_completion_time": "avg(completion_time)",
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM