1.后端
在elasticsearch.js文件夹下添加:
function addDocument(document) { return elasticClient.index({ index: indexName, type: "suggest", body: { title: document.title, content: document.content, suggest: document.suggest } }); } exports.addDocument = addDocument;
2.在路由router文件夹下的documents.js文件下添加:
/* POST document to be indexed */ router.post('/document', function(req, res, next) { elastic.addDocument(req.body).then(function(result) { res.json(result) }); });
3.前段可以写一个测试的按钮:
function ceshi(){ $.post('documents/document/',{title:'hitElash',content:'hitElash',suggest:'hitElash'},function(rs){ }) } <button οnclick="ceshi()">测试</button>