Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

Elasticsearch, how to store data per date?

I'd like to store daily statistics of sales.

By daily, I mean day of year.

Conceptually the data would look like the following.

{ 
  name: bmw,
  daily_statics : {
     1: { sold_count: 5},
     2: { sold_count: 3},

  ...
     365: {sold_count: 2}
}

I'm trying to create a mapping in elasticsearch-dsl-py and having trouble.

I guess it would be similarly troublesome to create a mapping for this document?

  • edit

I'd like to view seasonal trend of data. so day-of-year 1 and day-of-year 365 is actually pretty close input point. Besides, I only need one year worth of data

question from:https://stackoverflow.com/questions/65936853/elasticsearch-how-to-store-data-per-date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can let ES generate the mappings for you with the help of dynamic template path matching:

PUT myindex
{
  "mappings": {
    "dynamic_templates": [
      {
        "catch_daily_statistics": {
          "path_match": "daily_statistics.*",
          "mapping": {
            "type": "object",
            "properties": {
              "sold_count": {
                "type": "long"
              }
            }
          }
        }
      }
    ]
  }
}

But before you proceed with this architectural choice, I'd recommend to first check this answer which deals with the difficulties of aggregating on dicts-of-dicts.

It'd be more reasonable to use daily indices as @Evaldas pointed out. But I'd go with nested fields of the form:

POST myindex/_doc
{
  "name": "bmw",
  "daily_stats": [
    {
      "day_date": "2021/01/01",
      "day_iso_num": 1,
      "sold_count": 5
    },
    { ... }
  ]
}

That way, you'll be able to perform


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...