Elasticsearch: フィールド名を保持

Elasticsearch ユーザーとして、私たちはさまざまな場所からデータを収集します。Logstash、Beats、およびその他のツールを使用してデータをスクレイピングし、Elasticsearch に送信します。場合によっては、データ自体を制御できず、データの構造を管理する必要があり、データを取り込むときにフィールド名を処理する必要さえあります。

Elasticsearch には、ドキュメントで使用できない予約済みのフィールド名がいくつかあります。

これらのフィールドのいずれかが含まれているドキュメントは、インデックスを作成できません。ただし、これらのフィールド名をドキュメントのどこでも使用できないというわけではありません。この制限は、ルート ノードに対してのみ有効です。したがって、次のドキュメントは索引付けできません。

PUT twitter/_doc/1
{
  "_id": 1
}

次のようなエラーが表示されます。

しかし、次のような文書をうまく書くことができます:

PUT twitter/_doc/1
{
  "user": {
    "_id": 1,
    "name": "liuxg"
  }
}

その理由は、上記の _id がルート ノードの下のフィールドではないためです。

したがって、上記の _id フィールドでわかるように、次のフィールド名をドキュメントのルート フィールド名として使用することはできません。

_id
_field_names
_index
_seq_no
_nested_path
_ignored
_routing
_data_stream_timestamp
_tier
_version
_feature
_source
_primary_term *
_type *

注:上記の*でマークされたフィールドは 、elasticsearch の古いバージョンの予約済みキーワードでもあります。

これは、ルート フィールドとしてこれらのフィールド名を持つドキュメントがある場合を意味します。エラーが発生します。たとえば、Logstash を使用してそのデータを移動するとします。次の内容のファイルがあります。

{
	"ImdbId": "tt0030629",
	"_id": "tt0030629",
	"name": "Prison Without Bars",
	"year": "1938",
	"certificate": "Approved",
	"runtime": "72 min",
	"genre": ["Crime", " Drama", " Romance"],
	"ratingValue": "6.2",
	"summary_text": "Suzanne, Renee, Nina and Marta all hate being in prison, being slapped and treated badly, and so all the girls are trying to escape. Madame Appel just causes chaos all the time, with her ...                See full summary\u00a0\u00bb",
	"ratingCount": "66"
} 

{
	"ImdbId": "tt0030528",
	"_id": "tt0030528",
	"name": "Orage",
	"year": "1938",
	"certificate": "",
	"runtime": "98 min",
	"genre": ["Drama"],
	"ratingValue": "5.7",
	"summary_text": "Orage is a 1938 French drama film directed by Marc All\u00e9gret. The screenplay was written by Marcel Achard and H.G. Lustig, based on play \"Le venin\" by Henri Bernstein. The films stars ...                See full summary\u00a0\u00bb",
	"ratingCount": "66"
} 

そのため、Logstash を使用して取り込もうとすると、次のエラーが発生します。これらの文書を個別に索引付けしても、上記のエラーが発生します。たとえば、次の例を使用します。

sample.log

{"_id":1,"timestamp":"2019-09-12T13:43:42Z","paymentType":"Amex","name":"Merrill Duffield","gender":"Female","ip_address":"132.150.218.21","purpose":"Toys","country":"United Arab Emirates","age":33}
{"_id":2,"timestamp":"2019-08-11T17:55:56Z","paymentType":"Visa","name":"Darby Dacks","gender":"Female","ip_address":"77.72.239.47","purpose":"Shoes","country":"Poland","age":55}
{"_id":3,"timestamp":"2019-07-14T04:48:25Z","paymentType":"Visa","name":"Harri Cayette","gender":"Female","ip_address":"227.6.210.146","purpose":"Sports","country":"Canada","age":27}
{"_id":4,"timestamp":"2020-02-29T12:41:59Z","paymentType":"Mastercard","name":"Regan Stockman","gender":"Male","ip_address":"139.224.15.154","purpose":"Home","country":"Indonesia","age":34}
{"_id":5,"timestamp":"2019-08-03T19:37:51Z","paymentType":"Mastercard","name":"Wilhelmina Polle","gender":"Female","ip_address":"252.254.68.68","purpose":"Health","country":"Ukraine","age":51}

logstash_input.conf

input {
  file {
    path => "//Users/liuxg/elastic/logstash-8.6.2/sample.log"
    type    => "applog"
    codec   => "json"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}

output {
   stdout {
      codec => rubydebug
   }

  elasticsearch {
    hosts => ["localhost:9200"]
    index => "json-%{+YYYY.MM.dd}"
  }
}

次のコマンドを使用して、データの収集を開始します。

$ pwd
/Users/liuxg/elastic/logstash-8.6.2
$ ls sample.log logstash_input.conf 
logstash_input.conf sample.log
./bin/logstash -f logstash_input.conf 

ご覧のとおり、エラーは上記と同じです。それで、私たちは何をする必要がありますか?これらのタイプのデータ操作を処理するソリューションがあります。ソース上のドキュメントを修正するか、mutate フィルターを使用して Logstash でドキュメントを管理できます。

logstash_input.conf

input {
  file {
    path => "//Users/liuxg/elastic/logstash-8.6.2/sample.log"
    type    => "applog"
    codec   => "json"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}

filter {
  mutate {
    rename => {"_id" => "id"}
  }
}

output {
   stdout {
      codec => rubydebug
   }

  elasticsearch {
    hosts => ["localhost:9200"]
    index => "json-%{+YYYY.MM.dd}"
  }
}

Logstash を再度実行します。

./bin/logstash -f logstash_input.conf 

今回はデータが正常に書き込まれたことがわかり、Kibana で表示できます。

logstash を再実行すると、ドキュメントが適切にインデックス化されていることがわかります。一方で、取り込みパイプラインのトラブルシューティングを試みています。最初は、これが私にとってより良い解決策かもしれないと思いました。しかし面白いことに、取り込みパイプラインでさまざまなことを試しましたが、解決策が見つかりません。取り込みパイプラインを使用する方法は次のとおりです。

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "rename": {
          "field": "_id",
          "target_field": "id"
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "myindex",
      "_id": 1,
      "_source": {
        "_id": "2"
      }
    }
  ]
}

次のエラーが表示されます。

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "unexpected metadata [_id:1] in source"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "unexpected metadata [_id:1] in source"
  },
  "status": 400
}

おすすめ

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