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

如何在Solr中排除分组后匹配否定关键词的广告组结果

针对单关键词文档结构的解决方案

每个关键词作为独立文档存储时,核心思路是先通过子查询定位出所有最高得分关键词为negative的adset_id,再在主查询中排除这些adset,同时保留分组取top1的逻辑。

具体查询参数如下:

/select
?group.field=adset_id
&group.limit=1
&sort=score desc
&group=true
&defType=edismax
&qf=kw_exact_test_bool2^6 kw_broad_test_bool2 kw_negative_test_bool2^7
&rows=200
&fl=adset_id,kw_broad,kw_exact,kw_negative,match_type
&q=dress
&fq=-adset_id:(
    {!subquery cache=false}
    q=dress
    group=true
    group.field=adset_id
    group.limit=1
    sort=score desc
    fl=adset_id
    fq=match_type:negative
)

逻辑说明

  1. 子查询部分:按adset_id分组,每组取得分最高的1条文档,仅保留adset_id字段,并筛选出其中match_type为negative的结果,得到所有需要排除的广告组ID。
  2. 主查询部分:通过-adset_id:(...)排除上述广告组,再执行常规的分组取top1逻辑,最终返回的结果就是符合要求的广告组及其最高得分关键词。
针对嵌套文档结构的解决方案

嵌套文档结构下,推荐索引预处理+block join查询的方案,平衡性能与需求:

步骤1:索引时预处理父文档字段

在adset父文档中新增一个字段(如top_priority_match_type),预先计算并存储该广告组下所有关键词中,权重最高的match_type(根据你的权重配置:negative^7 > exact^6 > broad^1)。例如:

  • 如果广告组存在negative关键词,top_priority_match_type设为negative
  • 否则如果有exact关键词,设为exact
  • 否则设为broad

步骤2:查询时过滤+获取top1关键词

利用block join查询,先排除top_priority_match_type:negative的父文档,再获取每个有效父文档下得分最高的关键词:

/select
?q={!parent which="type:adset"}dress
&fq=-top_priority_match_type:negative
&fl=adset_id,kw_broad,kw_exact,kw_negative,match_type
&group.field=adset_id
&group.limit=1
&sort=score desc
&group=true
&defType=edismax
&qf=kw_exact_test_bool2^6 kw_broad_test_bool2 kw_negative_test_bool2^7

逻辑说明

  1. 通过{!parent which="type:adset"}dress匹配所有包含匹配关键词子文档的父广告组。
  2. 用fq=-top_priority_match_type:negative直接排除最高优先级为否定关键词的广告组。
  3. 最后按adset_id分组取每组得分最高的关键词文档,得到符合要求的结果。

如果无法做索引预处理,也可以用实时子查询的方式,但性能会受影响:

/select
?q={!parent which="type:adset"}dress
&fq=-{!child of="type:adset"}(match_type:negative AND {!func}max(score) OVER (PARTITION BY adset_id))
&fl=adset_id,kw_broad,kw_exact,kw_negative,match_type
&group.field=adset_id
&group.limit=1
&sort=score desc
&group=true
&defType=edismax
&qf=kw_exact_test_bool2^6 kw_broad_test_bool2 kw_negative_test_bool2^7

该查询通过窗口函数max(score) OVER (PARTITION BY adset_id)判断当前negative关键词是否为组内得分最高的,进而排除对应的父广告组。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 15:35:36