Skip to content

Instantly share code, notes, and snippets.

@ngs
Last active April 18, 2018 09:45
Show Gist options
  • Select an option

  • Save ngs/f2db11e12ac2548d7fcf2550c9a0a92f to your computer and use it in GitHub Desktop.

Select an option

Save ngs/f2db11e12ac2548d7fcf2550c9a0a92f to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk');
const path = require ('path');
const https = require ('https');
exports.handler = (event, context, callback) => {
const { query, endpoint, index, doctype } = event;
const size = 10;
const from = parseInt(event.from) || 0;
if (!query || !endpoint || !index || !doctype) {
callback(new Error('Missing required parameters'), event);
return;
}
function getESResult(method, apiPath, body, callback) {
const creds = new AWS.EnvironmentCredentials('AWS');
const esEndpoint = new AWS.Endpoint(endpoint);
const req = new AWS.HttpRequest(esEndpoint);
req.method =method;
req.path = path.join('/', index, doctype, apiPath);
console.log(req.path);
req.region = 'ap-northeast-1';
if (body) {
req.body = JSON.stringify(body);
}
req.headers['presigned-expires'] = false;
req.headers['Host'] = esEndpoint.host;
const signer = new AWS.Signers.V4(req, 'es');
signer.addAuthorization(creds, new Date());
const send = new AWS.NodeHttpClient();
send.handleRequest(req, null, (httpResp) => {
let body = '';
httpResp.on('data', (chunk) => {
body += chunk || '';
});
httpResp.on('end', (chunk) => {
body += chunk || '';
console.log(body);
try {
const data = JSON.parse(body);
callback(null, data);
} catch(e) {
console.log(e);
callback(e, null);
}
});
}, (err) => {
callback(err, null);
});
}
getESResult('POST', '_search', {
from, size,
query: {
match: {
body: query
}
}
}, (e, data) => {
const { hits, total } = data.hits;
let next = from + size;
if (next > total) {
next = null;
}
const articles = hits.map((hit) => {
return Object.assign({ id: hit._id }, hit._source)
});
callback(null, { total, next, articles });
});
};
const AWS = require('aws-sdk');
const path = require ('path');
const https = require ('https');
exports.handler = (event, context, callback) => {
console.log(event);
const { id, endpoint, index, doctype } = event;
const size = 10;
if (!id || !endpoint || !index || !doctype) {
callback(new Error('Missing required parameters' + id + endpoint + index + doctype), event);
return;
}
function getESResult(method, apiPath, body, callback) {
const creds = new AWS.EnvironmentCredentials('AWS');
const esEndpoint = new AWS.Endpoint(endpoint);
const req = new AWS.HttpRequest(esEndpoint);
req.method =method;
req.path = path.join('/', index, doctype, apiPath);
console.log(req.path);
req.region = 'ap-northeast-1';
if (body) {
req.body = JSON.stringify(body);
}
req.headers['presigned-expires'] = false;
req.headers['Host'] = esEndpoint.host;
const signer = new AWS.Signers.V4(req, 'es');
signer.addAuthorization(creds, new Date());
const send = new AWS.NodeHttpClient();
send.handleRequest(req, null, (httpResp) => {
let body = '';
httpResp.on('data', (chunk) => {
body += chunk || '';
});
httpResp.on('end', (chunk) => {
body += chunk || '';
console.log(body);
try {
const data = JSON.parse(body);
callback(null, data);
} catch(e) {
console.log(e);
callback(e, null);
}
});
}, (err) => {
callback(err, null);
});
}
getESResult('GET', encodeURIComponent(id), null, (e, data) => {
if (e) {
callback(e, null);
return;
}
const { body, title } = data._source || {};
getESResult('POST', '_search', {
from: 0, size,
query: {
more_like_this: {
fields: ['title', 'tags', 'body'],
like: title + ' ' + body
}
}
}, (e, data) => {
const { hits } = data.hits;
const articles = hits.map((hit) => {
return Object.assign({ id: hit._id }, hit._source)
});
callback(null, { articles });
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment