All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.client.transform.PreviewTransformResponse Maven / Gradle / Ivy

There is a newer version: 8.0.0-alpha2
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.client.transform;

import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;

public class PreviewTransformResponse {

    public static class GeneratedDestIndexSettings {
        static final ParseField MAPPINGS = new ParseField("mappings");
        private static final ParseField SETTINGS = new ParseField("settings");
        private static final ParseField ALIASES = new ParseField("aliases");

        private final Map mappings;
        private final Settings settings;
        private final Set aliases;

        private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(
            "transform_preview_generated_dest_index",
            true,
            args -> {
                @SuppressWarnings("unchecked")
                Map mappings = (Map) args[0];
                Settings settings = (Settings) args[1];
                @SuppressWarnings("unchecked")
                Set aliases = (Set) args[2];

                return new GeneratedDestIndexSettings(mappings, settings, aliases);
            }
        );

        static {
            PARSER.declareObject(optionalConstructorArg(), (p, c) -> p.mapOrdered(), MAPPINGS);
            PARSER.declareObject(optionalConstructorArg(), (p, c) -> Settings.fromXContent(p), SETTINGS);
            PARSER.declareObject(optionalConstructorArg(), (p, c) -> {
                Set aliases = new HashSet<>();
                while ((p.nextToken()) != XContentParser.Token.END_OBJECT) {
                    aliases.add(Alias.fromXContent(p));
                }
                return aliases;
            }, ALIASES);
        }

        public GeneratedDestIndexSettings(Map mappings, Settings settings, Set aliases) {
            this.mappings = mappings == null ? Collections.emptyMap() : Collections.unmodifiableMap(mappings);
            this.settings = settings == null ? Settings.EMPTY : settings;
            this.aliases = aliases == null ? Collections.emptySet() : Collections.unmodifiableSet(aliases);
        }

        public Map getMappings() {
            return mappings;
        }

        public Settings getSettings() {
            return settings;
        }

        public Set getAliases() {
            return aliases;
        }

        public static GeneratedDestIndexSettings fromXContent(final XContentParser parser) {
            return PARSER.apply(parser, null);
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }

            if (obj == null || obj.getClass() != getClass()) {
                return false;
            }

            GeneratedDestIndexSettings other = (GeneratedDestIndexSettings) obj;
            return Objects.equals(other.mappings, mappings)
                && Objects.equals(other.settings, settings)
                && Objects.equals(other.aliases, aliases);
        }

        @Override
        public int hashCode() {
            return Objects.hash(mappings, settings, aliases);
        }
    }

    public static final ParseField PREVIEW = new ParseField("preview");
    public static final ParseField GENERATED_DEST_INDEX_SETTINGS = new ParseField("generated_dest_index");

    private final List> docs;
    private final GeneratedDestIndexSettings generatedDestIndexSettings;

    private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(
        "data_frame_transform_preview",
        true,
        args -> {
            @SuppressWarnings("unchecked")
            List> docs = (List>) args[0];
            GeneratedDestIndexSettings generatedDestIndex = (GeneratedDestIndexSettings) args[1];

            // ensure generatedDestIndex is not null
            if (generatedDestIndex == null) {
                // BWC parsing the output from nodes < 7.7
                @SuppressWarnings("unchecked")
                Map mappings = (Map) args[2];
                generatedDestIndex = new GeneratedDestIndexSettings(mappings, null, null);
            }

            return new PreviewTransformResponse(docs, generatedDestIndex);
        }
    );
    static {
        PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> p.mapOrdered(), PREVIEW);
        PARSER.declareObject(optionalConstructorArg(), (p, c) -> GeneratedDestIndexSettings.fromXContent(p), GENERATED_DEST_INDEX_SETTINGS);
        PARSER.declareObject(optionalConstructorArg(), (p, c) -> p.mapOrdered(), GeneratedDestIndexSettings.MAPPINGS);
    }

    public PreviewTransformResponse(List> docs, GeneratedDestIndexSettings generatedDestIndexSettings) {
        this.docs = docs;
        this.generatedDestIndexSettings = generatedDestIndexSettings;
    }

    public List> getDocs() {
        return docs;
    }

    public GeneratedDestIndexSettings getGeneratedDestIndexSettings() {
        return generatedDestIndexSettings;
    }

    public Map getMappings() {
        return generatedDestIndexSettings.getMappings();
    }

    public Settings getSettings() {
        return generatedDestIndexSettings.getSettings();
    }

    public Set getAliases() {
        return generatedDestIndexSettings.getAliases();
    }

    public CreateIndexRequest getCreateIndexRequest(String index) {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
        createIndexRequest.aliases(generatedDestIndexSettings.getAliases());
        createIndexRequest.settings(generatedDestIndexSettings.getSettings());
        createIndexRequest.mapping(generatedDestIndexSettings.getMappings());

        return createIndexRequest;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if (obj == null || obj.getClass() != getClass()) {
            return false;
        }

        PreviewTransformResponse other = (PreviewTransformResponse) obj;
        return Objects.equals(other.docs, docs) && Objects.equals(other.generatedDestIndexSettings, generatedDestIndexSettings);
    }

    @Override
    public int hashCode() {
        return Objects.hash(docs, generatedDestIndexSettings);
    }

    public static PreviewTransformResponse fromXContent(final XContentParser parser) throws IOException {
        return PARSER.parse(parser, null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy