Elasticsearch: 日付フィールドにサーバー時刻を自動的に設定し、タイムゾーンを更新します

ほとんどの場合、データには create_date という名前のフィールドが含まれています。日付フィールドがない場合でも、さまざまな形式やタイムゾーンで日付を処理することは、データ ウェアハウスにとって大きな課題です。同様に、データの変化を検出したい場合は、日付フィールドを正確に設定する必要があります。

Elasticsearch には、サーバーの日付をフィールドとして自動的に設定するオプションもあります。

パイプライン プロパティを取り込む setプロセッサ と dateプロセッサを使用します 。

取り込みパイプラインを作成する

まず、タイムスタンプフィールドを設定する必要があります。後で、日付ハンドラーを使用してフィールドを更新します。

日付ハンドラーにはいくつかの機能があります。target_field 属性もその 1 つです。target_field 属性が定義されていない場合は、field が評価され、@timestamp という新しいフィールドに書き込まれます。しかし、既存のフィールドを変更したいと考えています。

PUT _ingest/pipeline/sales-timestamp
{
  "description": "Set two different timestamp fields.",
  "processors": [
    {
      "set": {
        "field": "timestamp",
        "value": "{
   
   {
   
   {_ingest.timestamp}}}"
      }
    },
    {
      "date": {
          "field": "timestamp",
          "target_field": "tr_timestamp",
          "timezone": "+0300",
          "formats": [ "ISO8601"]
      }
    }
  ]
} 

上記のスクリプトを実行すると、("acknowledged": true) メッセージが表示されます。

{
  "acknowledged": true
}

さらに、DELETE コマンドを使用して削除したり、GET コマンドを使用して属性を確認したりできます。パイプラインを更新するには、PUT コマンドを再実行するだけで十分です。

GET _ingest/pipeline

DELETE _ingest/pipeline/sales-timestamp

次のステップでは、パイプラインを使用してインデックスを作成します。新しく確立された取り込みパイプラインの場合、index.default_pipeline を設定する必要があります。

日付フィールドには自動的に値が入力されますが、インデックスのマッピングで日付フィールドを定義する必要があります。

# Create "sales" Index
PUT sales
{
  "settings": {
    "index.default_pipeline": "sales-timestamp"
  },
  "mappings": {
     "properties": {
       "timestamp": { "type": "date" },
       "tr_timestamp": { "type": "date" },
       "name": { "type": "text" },
       "authour": { "type": "keyword" }
     }
   }
}

データ入力

複数のデータを同時にインデックスに追加する場合は、Bulk APIを使用できます。スクリプトを各行から解析しようとします。これは、一括挿入中にフォーマットされた JSON を使用できないことを意味します。

この手法を使用すると、Elasticsearch はデータに ID を自動的に割り当てます。

POST sales/_bulk
{"index":{}}
{"name":"The Lord of the Rings: The Fellowship of the Ring","authour":"J. R. R. Tolkien"}
{"index":{}}
{"name":"The Lord of the Rings 2: The Two Towers","authour":"J. R. R. Tolkien"}

次のステップでは、データを一覧表示または検索できるようになります。

GET sales/_search?filter_path=**.hits
{
  "size": 5, 
  "query": {
    "match_all": {}
  }
}

上記の操作の結果は次のようになります。

{
  "hits": {
    "hits": [
      {
        "_index": "sales",
        "_id": "rVjrTooBxPLM4Lwr4CwQ",
        "_score": 1,
        "_source": {
          "name": "The Lord of the Rings: The Fellowship of the Ring",
          "authour": "J. R. R. Tolkien",
          "tr_timestamp": "2023-09-01T07:06:35.783+03:00",
          "timestamp": "2023-09-01T04:06:35.783682Z"
        }
      },
      {
        "_index": "sales",
        "_id": "rljrTooBxPLM4Lwr4CwQ",
        "_score": 1,
        "_source": {
          "name": "The Lord of the Rings 2: The Two Towers",
          "authour": "J. R. R. Tolkien",
          "tr_timestamp": "2023-09-01T07:06:35.792+03:00",
          "timestamp": "2023-09-01T04:06:35.792130Z"
        }
      }
    ]
  }
}

この場合、tr_timestamp と timestamp データに細心の注意を払う必要があります。tr_timestamp列のデータは末尾に「+03:00」が付きます。

インデックスにさらにデータを追加できます。

POST sales/_bulk
{"index":{}}
{"name":"The Lord of the Rings 3: The Return of the King", "authour":"J. R. R. Tolkien"}
      {
        "_index": "sales",
        "_id": "r1j0TooBxPLM4LwreSyl",
        "_score": 1,
        "_source": {
          "name": "The Lord of the Rings 3: The Return of the King",
          "authour": "J. R. R. Tolkien",
          "tr_timestamp": "2023-09-01T07:15:59.397+03:00",
          "timestamp": "2023-09-01T04:15:59.397509Z"
        }
      }

パイプラインの使用方法の詳細については、記事「Elasticsearch: パイプラインの取り込み - ヒントとコツ」を参照してください。

おすすめ

転載: blog.csdn.net/UbuntuTouch/article/details/132619539