简体   繁体   中英

elastic search method based on condition in nodejs or nestjs

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?

const query = [];
if(keyword){
   query.push({
      query_string:{
         query : xxx
      }
   })
}

const { hits } = await this.elasticsearchService.search({
      index: 'myindex',
      body: {
        query: {
          bool: {
            must: query
          },
        },
      },
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM