Skip to content

Instantly share code, notes, and snippets.

@jrishabh55
Created April 23, 2018 11:21
Show Gist options
  • Save jrishabh55/bf1d7d1259f1a49f4b2c00dd4a13c890 to your computer and use it in GitHub Desktop.
Save jrishabh55/bf1d7d1259f1a49f4b2c00dd4a13c890 to your computer and use it in GitHub Desktop.
import { saveAs } from 'file-saver';
import { unParse } from 'papaparse';
const HOST = 'https://your-es-domain.com';
const appname = 'your-es-app';
const type = 'your-es-type';
const jsonData = [];
const credentials = "username:password";
// This should be updated by the onQueryChange method on the result component
const currentQuery = null;
getScrollApiData = (data) => {
const hits = data.hits.hits;
jsonData = jsonData.concat(hits);
let str = null;
/**
* Checking if the current data length is less then the total hits from the ES results,
* If yes calling the scrollApi function again to fetch the remaining data.
*
* */
if (jsonData.length < data.hits.total) {
const scrollObj = {
scroll: '1m',
scroll_id: data._scroll_id
};
scrollApi({ activeQuery: scrollObj, scroll: true, scroll_id: data._scroll_id });
} else {
str = JSON.stringify(jsonData, null, 4);
jsonData = [];
}
return str;
}
defaultQuery = () => ({ query: { match_all: {} } });
exportData = () => {
const defaultQuery = defaultQuery();
const activeQuery = currentQuery ? currentQuery : defaultQuery;
scrollApi({ activeQuery });
};
/**
* Param info { activeQuery, scroll, scroll_id }
*/
scrollApi = (info) => {
scrollQuery(info.activeQuery, info.scroll, info.scroll_id)
.then((res) => {
const data = getScrollApiData(res);
if (data) {
try {
let exportData = JSON.parse(data);
exportData = exportData.map((value) => {
const item = Object.assign(value, value._source);
return flatten(item);
});
// Unparsing the data using papaparse package,
// A Custom implementation can also be accepted
const newData = unparse(exportData);
const file = new File([newData], 'data.csv', { type: 'text/comma-separated-values;charset=utf-8' });
// Downloading the file using file-saver package
saveAs(file);
} catch (e) {
console.error(e);
}
}
});
}
/**
* A function to convert multilevel object to single level object and use key value pares as Column and row pairs using recursion
*/
flatten = (data) => {
const result = {};
function recurse(cur, prop = '') {
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
const l = cur.length;
for (let i = 0; i < l; i += 1) {
recurse(cur[i], `${prop}[${i}]`);
}
if (l === 0) {
result[prop] = [];
}
} else {
let isEmpty = true;
Object.keys(cur).forEach((p) => {
isEmpty = false;
recurse(cur[p], prop ? `${prop}.${p}` : p);
});
if (isEmpty && prop) {
result[prop] = {};
}
}
}
recurse(data);
return result;
}
/**
* Setting up the query,
* If scrollId is defined then it will continue the last scroll call and fetch the next chunk of data.
*/
scrollQuery = (queryBody, scroll, scrollId) => {
const createUrl = `${HOST}/${appname}/${type}/_search?scroll=5m`;
const scrollUrl = `${HOST}/_search/scroll?scroll=5m&scroll_id=${scrollId}`;
const finalUrl = scroll ? scrollUrl : createUrl;
return applyQuery(finalUrl, Object.assign({ size: 1000 }, queryBody));
}
// Calling the ES using fetch for the given query
applyQuery = (url, queryBody) => fetch(url, {
method: 'POST',
headers: {
Authorization: `Basic ${btoa(credentials)}`,
'content-type': 'application/json',
},
// credentials: 'include',
body: JSON.stringify(queryBody),
}).then(data => data.json());
@allendol
Copy link

This is working when I run in Google Chrome. When running with Edge, I am getting error on applyQuery (line 119).
Following is the error coming from Edge:
TypeError: Function expected at Anonymous function (http://localhost:3000/static/js/bundle.js:85606:17)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment