public Resource()

in mr/src/main/java/org/elasticsearch/hadoop/rest/Resource.java [49:150]


    public Resource(Settings settings, boolean read) {
        String resource = (read ? settings.getResourceRead() : settings.getResourceWrite());

        // 1) Resource must not be null
        String errorMessage = "invalid resource given; expecting [index]/[type] - received ";
        Assert.hasText(resource, errorMessage + resource);

        // 2) Resource may contain a query, so retrieve it and complain if it's already set
        if (resource.contains("?") || resource.contains("&")) {
            if (StringUtils.hasText(settings.getQuery())) {
                throw new EsHadoopIllegalArgumentException(String.format(
                        "Cannot specify a query in the target index AND through %s", ConfigurationOptions.ES_QUERY));
            }

            int index = resource.indexOf("?");
            if (index > 0) {
                String query = resource.substring(index);

                // clean resource
                resource = resource.substring(0, index);
                index = resource.lastIndexOf("/");
                resource = (index > 0 ? resource.substring(0, index) : resource);

                settings.setProperty(ConfigurationOptions.ES_RESOURCE, resource);
                settings.setQuery(query);
            }
        }

        String res = StringUtils.sanitizeResource(resource);

        // 3) Resource must contain an index, but may not necessarily contain a type.
        // This is dependent on the version of ES we are talking with.
        int slash = res.indexOf("/");
        boolean typeExists = slash >= 0;

        EsMajorVersion esMajorVersion = settings.getInternalVersionOrThrow();
        if (esMajorVersion.after(EsMajorVersion.V_7_X)) {
            // Types can no longer the specified at all! Index names only!
            if (typeExists) {
                throw new EsHadoopIllegalArgumentException(String.format(
                        "Detected type name in resource [%s]. Remove type name to continue.",
                        resource
                ));
            }
        }
        if (esMajorVersion.onOrBefore(EsMajorVersion.V_7_X)) {
            // Type can be specified, but a warning will be returned. An ES 7.X cluster will accept types if include_type_name is true,
            // which we will set in the case of a type existing.
            // This is onOrBefore because we want to print the deprecation log no matter what version of ES they're running on.
            if (typeExists) {
                LOG.warn(String.format(
                        "Detected type name in resource [%s]. Type names are deprecated and will be removed in a later release.",
                        resource
                ));
            }
        }
        if (esMajorVersion.onOrBefore(EsMajorVersion.V_6_X)) {
            // Type is required for writing via the bulk API, but not for reading. No type on a read resource means to read all types.
            // This is important even if we're on a 6.x cluster that enforces a single type per index. 6.x STILL supports opening old 5.x
            // indices in order to ease the upgrade process!!!!
            if (!read && !typeExists) {
                throw new EsHadoopIllegalArgumentException(String.format(
                        "No type found; Types are required when writing in ES versions 6 and below. Expected [index]/[type], but got [%s]",
                        resource
                ));
            }
        }

        // Parse out the type if it exists and is valid.
        if (typeExists) {
            index = res.substring(0, slash);
            type = res.substring(slash + 1);
            typed = true;
            Assert.hasText(type, "No type found; expecting [index]/[type]");
        } else {
            index = res;
            type = UNDERSCORE_DOC;
            typed = false;
        }
        Assert.hasText(index, "No index found; expecting [index]/[type]");
        Assert.isTrue(!StringUtils.hasWhitespace(index) && !StringUtils.hasWhitespace(type), "Index/type should not contain whitespaces");

        // 4) Render the other endpoints
        String bulkEndpoint = "/_bulk";

        String ingestPipeline = settings.getIngestPipeline();
        if (StringUtils.hasText(ingestPipeline)) {
            Assert.isTrue(!StringUtils.hasWhitespace(ingestPipeline), "Ingest Pipeline name should not contain whitespaces");
            Assert.isTrue(!(ES_OPERATION_UPDATE.equals(settings.getOperation()) || ES_OPERATION_UPSERT.equals(settings.getOperation())), "Cannot specify an ingest pipeline when doing updates or upserts");
            bulkEndpoint = bulkEndpoint + "?pipeline=" + ingestPipeline;
        }

        // check bulk
        if (index.contains("{") || (typed && type.contains("{"))) {
            bulk = bulkEndpoint;
        } else if (typed){
            bulk = index + "/" + type + bulkEndpoint;
        } else {
            bulk = index + bulkEndpoint;
        }
        refresh = (index.contains("{") ? "/_refresh" : index + "/_refresh");
    }