簡體   English   中英

腳本過濾器字段值在數組中

[英]script filter field value is in array

這是我的查詢的一部分:

 must_not: {
    script: {
      script: {
        source: "doc['type.keyword'].value=='CHANNEL' ? doc['id'].value == 0 : doc['id'].value.contains(['1','3','7'])",
        lang: 'painless'
      }
    }
  }

如何檢查doc['id'].value是否具有數組中的值?

我嘗試了不同的方法:

source:"doc['type.keyword'].value=='CHANNEL' ? doc['id'].value == 0 : params.ids.values.contains(doc['id'].value)",
 params: {
   ids: [5,6]
}

source:"doc['type.keyword'].value=='CHANNEL' ? doc['id'].value == 0 : params.ids.contains(doc['id'].value)",
 params: {
   ids: ['5','6']
}

source:"doc['type.keyword'].value=='CHANNEL' ? doc['id'].value == 0 : for (el in params.ids) {doc['id'].value==el}",
 params: {
   ids: ['5','6']
} . ERROR

唯一的方法是沒有數組,但不能幫助我:

source:"doc['type.keyword'].value=='CHANNEL' ? doc['id'].value == 0 : doc['id'].value==params.ids",
 params: {
   ids: 5
}

完整的查詢:

searchStr = 'a string' //dynamic
types = ['CHANNEL', 'AUDIO', 'ALBUM', 'MOVIE'] .  ///dynamic array of strings
searchAllTypes = ['CHANNEL', 'AUDIO', 'ALBUM', 'MOVIE']
excludedContent = [1, 5, 6, 8] - //a dynamic array
genres = 'aString' //dynamic
tag = 3 //dynamic
       query:
       {
         bool: {
           should:
           [
               //1.here is a function I use, but I added the entire code so it can be read better
               return types.map(type => {
             type === 'CHANNEL' ? excludedContentIds = [] : excludedContentIds = excludedContents; //2.this is my workaround -
   the line I want to get rid of
             return {
               bool: {
                 must: [
                   { match: { 'type': type } }, //3.if I get rid of line (2) I would use filter - terms - all types here. 
                   //But trying to use terms in combination with 'execution='OR'' is not working inside filters anymore, but inside
   query  
                   { match: { 'discoverable': true } },
                   { match: { 'status': 'PUBLISHED' } },
                   { match: { 'category.name': genres } },
                   { match: { 'tag.id': tag } },
                   {
                     multi_match: {
                       query: searchStr,
                       type: 'phrase_prefix',
                       slop: 5,
                       fields: config.elasticSearch.search.fields,
                       operator: 'AND',
                     }
                   }
                 ],
                 //// must_not workaround now, because I'm filtering excludedContentIds for CHANNEL in the function above (2)
                 must_not: [
                   { terms: { 'id': excludedContentIds } }
                 ]
               }
             };
           });
             ]
         }
       },
       aggs: {
         'by_top_first': {
           terms: {
             field: 'type.keyword',
               size: searchAllTypes.length,
             },
           aggs: {
             'by_top_hit': { top_hits: { size: 1 } },
             'max_score': { max: { script: '_score' } },
           }
         }
       },
       {
         'by_type': {
           terms: {
             field: 'type.keyword',
               size: searchAllTypes.length
           },
           aggs: {
             'by_top_hit': { top_hits: { size: limitSize ? limitSize : 6 } },
             'max_score': { max: { script: '_score' } },
           }
         }
       }

您可以簡單地將數組值聲明為腳本參數,然后在腳本中使用它來測試它是否包含值:

must_not: {
  script: {
    script: {
      source: "doc['type.keyword'].value=='CHANNEL' ? doc['id'].value == 0 : params.ids.contains(doc['id'].value)",
      params: {
        ids: ['1','3','7']
      },
      lang: 'painless'
    }
  }
}

有關更多信息,請參見無痛文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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