仅检索_source的内容 - Elasticsearch ( Node JS)



根据检索文档文档

 GET /website/blog/123/_source

将直接返回存储在_source字段中的文档。我目前正在使用Node JS的快速框架。我应该如何在代码中实现这一点?

esClient.search({
        index: "myIndex",
        type: "myType",
        body: {
            "query": {
                "match_all": {}
            },
            "size": 3,
            "from": 1
        }
    }).then(function (resp) {
        var result = resp.hits.hits;
        res.status(200).send({data: {recommendations: result, showItemFrom: showItemFrom}})
    }, function (err) {
        console.trace(err.message)
        res.status(500).send({data: err.message})
    })

我以这种方式得到回应...

[
  "_source":{
             {
                 "id": 1,
                 "title": "Test"
                }
            } 
]

但是,我想要这样...

[
   {
      id:1,
      title:"Test"
   }
]

我不认为Elasticsearch API有一种方法可以用于搜索,Val提到的方法有效,但它只能直接通过其ID获取文档。

但是你可以使用 Javascript Array#map(( 方法映射结果:

var result = resp.hits.hits.map(hit => hit._source);

之后

index:"myIndex"

加:

source:true

你需要调用 getSource() 函数,如下所示:

esClient.getSource({
    index: "website",
    type: "blog",
    id: "123"
}).then(function (source) {
   // do something with source
}, function (err) {
   // error happened
})

最新更新