【问题标题】:Elastic search is returning undefined for _id in elastic search弹性搜索在弹性搜索中返回未定义的 _id
【发布时间】:2017-03-26 06:25:57
【问题描述】:

我在我的应用程序中使用弹性搜索,一切正常,直到您单击返回 undefined 的搜索结果链接。我遇到的问题是_source._id 正在返回undefined。所以我无法查看搜索结果。

如何在/blog/article/<%= data[i]._source._id 中传递对象ID?

我的代码在下面

路由器.js

router.post('/search', function(req, res, next) {
  res.redirect('/search?q=' + req.body.q);
});

router.get('/search', function(req, res, next) {
  if (req.query.q) {
    Article.search({
      query_string: { query: req.query.q}
    }, function(err, results) {
      results:
      if (err) return next (err);
      var data = results.hits.hits.map(function(hit) {
        return hit;
      });
      res.render('main/search-result', {
        query: req.query.q,
        data: data
      });
    });
  }
});

搜索结果.ejs

<% for (var i=0; i < data.length; i++) { %>
         <div class="col-md-4">
           <a href="/blog/article/<%= data[i]._source._id %>">
             <div class="thumbnail">
               <h3 id="data_title"><%= data[i]._source.title %></h3>
             </div>
         </div>
       <% } %>

【问题讨论】:

  • p0k8_ 你看到我的代码中缺少什么了吗?

标签: node.js mongodb elasticsearch


【解决方案1】:

终于通过修改解决了这个问题

<a href="/blog/article/<%= data[i]._source._id %>">

<a href="/blog/article/<%= data[i]._id %>">

【讨论】:

    猜你喜欢
    • 2015-09-16
    • 2021-11-23
    • 2018-10-26
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2016-02-27
    相关资源
    最近更新 更多