/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed 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.hadoop.rest;


/**
 * ElasticSearch Rest Resource (index or query).
 */
public class Resource {

    private final StringBuilder resource;
    // cleaned up index and type with trailing "/"
    private final String root;
    private final String type;

    public Resource(String resource) {
        this.resource = new StringBuilder(resource);
        int index = resource.lastIndexOf("_");
        if (index <= 0) {
            index = resource.length();
        }
        String localRoot = resource.substring(0, index);
        if (!localRoot.endsWith("/")) {
            localRoot = localRoot + "/";
        }
        root = localRoot;
        index = localRoot.substring(0, root.length() - 1).lastIndexOf("/");
        type = root.substring(index, root.length() - 1);
    }

    String bulkIndexing() {
        return root + "_bulk";
    }

    // https://github.com/elasticsearch/elasticsearch/issues/2726
    String targetShards() {
        return root + "_search_shards";
    }

    String mapping() {
        return root + "_mapping";
    }

    String indexAndType() {
        return root;
    }

    public String type() {
        return type;
    }
}


@@ -886,16 +886,48 @@
 ng type;
+%0A    private final String index;
 %0A%0A    pu
@@ -1024,21 +1024,24 @@
     int 
-index
+location
  = resou
@@ -1074,21 +1074,24 @@
     if (
-index
+location
  %3C= 0) %7B
@@ -1103,21 +1103,24 @@
         
-index
+location
  = resou
@@ -1192,21 +1192,24 @@
 ring(0, 
-index
+location
 );%0A     
@@ -1328,21 +1328,24 @@
         
-index
+location
  = local
@@ -1429,21 +1429,28 @@
 bstring(
-index
+location + 1
 , root.l
@@ -1463,16 +1463,61 @@
 ) - 1);%0A
+        index = root.substring(0, location);%0A
     %7D%0A%0A 
@@ -1895,12 +1895,69 @@
 e;%0A    %7D
+%0A%0A    public String index() %7B%0A        return index;%0A    %7D
 %0A%7D%0A%0A

fix bug in detecting the index vs type