Elasticsearch将嵌套的json视为纯字符串:无法在start_object上获取文本

时间:2019-03-21 10:39:36

标签: json elasticsearch mapping

我正在尝试添加一些具有深度嵌套json结构的松紧json文件。

我想将某些嵌套字段视为未编入索引的import { createWriteStream, supported } from 'streamsaver'; import { WritableStream } from 'web-streams-polyfill/ponyfill/es6'; const downloadWithToken = async (url, name) => { const token = localStorage.getItem('token'); const authorization = 'Bearer ' + token; const headers = new Headers({ authorization }); const options = { headers }; let response = await fetch(url, options) if (!supported) { window.WritableStream = WritableStream; } const filestream = createWriteStream(name); const writer = filestream.getWriter(); if (response.body.pipeTo) { writer.releaseLock(); return response.body.pipeTo(filestream); } const reader = response.body.getReader(); const pump = () => reader .read() .then(({ value, done }) => (done ? writer.close() : writer.write(value).then(pump))); pump(); }; ,而不是json skip ,或者这是不可能的。

例如,我有text个文档,如下所示:

event

我希望将{ "cluster": "production1", "header": { "os": "ios", "experiments": { "e1": { "field1": "value1", "field2": "value2" "array": ["id0"] }, "e2": { "field3" : "value3", "array": ["id1", "id2", "id3"] } } }, } 字段下的所有内容都视为一个纯文本。
我尝试了这种映射,但是它对我不起作用,因为header.experiments字段是对象,而不是字符串:experiments

error: can't get text on a start_object

如何使ElasticSearch将子字段视为纯文本?

1 个答案:

答案 0 :(得分:1)

要完全跳过该字段,可以在enabled字段的索引映射中将false设置为experiments

"mappings": {
  "event": {
    "properties": {
      "cluster": {
        "type": "keyword",
        "index": true
      },
      "header": {
        "os": {
          "type": "keyword",
          "index": true
        },
        "experiments": {
          "enabled": false
        }
      }
    }
  }
}
  

enabled设置(仅可应用于映射类型和object字段)使Elasticsearch完全跳过对字段内容的解析。仍然可以从_source字段中检索JSON,但是无法搜索或以其他任何方式存储JSON:

有关详情,请参见:
https://www.elastic.co/guide/en/elasticsearch/reference/current/enabled.html