【问题标题】:Post a dynamic template array to ElasticSearch将动态模板数组发布到 ElasticSearch
【发布时间】:2018-09-18 21:34:27
【问题描述】:

我想将动态对象数组发布到弹性搜索。如果我发布一个对象,所有字段都将正确可见,但如果我使用数组,我只会看到一个带有字符串版本的 Array 的字段。

```
const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client();
const bodyMsg = [
  { index: { _index: 'logs-2018.04.09', _type: 'log', pipeline: null } },
  {
    '@timestamp': '2018-04-09T12:17:17.645Z',
    message: 'logmessage1',
    severity: 'info',
    fields: {
      temp:
        {
          testKey: 'testValue',
        }
    }
  }];

client.bulk({
  body: bodyMsg,
  waitForActiveShards: this.waitForActiveShards,
  timeout: '10ms',
  // type: this.type
}).then((res) => {
  if (res.errors && res.items) {
    res.items.forEach((item) => {
      if (item.index && item.index.error) {
        console.error('Elasticsearch index error', item.index.error);
      }
    });
  }
})
```

对于上面的代码,我确实将对象作为fields.temp.testKey 发布到elasticsearch,值为testValue。 但是,如果我对字段使用数组,即

```
    fields:  {
      temp:
        [{
          testKey: 'testValue'
        }]
    }
```

然后在 ES 上,我看到键 fields 的值为 temp[{ testKey: 'testValue' }],但我期望的是如下所示: fields.temp[0].testKey 的值为 testValue1fields.temp[1].testKey 的值为 testValue2 等等。

我的动态模板是:

```
  "mappings": {
    "_default_": {
      "_all": { "enabled": false, "omit_norms": true },
      "_source": { "enabled": true },
      "_ttl": { "enabled": true, "default": "900d" },
      "dynamic_templates": [
        {
          "string_fields": {
            "match": "*",
            "match_mapping_type": "keyword",
            "mapping": {
              "type": "text", "index": true, "omit_norms": true,
              "fields": {
                "raw": { "type": "keyword", "index": true, "ignore_above": 256 }
              }
            }
          }
        }
      ],
      "properties": {
        "@timestamp": { "type": "date" },
        "@version": { "type": "keyword" },
        "message": { "type": "text", "index": true },
        "severity": { "type": "keyword", "index": true },
        "fields": {
          "type": "object",
          "dynamic": true
        }
      }
    }
  }
```

请建议在发布到 ES 时如何处理数组?

【问题讨论】:

    标签: node.js elasticsearch elasticsearch-5


    【解决方案1】:

    每个文档都需要是数组的一个新元素

    [indexDef, item1, item2, item3 ... itemx]
    

    添加两个文档的示例如下:

    [
      { index: { _index: 'logs-2018.04.09', _type: 'log', pipeline: null } },
      {
        '@timestamp': '2018-04-09T12:17:17.645Z',
        message: 'logmessage1',
        severity: 'info',
        fields: {
          temp:
            {
              testKey: 'testValue',
            }
        }
      },
    {
        '@timestamp': '2018-04-09T12:17:17.645Z',
        message: 'logmessage2',
        severity: 'info',
        fields: {
          temp:
            {
              testKey: 'testValue2',
            }
        }
      }];
    

    您需要将 @timestamp severityfields 添加到数组中的每个元素。

    或者,如果您希望在文档中存储一个数组并直接从该数组中查找一个值,只需插入一个文档

    {
        '@timestamp': '2018-04-09T12:17:17.645Z',
        message: 'logmessage1',
        severity: 'info',
        fields: {
          temp:
            {
              values: [
                testKey: 'testValue';
               ]
            }
        }
      }
    

    可以通过temp.values[0].testKey访问

    但是我不确定这是如何索引的……通常每个键都会放在一个单独的文档中。

    【讨论】:

    • 感谢您的回复。但我需要使用 Array fields 发布一条消息。这是 ES 做不到的吗?
    • 那么它是一个包含多个字段的条目?我不确定我是否理解您想要达到的结果
    • 是的,它是一个包含数组或多个字段的条目。我基本上需要在 ES 中看到类似这样的键值对:fields.temp[0].testKey 具有值 testValue1fields.temp[1].testKey 具有值 testValue2 等等。
    • 人力资源管理。不确定你想要什么,因为字段只是对象索引,我已经用一个可能的解决方案更新了上面的答案,你可以存储一个包含数组的对象,即fields.temp.values[0].testkey
    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2015-07-24
    相关资源
    最近更新 更多