【问题标题】:Run Elasticsearch processor on all the fields of a document在文档的所有字段上运行 Elasticsearch 处理器
【发布时间】:2021-07-04 01:08:24
【问题描述】:

我正在尝试修剪和小写被索引到 Elasticsearch 中的文档的所有值

可用的处理器具有字段键是强制性的。这意味着一个人只能在一个领域使用处理器

有没有办法在文档的所有字段上运行处理器?

【问题讨论】:

    标签: elasticsearch processor


    【解决方案1】:

    确实有。使用script processor,但要注意_type、_id 等保留键:

    PUT _ingest/pipeline/my_string_trimmer
    {
      "description": "Trims and lowercases all string values",
      "processors": [
        {
          "script": {
            "source": """
              def forbidden_keys = [
                '_type',
                '_id',
                '_version_type',
                '_index',
                '_version'
              ];
              
              def corrected_source = [:];
              
              for (pair in ctx.entrySet()) {
                def key = pair.getKey();
                if (forbidden_keys.contains(key)) {
                  continue;
                }
                def value = pair.getValue();
                
                if (value instanceof String) {
                  corrected_source[key] = value.trim().toLowerCase();
                } else {
                  corrected_source[key] = value;
                }
              }
              
              // overwrite the original
              ctx.putAll(corrected_source);
            """
          }
        }
      ]
    }
    

    使用示例文档进行测试:

    POST my-index/_doc?pipeline=my_string_trimmer
    {
      "abc": " DEF ",
      "def": 123,
      "xyz": false
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多