簡體   English   中英

Date_histogram Elasticsearch構面找不到字段

[英]Date_histogram Elasticsearch facet can't find field

我正在使用date_histogram構面來查找基於紀元時間戳的結果。 結果顯示在直方圖上,日期在x軸上,事件計數在y軸上。 這是我不起作用的代碼:

angular.module('controllers', [])
  .controller('FacetsController', function($scope, $http) {
    var payload = {
      query: {
        match: {
          run_id: '9'
        }
      },
      facets: {
        date: {
          date_histogram: {
            field: 'event_timestamp',
            factor: '1000',
            interval: 'second'
          }
        }
      }
    }

如果我使用的是ISO8601格式的字段'@timestamp',它會起作用; 但是,我現在需要它來與Epoch時間戳一起使用。

這是我的Elasticsearch中的示例,可能會導致一些答案:

{"@version":"1",
"@timestamp":"2014-07-04T13:13:35.372Z","type":"automatic",
"installer_version":"0.3.0",
"log_type":"access.log","user_id":"1",
"event_timestamp":"1404479613","run_id":"9"}
},

運行此命令時,出現以下錯誤: POST 400 (Bad Request)

關於這里可能出什么問題的任何想法? 我不明白為什么我與使用兩個不同的字段會有如此不同,因為唯一的區別是格式。 我盡了最大的努力研究,發現我應該使用“因子”,但這似乎並不能解決我的問題。 我可能在犯一個愚蠢的初學者錯誤!

您需要首先設置索引。 Elasticsearch擅長使用默認值,但無法確定所提供的值是時間戳,整數還是字符串。 因此,將相同的事情告訴Elasticsearch是您的工作。

讓我舉例說明。 讓我們考慮以下文檔是您要索引的內容:

{
   "@version": "1",
   "@timestamp": "2014-07-04T13:13:35.372Z",
   "type": "automatic",
   "installer_version": "0.3.0",
   "log_type": "access.log",
   "user_id": "1",
   "event_timestamp": "1404474613",
   "run_id": "9"
}

因此,最初您沒有索引,而是通過發出HTTP請求來索引文檔,如下所示:

POST /test/date_experiments
{
   "@version": "1",
   "@timestamp": "2014-07-04T13:13:35.372Z",
   "type": "automatic",
   "installer_version": "0.3.0",
   "log_type": "access.log",
   "user_id": "1",
   "event_timestamp": "1404474613",
   "run_id": "9"
}

這將創建一個稱為test的新索引和一個名為date_experiments索引test的新doc類型。

您可以通過以下方式檢查此文檔類型date_experiments的映射:

GET /test/date_experiments/_mapping

結果中得到的是由Elasticsearch生成的自動生成的映射:

{
   "test": {
      "date_experiments": {
         "properties": {
            "@timestamp": {
               "type": "date",
               "format": "dateOptionalTime"
            },
            "@version": {
               "type": "string"
            },
            "event_timestamp": {
               "type": "string"
            },
            "installer_version": {
               "type": "string"
            },
            "log_type": {
               "type": "string"
            },
            "run_id": {
               "type": "string"
            },
            "type": {
               "type": "string"
            },
            "user_id": {
               "type": "string"
            }
         }
      }
   }
}

請注意, event_timestamp字段的類型設置為string 這就是為什么您的date_histogram無法正常工作的原因。 還要注意, @timestamp字段的類型已經是date因為您以標准格式推送了日期,這使Elasticsearch容易識別出您打算在該字段中推送日期。

通過向/test/date_experiments發送DELETE請求來刪除此映射,並從頭開始。

這一次而不是先推送文檔,而是根據需求進行映射,以便將event_timestamp字段視為日期。

發出以下HTTP請求:

PUT /test/date_experiments/_mapping
{
   "date_experiments": {
      "properties": {
         "@timestamp": {
            "type": "date"
         },
         "@version": {
            "type": "string"
         },
         "event_timestamp": {
            "type": "date"
         },
         "installer_version": {
            "type": "string"
         },
         "log_type": {
            "type": "string"
         },
         "run_id": {
            "type": "string"
         },
         "type": {
            "type": "string"
         },
         "user_id": {
            "type": "string"
         }
      }
   }
}

注意,我已將event_timestamp字段的類型更改為date 我沒有指定格式,因為Elasticsearch善於理解一些標准格式,例如@timestamp字段中您輸入日期的情況。 在這種情況下,Elasticsearch將能夠理解您正在嘗試推送UNIX時間戳並在內部對其進行轉換,以將其視為日期並允許對其進行所有日期操作。 您可以在映射中指定日期格式,以防萬一您要推送的日期不是任何標准格式。

現在,您可以像以前一樣開始為文檔建立索引並開始運行日期查詢和構面。

您應該閱讀有關映射日期格式的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM