summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/ingest/processor/DeDotProcessor.java
blob: 62063a49fd02a7731bde21b12e9029b0c8c5e5d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.ingest.processor;

import org.elasticsearch.ingest.core.AbstractProcessor;
import org.elasticsearch.ingest.core.AbstractProcessorFactory;
import org.elasticsearch.ingest.core.ConfigurationUtils;
import org.elasticsearch.ingest.core.IngestDocument;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Processor that replaces dots in document field names with a
 * specified separator.
 */
public class DeDotProcessor extends AbstractProcessor {

    public static final String TYPE = "dedot";
    static final String DEFAULT_SEPARATOR = "_";

    private final String separator;

    DeDotProcessor(String tag, String separator) {
        super(tag);
        this.separator = separator;
    }

    public String getSeparator() {
        return separator;
    }

    @Override
    public void execute(IngestDocument document) {
        deDot(document.getSourceAndMetadata());
    }

    @Override
    public String getType() {
        return TYPE;
    }

    /**
     * Recursively iterates through Maps and Lists in search of map entries with
     * keys containing dots. The dots in these fields are replaced with {@link #separator}.
     *
     * @param obj The current object in context to be checked for dots in its fields.
     */
    private void deDot(Object obj) {
        if (obj instanceof Map) {
            @SuppressWarnings("unchecked")
            Map<String, Object> doc = (Map) obj;
            Iterator<Map.Entry<String, Object>> it = doc.entrySet().iterator();
            Map<String, Object> deDottedFields = new HashMap<>();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                deDot(entry.getValue());
                String fieldName = entry.getKey();
                if (fieldName.contains(".")) {
                    String deDottedFieldName = fieldName.replaceAll("\\.", separator);
                    deDottedFields.put(deDottedFieldName, entry.getValue());
                    it.remove();
                }
            }
            doc.putAll(deDottedFields);
        } else if (obj instanceof List) {
            @SuppressWarnings("unchecked")
            List<Object> list = (List) obj;
            for (Object value : list) {
                deDot(value);
            }
        }
    }

    public static class Factory extends AbstractProcessorFactory<DeDotProcessor> {

        @Override
        public DeDotProcessor doCreate(String processorTag, Map<String, Object> config) throws Exception {
            String separator = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "separator");
            if (separator == null) {
                separator = DEFAULT_SEPARATOR;
            }
            return new DeDotProcessor(processorTag, separator);
        }
    }
}