第五篇 elasticsearch express插入数据

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 onclick="ceshi()">测试</button>

 

posted @ 2017-12-22 17:20  开始战斗  阅读(464)  评论(0编辑  收藏  举报