简体   繁体   中英

ElasticSearch facet rolling weeks (rails / tire)

using ElasticSearch with Rails 3.2.6 (/w Tire) I have a facet on my Project object which displays currently which month the project was posted in:

facet('timeline') { date :post_date, :interval => 'month' }

This gives me the projects grouped in calendar months. Have also tried 'week' instead of 'month', but this gives me grouped by calendar week. Ideally, what we are looking to do is have rolling groups like this:

  • last 7 days
  • last 14 days
  • last 21 days

Any suggestions on how this can be achieved so that we are not limited to calendar weeks and can easily pick out the relevant buckets from the data?? Many thanks

Do you mean for example: the number of projects that have been updated in the previous 7 days, 14 days, 21 days?

If so, use a range facet :

curl -XGET 'http://127.0.0.1:9200/YOUR_INDEX/project/_search?pretty=1'  -d '
{
   "query" : {
      "match_all" : {}
   },
   "facets" : {
      "projects" : {
         "range" : {
            "ranges" : [
               {
                  "to" : "2012-07-09"
               },
               {
                  "to" : "2012-07-02"
               }
            ],
            "field" : "last_modified_date"
         }
      }
   }
}
'

It would be useful to use date math , eg:

curl -XGET 'http://127.0.0.1:9200/_search?pretty=1&search_type=count'  -d '
{
   "query" : {
      "match_all" : {}
   },
   "facets" : {
      "created" : {
         "range" : {
            "ranges" : [
               {
                  "to" : "now-7d"
               },
               {
                  "to" : "now-14d"
               }
            ],
            "field" : "created"
         }
      }
   }
}
'

but that is not yet supported. I have opened an issue to request it: https://github.com/elasticsearch/elasticsearch/issues/2102

您可以改用Range Facets ,在那里您将完全控制将要使用的组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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