gpt4 book ai didi

elasticsearch - 脚本过滤器字段值在数组中

转载 作者:行者123 更新时间:2023-12-02 23:55:30 26 4
gpt4 key购买 nike

这是我查询的一部分:

 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'
}
}
}

有关更多信息,请参见 Painless docs

关于elasticsearch - 脚本过滤器字段值在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53856272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com