package io.jenkins.plugins.datastore.support;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ElasticsearchTransformer {

  private static Logger logger = LoggerFactory.getLogger(ElasticsearchTransformer.class);

  public static JSONObject transformGet(GetResponse get) {
    final JSONObject json = new JSONObject();
    get.getSource().entrySet().stream().forEach((entry) -> {
      json.put(entry.getKey(), entry.getValue());
    });
    return json;
  }

  public static JSONArray transformHits(SearchHits hits) {
    final JSONArray json = new JSONArray();
    hits.forEach((hit) -> json.put(transformHit(hit)));
    return json;
  }

  public static JSONObject transformHit(SearchHit hit) {
    final JSONObject json = new JSONObject();
    hit.getFields().entrySet().stream().forEach((entry) -> {
      final String key = entry.getKey();
      if (key.equals("labels") || key.equals("categories")) {
        json.put(key, entry.getValue().getValues());
      } else {
        json.put(key, entry.getValue().getValues().get(0));
      }
    });
    json.put("highlight", hit.getHighlightFields().get("excerpt").fragments()[0].string());
    return json;
  }

}

@@ -1255,24 +1255,85 @@
   %7D%0A    %7D);%0A
+    if (hit.getHighlightFields().containsKey(%22excerpt%22)) %7B%0A  
     json.put
@@ -1408,24 +1408,30 @@
 .string());%0A
+    %7D%0A
     return j

Fix possible NPE