我正在尝试编写一个Elasticsearch正则表达式,以排除具有包含子字符串的键的元素,比如书名。
elasticsearch docs建议使用以下代码段排除子字符串:
@&~(foo.+) # anything except string beginning with "foo"
但是,就我而言,我试图创建这样的过滤器并失败了。
{
query: {
constant_score: {
filter: {
bool: {
filter: query_filters,
},
},
},
},
size: 1_000,
}
def query_filters
[
{ regexp: { title: "@&~(red)" } },
# goal: exclude titles that start with "Red"
]
end
我在相同的查询过滤器中使用了其他正则表达式,但是我认为正则表达式传递给ES的方式中没有错误。
有任何想法吗?提前致谢!
最佳答案
更新:
我找到了一种解决方法:我可以在过滤器中添加must_not
子句。
{
query: {
constant_score: {
filter: {
bool: {
filter: query_filters,
must_not: must_not_filters,
},
},
},
},
size: 1_000,
}
def must_not_filters
[ { regexp: { title: "red.*" } } ]
end
仍然好奇原始正则表达式是否还有其他想法
关于regex - 如何在Elasticsearch正则表达式中排除子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50119173/