簡體   English   中英

nodejs或nestjs中基於條件的elasticsearch方法

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM