@Override
    public boolean next(Text key, MapWritable value) throws IOException {
      if (result == null) {
        result = client.query(query);
      }

      boolean hasNext = result.hasNext();

      if (!hasNext) {
        return false;
      }

      Map<String, Object> next = result.next();
      // we save the key as is since under the old API, we don't have to create a new Text() object
      currentKey = next.get("_id").toString();
      currentValue = (MapWritable) WritableUtils.toWritable(next.get("_source"));

      if (key != null) {
        key.set(currentKey);
      }
      if (value != null) {
        value.clear();
        value.putAll(currentValue);
      }

      // keep on counting
      read++;
      return true;
    }
    public void reduce(Text key, Iterable<TermFrequencyWritable> values, Context context)
        throws IOException, InterruptedException {

      HashMap<Text, IntWritable> map = new HashMap<Text, IntWritable>();
      for (TermFrequencyWritable val : values) {
        Text docID = new Text(val.getDocumentID());
        int freq = val.getFreq().get();
        if (map.get(docID) != null) {
          map.put(docID, new IntWritable(map.get(docID).get() + freq));
        } else {
          map.put(docID, new IntWritable(freq));
        }
      }

      MapWritable outputMap = new MapWritable();
      outputMap.putAll(map);
      context.write(key, outputMap);
    }