【发布时间】: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 的值为 testValue1 和 fields.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