您好,我正在使用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是一個可能存在也可能不存在的變數。
但是我在上面的代碼中遇到錯誤,我應該如何設定條件?
uj5u.com熱心網友回復:
const query = [];
if(keyword){
query.push({
query_string:{
query : xxx
}
})
}
const { hits } = await this.elasticsearchService.search({
index: 'myindex',
body: {
query: {
bool: {
must: query
},
},
},
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537898.html
標籤:弹性搜索
