mysql - 如何通过对solr中的天数进行分组来获取列的总和

标签 mysql solr lucene solr4

我的solr数据如下:

{
      {
        "total_amount": 100,
        "created_at": "2015-07-08T18:30:00Z"
      },
      {
        "total_amount": 200,
        "created_at": "2015-07-08T18:30:00Z"
      },
      {
        "total_amount": 300,
        "created_at": "2015-06-08T18:30:00Z"
      },
      {
        "total_amount": 400,
        "created_at": "2015-06-08T18:30:00Z"
      }
}

我想通过分组月份获得 total_amount 的总和。因此输出响应将如下所示:

{
  "2015-07-01T00:00:00Z" : 300,
  "2015-06-01T00:00:00Z" : 700
}

我正在使用下面的查询来获得给定的响应,但它只给我出现的次数。

查询和响应如下:

{
    "responseHeader": {
    "status": 0,
    "QTime": 2,
    "params": {
    "facet.date.start": "2015-04-01T00:00:00Z",
    "facet": "true",
    "fl": "created_at,total_amount",
    "q": "campaign_id:1",
    "facet.date": "created_at",
    "facet.date.gap": "+1MONTH",
    "wt": "json",
    "facet.date.end": "2015-09-01T00:00:00Z"
    }
    },
    "response": {
    "numFound": 3,
    "start": 0,
    "docs": [
    {
    "total_amount": 100,
    "created_at": "2015-07-08T18:30:00Z"
    },
    {
    "total_amount": 100,
    "created_at": "2015-07-08T18:30:00Z"
    },
    {
    "total_amount": 100,
    "created_at": "2015-07-08T18:30:00Z"
    }
    ]
    },
    "facet_counts": {
    "facet_queries": {},
    "facet_fields": {},
    "facet_dates": {
    "created_at": {
    "2015-04-01T00:00:00Z": 0,
    "2015-05-01T00:00:00Z": 0,
    "2015-06-01T00:00:00Z": 0,
    "2015-07-01T00:00:00Z": 3,
    "2015-08-01T00:00:00Z": 0,
    "gap": "+1MONTH",
    "start": "2015-04-01T00:00:00Z",
    "end": "2015-09-01T00:00:00Z"
    }
    },
    "facet_ranges": {},
    "facet_intervals": {},
    "facet_heatmaps": {}
    }

}

最佳答案

试试这个查询:

stats=true&stats.field=total_amount&stats.facet=created_at

这是来自 solr-ref-guide .

The Stats Component accepts the following parameters:

stats | If true, then invokes the Stats component.

stats.field | Specifies a field for which statistics should be generated. This parameter may be invoked multiple times in a query in order to request statistics on multiple fields. (See the example below.)

stats.facet | Returns sub-results for values within the specified facet.

我在运行“java -jar post.jar *.xml”后使用 SOLR_HOME/example/examplesdoc 目录中的数据测试了查询

这是部分结果。

 "belkin":{
    "min":11.5,
    "max":19.950000762939453,
    "count":2,
    "missing":0,
    "sum":31.450000762939453,
    "sumOfSquares":530.2525304412848,
    "mean":15.725000381469727,
    "stddev":5.975052840505987,
    "facets":{}},
  "maxtor":{
    "min":350.0,
    "max":350.0,
    "count":1,
    "missing":0,
    "sum":350.0,
    "sumOfSquares":122500.0,
    "mean":350.0,
    "stddev":0.0,
    "facets":{}},
  "inc":{
    "min":179.99000549316406,
    "max":2199.0,
    "count":5,
    "missing":0,
    "sum":3587.8900299072266,
    "sumOfSquares":5366417.4268503785,
    "mean":717.5780059814454,
    "stddev":835.4379769780703,
    "facets":{}}}}}}}}

关于mysql - 如何通过对solr中的天数进行分组来获取列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31429948/

相关文章:

c# - Odbc INSERT 不工作

xml - Solr DIH 数据导入不适用于 XML 文件

solr - Apache solr 中的索引是什么?

java - Lucene从2.2升级到2.9到3.1的问题

search - 计算lucene索引中的词频

php - 将 Node 与 MySql 一起使用或将 Node 与 PHP 一起使用(这将处理 MySql 操作)

日期列的 MySQL 条件 ORDER BY ASC/DESC

solr - Hybris:在完整索引时启用 SOLR 搜索

Azure 搜索突出显示不仅匹配 Lucene 字段范围查询中使用的字段

php - 每天两个时间戳的时间片