「return」コマンドは、サブサーチの結果をメインサーチに返却します。
使いこなすと用意にデータの絞り込み、サーチ処理を効率化できたり、返却した値を別フィールドに設定することができます。
サンプルデータ
以下のメタデータをサンプルに使用します。
| metadata type=sources | eval recentTime = strftime(recentTime,"%Y-%m-%d %H:%M:%S") , lastTime = strftime(lastTime,"%Y-%m-%d %H:%M:%S") ,firstTime = strftime(firstTime,"%Y-%m-%d %H:%M:%S")
結果

基本的な使い方
返却(return)する値は、3種類です。
- フィールド名=値
- 任意のフィールド名=値
- 値
「フィールド名=値」の返却
「return フィールド名」を指定します。
| metadata type=sources | eval recentTime = strftime(recentTime,"%Y-%m-%d %H:%M:%S") , lastTime = strftime(lastTime,"%Y-%m-%d %H:%M:%S") ,firstTime = strftime(firstTime,"%Y-%m-%d %H:%M:%S") | sort recentTime desc | return source
結果

「source=”zenkoku_20190403.csv”」が返却されました。
これをサブサーチ結果として利用することで、「source=”zenkoku_20190403.csv”」のデータのみを絞り込みすることが出来ます。
以下は、サブサーチの結果をreturnコマンドで返却してデータの絞り込みをしている例です。
「source=”zenkoku_20190403.csv”」のデータのみ抽出されています。
sourcetype="adressJapanCSV" [| metadata type=sources | sort recentTime desc | return source] | table source | stats count by source
結果

サブサーチをreturnされた結果に置き換えると以下のサーチ分と同様になります。(ANDは省略可能)
sourcetype="adressJapanCSV" AND source="zenkoku_20190403.csv" | table source | stats count by source
結果

「任意のフィールド名=値」の返却
「return フィールド名=フィールド名」を指定します。
メインサーチとサブサーチでフィール名が異なる場合などで使用します。
| metadata type=sources | eval recentTime = strftime(recentTime,"%Y-%m-%d %H:%M:%S") , lastTime = strftime(lastTime,"%Y-%m-%d %H:%M:%S") ,firstTime = strftime(firstTime,"%Y-%m-%d %H:%M:%S") | sort recentTime desc | return sourcetype=source
結果

「値」の返却
「return $フィールド名」を指定します。
| metadata type=sources | eval recentTime = strftime(recentTime,"%Y-%m-%d %H:%M:%S") , lastTime = strftime(lastTime,"%Y-%m-%d %H:%M:%S") ,firstTime = strftime(firstTime,"%Y-%m-%d %H:%M:%S") | sort recentTime desc | return $source
結果

メインサーチのフィールドにサブサーチ結果を設定したい場合等に使います。
以下のサンプルコードでは、サブサーチでトータル件数を取得してメインサーチのフィールドに設定しています。
sourcetype="adressJapanCSV" | stats count by source | eval testField= [search sourcetype="adressJapanCSV" | stats count as Total | return $Total]
結果

その他のオプション
複数フィールドの指定
「return フィールド名 フィールド名」を指定すると、AND条件の結果を返却します。
| metadata type=sources | eval recentTime = strftime(recentTime,"%Y-%m-%d %H:%M:%S") , lastTime = strftime(lastTime,"%Y-%m-%d %H:%M:%S") ,firstTime = strftime(firstTime,"%Y-%m-%d %H:%M:%S") | sort recentTime desc | return source type
結果

複数値の返却
「return 数値 フィールド名」を指定すると、数値で指定した件数分の値をOR条件の結果を返却します。
| metadata type=sources | eval recentTime = strftime(recentTime,"%Y-%m-%d %H:%M:%S") , lastTime = strftime(lastTime,"%Y-%m-%d %H:%M:%S") ,firstTime = strftime(firstTime,"%Y-%m-%d %H:%M:%S") | sort recentTime desc | return 3 source
結果

複数フィールド & 複数値の返却
上記を組み合わせることも可能です。
| metadata type=sources | eval recentTime = strftime(recentTime,"%Y-%m-%d %H:%M:%S") , lastTime = strftime(lastTime,"%Y-%m-%d %H:%M:%S") ,firstTime = strftime(firstTime,"%Y-%m-%d %H:%M:%S") | sort recentTime desc | return 3 source type
結果

まとめ
returnコマンドは使いこなすと大変便利で、サーチ文で出来ることの幅がかなり広がるので積極的に使っていきたいです。