regex - 如何在Elasticsearch正则表达式中排除子字符串

标签 regex elasticsearch

我正在尝试编写一个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/

相关文章:

python - 使用 Python 正则表达式从字符串中提取十进制数

java - 正则表达式的替代品

javascript - 正则表达式捕获字符集但不捕获某些特定字符

elasticsearch - Elasticsearch将日期与斜杠字符匹配

elasticsearch - 如何使用 nsq 作为 ELK 堆栈的代理(ElasticSearch+LogStash+Kibana)

匹配此 : 090129 YBB 100 的正则表达式

node.js - 如何在我的 Dockerfile 中添加 discovery.type=single-node

elasticsearch - 使用省略的Elasticsearch索引

elasticsearch - 匹配列表中的多个字段?

c# - 按组名称匹配正则表达式中的组的更好方法是什么