2017-09-17 97 views
0

我想從彈性搜索中獲得最近30天的文檔,但它返回空白。在elasticsearch中查詢日期範圍

這是我的映射:

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改變從日期字符串字段類型。我的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的映射chasnges這之後:

{ 
    "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和另一個是文本

+0

願你請添加您的查詢的全部輸出?您收藏的文件數量有多少?你需要自己匹配的文件還是查詢中的日期間隔文件數? –

+0

你也可以提供'GET/books /'的輸出嗎? –

+0

我發現了這個問題,我不能解決這個問題......我添加了映射,但是當我將日誌文件發送到彈性文件時,它會將文本類型從日期更改爲文本。它會考慮錯誤的領域。 我將添加將被記錄的json以及我的問題中的logstash配置文件。 –

回答

0

你需要把fromto在同一範圍內,像這樣:

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

它再次返回空匹配 –

+0

你能顯示一個應該匹配的文檔嗎? – Val

+0

你的日期字段被命名爲'BookCreated'而不是'booksCreated'。我已經更新了我的答案。請再試一次。 – Val