// Path: src/main/java/org/elasticsearch/script/SharedMethods.java
// public class SharedMethods {
// 
//     public static Map<String, Object> getSourceAsMap(String source) throws IOException {
//         XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(source);
//         return parser.mapOrdered();
//     }
// 
// }


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.termvectors.TermVectorsRequest;
import org.elasticsearch.action.termvectors.TermVectorsResponse;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.termvectors.TermVectorsService;
import org.elasticsearch.script.SharedMethods;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.internal.InternalSearchHitField;
import org.elasticsearch.search.internal.SearchContext;
    public void hitExecute(SearchContext context, HitContext hitContext) {
        TermVectorsFetchBuilder fetchSubPhaseBuilder = (TermVectorsFetchBuilder)context.getSearchExt(NAME);
        if (fetchSubPhaseBuilder == null) {
            return;
        }
        if (hitContext.hit().fieldsOrNull() == null) {
            hitContext.hit().fields(new HashMap<String, SearchHitField>());
        }
        SearchHitField hitField = hitContext.hit().fields().get(NAME);
        if (hitField == null) {
            hitField = new InternalSearchHitField(NAME, new ArrayList<>(1));
            hitContext.hit().fields().put(NAME, hitField);
        }
        TermVectorsRequest request = fetchSubPhaseBuilder.getRequest();
        request.id(hitContext.hit().id());
        request.type(hitContext.hit().type());
        request.index(context.indexShard().shardId().getIndexName());
        TermVectorsResponse termVector = TermVectorsService.getTermVectors(context.indexShard(), request);
        XContentBuilder builder;
        try {
            builder = jsonBuilder();
            builder.startObject();
            termVector.toXContent(builder, ToXContent.EMPTY_PARAMS);
            builder.endObject();
        } catch (IOException e) {
            throw new ElasticsearchException("could not build term vector respoonse", e);
        }


        try {
// Path: src/main/java/org/elasticsearch/script/SharedMethods.java
// public class SharedMethods {
// 
//     public static Map<String, Object> getSourceAsMap(String source) throws IOException {
//         XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(source);
//         return parser.mapOrdered();
//     }
// 
// }


// Path: src/main/java/org/elasticsearch/search/fetch/termvectors/TermVectorsFetchSubPhase.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.termvectors.TermVectorsRequest;
import org.elasticsearch.action.termvectors.TermVectorsResponse;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.termvectors.TermVectorsService;
import org.elasticsearch.script.SharedMethods;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.fetch.FetchSubPhase;
import org.elasticsearch.search.internal.InternalSearchHitField;
import org.elasticsearch.search.internal.SearchContext;

    public void hitExecute(SearchContext context, HitContext hitContext) {
        TermVectorsFetchBuilder fetchSubPhaseBuilder = (TermVectorsFetchBuilder)context.getSearchExt(NAME);
        if (fetchSubPhaseBuilder == null) {
            return;
        }
        if (hitContext.hit().fieldsOrNull() == null) {
            hitContext.hit().fields(new HashMap<String, SearchHitField>());
        }
        SearchHitField hitField = hitContext.hit().fields().get(NAME);
        if (hitField == null) {
            hitField = new InternalSearchHitField(NAME, new ArrayList<>(1));
            hitContext.hit().fields().put(NAME, hitField);
        }
        TermVectorsRequest request = fetchSubPhaseBuilder.getRequest();
        request.id(hitContext.hit().id());
        request.type(hitContext.hit().type());
        request.index(context.indexShard().shardId().getIndexName());
        TermVectorsResponse termVector = TermVectorsService.getTermVectors(context.indexShard(), request);
        XContentBuilder builder;
        try {
            builder = jsonBuilder();
            builder.startObject();
            termVector.toXContent(builder, ToXContent.EMPTY_PARAMS);
            builder.endObject();
        } catch (IOException e) {
            throw new ElasticsearchException("could not build term vector respoonse", e);
        }


        try {
            Map<String, Object> termVectorAsMap = SharedMethods.getSourceAsMap(builder.string());