def SearchDataAdapter(results, format, collection):
  if results and results['response'] and results['response']['docs']:
    search_data = results['response']['docs']

    if collection['template']['fieldsSelected']:
      headers = collection['template']['fieldsSelected']
    else:
      headers = [field['name'] for field in collection['fields']]

    rows = []

    for data in search_data:
      row = []
      for column in headers:
        if column not in data:
          row.append("")
        elif isinstance(data[column], basestring) or isinstance(data[column], (int, long, float, complex)):
          row.append(data[column])
        elif isinstance(data[column], list): # Multivalue field
          row.append([smart_str(val, errors='replace') for val in data[column]])
        else:
          row.append(smart_str(data[column]))
      rows.append(row)
  else:
    rows = [[]]

  yield headers, rows
SearchDataAdapter(results, format, db) -> headers, 2D array of data.
dataset/ETHPy150Open cloudera/hue/apps/search/src/search/data_export.py/SearchDataAdapter