Hello I am implementing elasticsearch using nestjs
.
Currently, I am trying to add match keywords according to each condition, but I am having difficulties, so I leave a question.
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 *') '*',
},
},
],
},
},
},
});
In the code above, keyword
is a variable that may or may not exist.
But I am getting an error in the above code, how should I set the condition?
CodePudding user response:
const query = [];
if(keyword){
query.push({
query_string:{
query : xxx
}
})
}
const { hits } = await this.elasticsearchService.search({
index: 'myindex',
body: {
query: {
bool: {
must: query
},
},
},
});