如何从Elasticsearch Node.js客户端获取建议的查询值。
我有一种名为 test 的索引:
PUT test
{
"settings": {
"index": {
"number_of_shards": 1,
"analysis": {
"analyzer": {
"trigram": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase","shingle"]
},
"reverse": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase","reverse"]
}
},
"filter": {
"shingle": {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 3
}
}
}
}
},
"mappings": {
"properties": {
"word": {
"type": "text",
"fields": {
"trigram": {
"type": "text",
"analyzer": "trigram"
},
"reverse": {
"type": "text",
"analyzer": "reverse"
}
}
}
}
}
}
,并希望通过Elasticsearch客户的方式采用 suggest.simple_phrase 值:
getElasticSearchResult: (_, params, context) =>
new Promise((resolve, reject) => {
const param: RequestParams.Search = {
index: 'test',
body: {
suggest: {
text: 'params.searchText',
simple_phrase: {
phrase: {
field: 'word.trigram',
size: 1,
gram_size: 3,
direct_generator: [
{
field: 'word.trigram',
suggest_mode: 'always',
},
],
highlight: {
pre_tag: '<em>',
post_tag: '</em>',
},
},
},
},
},
};
client
.search(param)
.then((result: ApiResponse) => {
resolve(result.body.suggest.simple_phrase);
console.log('---- ', resolve(result.body));
// keep results in array (allRecords)
})
.catch((err: Error) => {
console.log(err);
reject(err);
});
}).then(v => {
return allRecords;
}),
},
但是result.body未定义,为什么?
请求正确发出,结果结构也正确,但是里面的所有内容都是空的。