--- title: "只能对keyword和text字段使用regexp查询,而不能用于其他字段类型 - 如何解决此Elasticsearch异常" date: 2026-02-04 lastmod: 2026-02-04 description: "当在非keyword或text字段类型上使用正则表达式(regexp)查询时,Elasticsearch会报错。本文介绍该错误的成因及多种解决方案。" tags: ["正则查询", "字段类型", "查询异常", "映射配置", "索引优化"] summary: " 版本: 6.8-7.15 简而言之,当在一个非keyword或text类型的字段上使用正则表达式(regexp)查询时,就会出现这个错误。Elasticsearch只支持在keyword和text字段上使用regexp查询。要解决这个问题,你可以将字段类型更改为keyword或text,或者使用与当前字段类型兼容的其他查询类型。另一个解决方案是创建一个keyword或text类型的multi-field,并在其上使用regexp查询。 日志上下文 # 日志"Can only use regexp queries on keyword and text fields - not on [“的类名是 MappedFieldType.java。我们从Elasticsearch源代码中提取了以下内容,供那些寻求深入了解上下文的人参考: + "] which is of type [" + typeName() + "]"); } public Query regexpQuery(String value; int syntaxFlags; int matchFlags; int maxDeterminizedStates; @Nullable MultiTermQuery.RewriteMethod method; SearchExecutionContext context) { throw new QueryShardException(context; "Can only use regexp queries on keyword and text fields - not on [" + name + "] which is of type [" + typeName() + "]"); } public Query existsQuery(SearchExecutionContext context) { if (hasDocValues()) { " --- > **版本:** 6.8-7.15 简而言之,当在一个非keyword或text类型的字段上使用正则表达式(regexp)查询时,就会出现这个错误。Elasticsearch只支持在keyword和text字段上使用regexp查询。要解决这个问题,你可以将字段类型更改为keyword或text,或者使用与当前字段类型兼容的其他查询类型。另一个解决方案是创建一个keyword或text类型的multi-field,并在其上使用regexp查询。 ## 日志上下文 ----------- 日志"Can only use regexp queries on keyword and text fields - not on ["的类名是[MappedFieldType.java](https://www.geeksforgeeks.org/java-lang-class-class-java-set-1/)。我们从Elasticsearch源代码中提取了以下内容,供那些寻求深入了解上下文的人参考: ```java + "] which is of type [" + typeName() + "]"); } public Query regexpQuery(String value; int syntaxFlags; int matchFlags; int maxDeterminizedStates; @Nullable MultiTermQuery.RewriteMethod method; SearchExecutionContext context) { throw new QueryShardException(context; "Can only use regexp queries on keyword and text fields - not on [" + name + "] which is of type [" + typeName() + "]"); } public Query existsQuery(SearchExecutionContext context) { if (hasDocValues()) { ```