首页 文章

查询elasticsearch中的日期范围

提问于
浏览
0

我想从弹性搜索中获取过去30天的文档,但它返回空白 .

这是我的映射:

PUT /books
{
    "mappings": {
        "impressions": {
            "properties": {

                "booksCreated" : {
                  "type": "date",
                  "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
                  "index": true

                }
            }
        }
    }
}

这是我的疑问:

POST /books/_search?size=0
{
    "aggs": {
        "range": {
            "date_range": {
                "field": "booksCreated",
                "format": "yyyy-MM-dd",
                "ranges": [
                    { "to": "now" }, 
                    { "from": "now-1M/M" } 
                ]
            }
        }
    }
}

我已经尝试了所有可能的方法,但它返回空 .

但我可以查询@timestamp字段

问题是logstash将字段类型从date更改为string . 我的json是:

{
    "index":"books",
    "type":"book",
    "body":{
    "impressions":{
    "_source":{
    "enabled":true
    },
    "properties":{
    "BookCreated":"2017-09-18 12:18:39"
    }
    }
  }
 }

和我的logstash配置:

input {
    file {
        path => "E:\data2\log\logstash.log"
        start_position => "beginning"
        sincedb_path => "/dev/null"
        codec => json
    }
}

filter {
    mutate {
         strip => ["message"]
    }
}

output {
    elasticsearch {
        hosts => "localhost"
        index => "books"
        document_type => "book"         
     }

}

我将json记录到一个日志文件,logstash将它们发送到elasticsearch

添加json后,映射变为:

{
  "Books": {
    "mappings": {
      "Books": {
        "properties": {
          "@timestamp": {
            "type": "date"
          },
          "@version": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "BookCreated": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "body": {
            "properties": {
              "Books": {
                "properties": {
                  "_source": {
                    "properties": {
                      "enabled": {
                        "type": "boolean"
                      }
                    }
                  },
                  "properties": {
                    "properties": {
                      "BookCreated": {
                        "type": "text",
                        "fields": {
                          "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "host": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "index": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "path": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "type": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

它有两个BookCreated一个isdate,另一个是文本

1 回答

  • 0

    您需要将 fromto 放在相同的范围内,如下所示:

    POST /books/_search?size=0
    {
        "aggs": {
            "range": {
                "date_range": {
                    "field": "BookCreated",
                    "format": "yyyy-MM-dd",
                    "ranges": [
                        { 
                          "from": "now-1M/M",
                          "to": "now"
                        } 
                    ]
                }
            }
        }
    }
    

相关问题