我创建了我的第一个GET API调用,它针对弹性搜索索引进行模糊搜索,到目前为止一切运行良好(w00t w00t)。我虽然坚持了一件;我想解析一下从ElasticSearch返回的一些信息,特别是在_source
部分。我做了一些侦察并发现你可以使用映射函数来做到这一点,但是我无法在页面或控制台上显示结果。所以问题是,假设我只是想从name
中取出_source
字段是可能的,是否有一种首选方法可以做到这一点?
router.get('/suggest/:search', function(req, res, next) {
const searchTerm = req.params.search;
client.search({
index: 'product_pipeline_import',
type: 'jdbc-demo',
body: {
suggest: {
productSuggest: {
prefix: searchTerm,
completion: {
field: 'name',
fuzzy: {
fuzziness: 2
}
}
}
}
}
}).then(function(resp) {
var hits = resp.hits.hits.map(function(hit){
console.log(hit._source)
});
}, function(err) {
console.trace(err.message);
});
})
我正在尝试解析的内容:
{
"text": "Philips - Brilliance 27\" IPS LED HD Monitor - Text",
"_index": "test-index",
"_type": "products",
"_id": "Philips - Brilliance 27\" IPS LED HD Monitor - Textured Black",
"_score": 5,
"_source": {
"name": "Philips - Brilliance 27\" IPS LED HD Monitor - Textured Black",
"@version": "1",
"model": "272P4APJKEB",
"@timestamp": "2017-11-19T20:56:11.537Z",
"type": "products",
"manufacturer": "Philips"
}