[英]elastic search method based on condition in nodejs or nestjs
您好,我正在使用nestjs
。
目前,我正在尝试根据每个条件添加匹配关键字,但我遇到了困难,所以我留下了一个问题。
const { hits } = await this.elasticsearchService.search({
index: 'myindex',
body: {
query: {
bool: {
must: [
{
match: {
type: type.join(' or '),
},
},
keyword && {
query_string: {
query:
'*' + keyword.replace(/ /g, '* AND *') + '*',
},
},
],
},
},
},
});
在上面的代码中, keyword
是一个可能存在也可能不存在的变量。
但是我在上面的代码中遇到错误,我应该如何设置条件?
const query = [];
if(keyword){
query.push({
query_string:{
query : xxx
}
})
}
const { hits } = await this.elasticsearchService.search({
index: 'myindex',
body: {
query: {
bool: {
must: query
},
},
},
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.