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

org.elasticsearch.join.mapper.Relations Maven / Gradle / Ivy

There is a newer version: 7.17.26
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.join.mapper;

import org.elasticsearch.common.xcontent.support.XContentMapValues;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * Defines a relationship between a parent type and a set of child types
 */
class Relations {

    final String parent;
    final Set children;

    Relations(String parent, Set children) {
        this.parent = parent;
        this.children = children;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Relations relation = (Relations) o;
        return Objects.equals(parent, relation.parent) && Objects.equals(children, relation.children);
    }

    @Override
    public int hashCode() {
        return Objects.hash(parent, children);
    }

    @Override
    public String toString() {
        return parent + "->" + children;
    }

    static List parse(Object fieldNode) {
        List parsed = new ArrayList<>();
        Map relations = XContentMapValues.nodeMapValue(fieldNode, "relations");
        for (Map.Entry relation : relations.entrySet()) {
            final String parent = relation.getKey();
            Set children;
            if (XContentMapValues.isArray(relation.getValue())) {
                children = new HashSet<>(Arrays.asList(XContentMapValues.nodeStringArrayValue(relation.getValue())));
            } else {
                children = Collections.singleton(relation.getValue().toString());
            }
            parsed.add(new Relations(parent, children));
        }
        return parsed;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy