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

Elasticsearch:如何过滤掉包含指定字符串的URL搜索结果

Elasticsearch 查询过滤:排除 URL 包含 /history 的记录

你需要通过bool组合查询实现过滤需求,在保留原用户ID匹配条件的基础上,添加必须排除的规则。修改后的完整查询如下:

GET /user_log/_search 
{
  "size" : 50,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "user_id": 56678
          }
        }
      ],
      "must_not": [
        {
          "wildcard": {
            "url": "*\/history*"
          }
        }
      ]
    }
  }
}

关键修改说明:

  • bool查询用于组合多条件:
    • must数组:保留原有的match查询,确保结果仅包含user_id为56678的日志
    • must_not数组:添加wildcard规则,匹配并排除所有url字段中包含/history子串的记录

替代方案(更高效的文本匹配)

如果url字段是文本类型,可以用match_phrase替代wildcard,执行效率更高:

"must_not": [
  {
    "match_phrase": {
      "url": "/history"
    }
  }
]

两种方式都能满足过滤需求:wildcard适合任意位置的子串模糊匹配,match_phrase针对连续短语精准匹配,可根据实际场景选择。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 13:50:30