You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch正则表达式搜索求助:匹配重复内容失败

解决方案

1. 修正字段名错误

你的查询中使用了equipment.keyword,但实际字段名为equipments.keyword(多了一个s),这是导致匹配失败的核心原因之一。

2. 调整正则表达式

原正则.*Name:.*oo ba.*要求oo后直接跟空格和ba,但foo bar里oo和ba之间隔着空格与r,无法匹配。需要修改为允许中间存在任意字符的正则:

{
  "regexp": {
    "equipments.keyword": {
      "value": ".*Name:.*oo.*ba.*"
    }
  }
}

如果要精准匹配Name:foo bar,可以使用更明确的正则:

{
  "regexp": {
    "equipments.keyword": {
      "value": ".*Name:foo bar.*"
    }
  }
}

3. 优化字段结构(长期方案)

当前equipments字段是结构化的键值对字符串,用正则查询效率低且易出错。建议重新设计字段映射,将其拆分为嵌套对象类型:

"equipments": {
  "type": "nested",
  "properties": {
    "Name": {"type": "text", "fields": {"keyword": {"type": "keyword"}}},
    "price": {"type": "integer"}
  }
}

数据存储格式改为:

"equipments": [
  {"Name": "foo bar", "price": 1000},
  {"Name": "foo bar", "price": 1200}
]

之后可以用嵌套查询精准匹配,示例:

{
  "nested": {
    "path": "equipments",
    "query": {
      "match": {
        "equipments.Name": "foo bar"
      }
    }
  }
}

内容的提问来源于stack exchange,提问作者AyoubMK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:10:18